本文整理匯總了Python中multiproduct.env.ProductEnvironment.db_transaction方法的典型用法代碼示例。如果您正苦於以下問題:Python ProductEnvironment.db_transaction方法的具體用法?Python ProductEnvironment.db_transaction怎麽用?Python ProductEnvironment.db_transaction使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類multiproduct.env.ProductEnvironment
的用法示例。
在下文中一共展示了ProductEnvironment.db_transaction方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ProductMilestoneTestCase
# 需要導入模塊: from multiproduct.env import ProductEnvironment [as 別名]
# 或者: from multiproduct.env.ProductEnvironment import db_transaction [as 別名]
class ProductMilestoneTestCase(MilestoneTestCase, MultiproductTestCase):
def setUp(self):
self.global_env = self._setup_test_env(create_folder=True)
self._upgrade_mp(self.global_env)
self._setup_test_log(self.global_env)
self._load_product_from_data(self.global_env, self.default_product)
self.env = ProductEnvironment(self.global_env, self.default_product)
self._load_default_data(self.env)
def tearDown(self):
shutil.rmtree(self.global_env.path)
self.global_env.reset_db()
self.env = self.global_env = None
def test_update_milestone(self):
self.env.db_transaction("INSERT INTO milestone (name) VALUES ('Test')")
milestone = Milestone(self.env, 'Test')
t1 = datetime(2001, 01, 01, tzinfo=utc)
t2 = datetime(2002, 02, 02, tzinfo=utc)
milestone.due = t1
milestone.completed = t2
milestone.description = 'Foo bar'
milestone.update()
self.assertEqual(
[('Test', to_utimestamp(t1), to_utimestamp(t2), 'Foo bar',
self.default_product)],
self.env.db_query("SELECT * FROM milestone WHERE name='Test'"))
示例2: EnvironmentUpgradeTestCase
# 需要導入模塊: from multiproduct.env import ProductEnvironment [as 別名]
# 或者: from multiproduct.env.ProductEnvironment import db_transaction [as 別名]
#.........這裏部分代碼省略.........
self._enable_multiproduct()
self.env.upgrade()
with self.env.db_direct_transaction as db:
self.assertEqual(
len(db('SELECT * FROM "dummy_table"')), 5)
self.assertEqual(
len(db('SELECT * FROM "@_dummy_table"')), 0)
def test_creating_new_product_calls_environment_created(self):
self._enable_component(DummyPlugin)
self._enable_multiproduct()
self.env.upgrade()
prod = Product(self.env)
prod.update_field_dict(dict(prefix='p1'))
ProductEnvironment(self.env, prod, create=True)
with self.env.db_direct_transaction as db:
db('SELECT * FROM "p1_dummy_table"')
def test_migrating_to_multiproduct_with_custom_default_prefix(self):
ticket = self.insert_ticket('ticket')
self.env.config.set('multiproduct', 'default_product_prefix', 'xxx')
self._enable_multiproduct()
self.env.upgrade()
products = Product.select(self.env)
self.assertEqual(len(products), 1)
self.assertEqual(products[0].prefix, 'xxx')
def test_migration_to_multiproduct_preserves_ticket_ids(self):
for ticket_id in (1, 3, 5, 7):
with self.env.db_transaction as db:
cursor = db.cursor()
cursor.execute("INSERT INTO ticket (id) VALUES (%i)" % ticket_id)
db.update_sequence(cursor, 'ticket')
self._enable_multiproduct()
self.env.upgrade()
for ticket_id in (1, 3, 5, 7):
with self.product('@'):
ticket = Ticket(self.env, ticket_id)
self.assertEqual(ticket.id, ticket_id)
def test_can_insert_tickets_after_upgrade(self):
t1 = Ticket(self.env)
t1.summary = "test"
t1.insert()
self.assertEqual(t1.id, 1)
self._enable_multiproduct()
self.env.upgrade()
with self.product('@'):
ticket = Ticket(self.env)
ticket.summary = 'test'
ticket.insert()
self.assertEqual(ticket.id, 2)
def test_can_insert_tickets_with_same_id_to_different_products(self):
self._enable_multiproduct()
self.env.upgrade()
self.env.db_transaction("INSERT INTO ticket (id, summary)"
示例3: ProductMilestoneTestCase
# 需要導入模塊: from multiproduct.env import ProductEnvironment [as 別名]
# 或者: from multiproduct.env.ProductEnvironment import db_transaction [as 別名]
class ProductMilestoneTestCase(MilestoneTestCase, MultiproductTestCase):
def setUp(self):
self.global_env = self._setup_test_env(create_folder=True)
self._upgrade_mp(self.global_env)
self._setup_test_log(self.global_env)
self._load_product_from_data(self.global_env, self.default_product)
self.env = ProductEnvironment(self.global_env, self.default_product)
self._load_default_data(self.env)
def tearDown(self):
shutil.rmtree(self.global_env.path)
self.global_env.reset_db()
self.env = self.global_env = None
@unittest.skipUnless(threading, 'Threading required for test')
def test_milestone_threads(self):
""" Ensure that in threaded (e.g. mod_wsgi) situations, we get
an accurate list of milestones from Milestone.list
The basic strategy is:
thread-1 requests a list of milestones
thread-2 adds a milestone
thread-1 requests a new list of milestones
To pass, thread-1 should have a list of milestones that matches
those that are in the database.
"""
lock = threading.RLock()
results = []
# two events to coordinate the workers and ensure that the threads
# alternate appropriately
e1 = threading.Event()
e2 = threading.Event()
def task(add):
"""the thread task - either we are discovering or adding events"""
with lock:
env = ProductEnvironment(self.global_env,
self.default_product)
if add:
name = 'milestone_from_' + threading.current_thread().name
milestone = Milestone(env)
milestone.name = name
milestone.insert()
else:
# collect the names of milestones reported by Milestone and
# directly from the db - as sets to ease comparison later
results.append({
'from_t': set([m.name for m in Milestone.select(env)]),
'from_db': set(
[v[0] for v in self.env.db_query(
"SELECT name FROM milestone")])})
def worker1():
""" check milestones in this thread twice either side of ceding
control to worker2
"""
task(False)
e1.set()
e2.wait()
task(False)
def worker2():
""" adds a milestone when worker1 allows us to then cede control
back to worker1
"""
e1.wait()
task(True)
e2.set()
t1, t2 = [threading.Thread(target=f) for f in (worker1, worker2)]
t1.start()
t2.start()
t1.join()
t2.join()
r = results[-1] # note we only care about the final result
self.assertEqual(r['from_t'], r['from_db'])
def test_update_milestone(self):
self.env.db_transaction("INSERT INTO milestone (name) VALUES ('Test')")
milestone = Milestone(self.env, 'Test')
t1 = datetime(2001, 01, 01, tzinfo=utc)
t2 = datetime(2002, 02, 02, tzinfo=utc)
milestone.due = t1
milestone.completed = t2
milestone.description = 'Foo bar'
milestone.update()
self.assertEqual(
[('Test', to_utimestamp(t1), to_utimestamp(t2), 'Foo bar',
self.default_product)],
self.env.db_query("SELECT * FROM milestone WHERE name='Test'"))