本文整理汇总了Python中cms.db.Session.rollback方法的典型用法代码示例。如果您正苦于以下问题:Python Session.rollback方法的具体用法?Python Session.rollback怎么用?Python Session.rollback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cms.db.Session
的用法示例。
在下文中一共展示了Session.rollback方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestCaseWithDatabase
# 需要导入模块: from cms.db import Session [as 别名]
# 或者: from cms.db.Session import rollback [as 别名]
class TestCaseWithDatabase(unittest.TestCase):
"""TestCase subclass starting with a clean testing database."""
@classmethod
def setUpClass(cls):
cms.db.engine = create_engine(cms.config.database)
drop_db()
init_db()
cls.connection = cms.db.engine.connect()
cms.db.metadata.create_all(cls.connection)
@classmethod
def tearDownClass(cls):
drop_db()
cls.connection.close()
cms.db.engine.dispose()
def setUp(self):
self.session = Session()
def tearDown(self):
self.session.rollback()
def add_contest(self, **kwargs):
"""Add a contest."""
args = {
"name": unique_unicode_id(),
"description": unique_unicode_id(),
}
args.update(kwargs)
contest = Contest(**args)
self.session.add(contest)
return contest
def add_user(self, **kwargs):
"""Add a user."""
args = {
"username": unique_unicode_id(),
"password": "",
"first_name": unique_unicode_id(),
"last_name": unique_unicode_id(),
}
args.update(kwargs)
user = User(**args)
self.session.add(user)
return user
def add_participation(self, user=None, contest=None, **kwargs):
"""Add a participation."""
user = user if user is not None else self.add_user()
contest = contest if contest is not None else self.add_contest()
args = {
"user": user,
"contest": contest,
}
args.update(kwargs)
participation = Participation(**args)
self.session.add(participation)
return participation
def add_task(self, contest=None, **kwargs):
"""Add a task."""
contest = contest if contest is not None else self.add_contest()
args = {
"contest": contest,
"name": unique_unicode_id(),
"title": unique_unicode_id(),
}
args.update(kwargs)
task = Task(**args)
self.session.add(task)
return task
def add_dataset(self, task=None, **kwargs):
"""Add a dataset."""
task = task if task is not None else self.add_task()
args = {
"task": task,
"description": unique_unicode_id(),
"task_type": "",
"task_type_parameters": "",
"score_type": "",
"score_type_parameters": "",
}
args.update(kwargs)
dataset = Dataset(**args)
self.session.add(dataset)
return dataset
def add_testcase(self, dataset=None, **kwargs):
"""Add a testcase."""
dataset = dataset if dataset is not None else self.add_dataset()
args = {
"dataset": dataset,
"codename": unique_unicode_id(),
"input": unique_digest(),
"output": unique_digest(),
}
args.update(kwargs)
testcase = Testcase(**args)
#.........这里部分代码省略.........
示例2: TestCaseWithDatabase
# 需要导入模块: from cms.db import Session [as 别名]
# 或者: from cms.db.Session import rollback [as 别名]
class TestCaseWithDatabase(unittest.TestCase):
"""TestCase subclass starting with a clean testing database."""
@classmethod
def setUpClass(cls):
assert "fortesting" in str(cms.db.engine), \
"Monkey patching of DB connection string failed"
drop_db()
init_db()
cls.connection = cms.db.engine.connect()
cms.db.metadata.create_all(cls.connection)
@classmethod
def tearDownClass(cls):
drop_db()
cls.connection.close()
cms.db.engine.dispose()
def setUp(self):
self.session = Session()
def tearDown(self):
self.session.rollback()
def delete_data(self):
"""Delete all the data in the DB.
This is useful to call during tear down, for tests that rely on
starting from a clean DB.
"""
for table in itervalues(Base.metadata.tables):
self.session.execute(table.delete())
self.session.commit()
@staticmethod
def add_fsobject(digest, content):
dbbackend = DBBackend()
fobj = dbbackend.create_file(digest)
fobj.write(content)
dbbackend.commit_file(fobj, digest)
@staticmethod
def get_contest(**kwargs):
"""Create a contest"""
args = {
"name": unique_unicode_id(),
"description": unique_unicode_id(),
}
args.update(kwargs)
contest = Contest(**args)
return contest
def add_contest(self, **kwargs):
"""Create a contest and add it to the session"""
contest = self.get_contest(**kwargs)
self.session.add(contest)
return contest
@staticmethod
def get_user(**kwargs):
"""Create a user"""
args = {
"username": unique_unicode_id(),
"password": "",
"first_name": unique_unicode_id(),
"last_name": unique_unicode_id(),
}
args.update(kwargs)
user = User(**args)
return user
def add_user(self, **kwargs):
"""Create a user and add it to the session"""
user = self.get_user(**kwargs)
self.session.add(user)
return user
@staticmethod
def get_participation(user=None, contest=None, **kwargs):
"""Create a participation"""
user = user if user is not None else TestCaseWithDatabase.get_user()
contest = contest \
if contest is not None else TestCaseWithDatabase.get_contest()
args = {
"user": user,
"contest": contest,
}
args.update(kwargs)
participation = Participation(**args)
return participation
def add_participation(self, **kwargs):
"""Create a participation and add it to the session"""
participation = self.get_participation(**kwargs)
self.session.add(participation)
return participation
@staticmethod
def get_task(**kwargs):
#.........这里部分代码省略.........
示例3: DatabaseMixin
# 需要导入模块: from cms.db import Session [as 别名]
# 或者: from cms.db.Session import rollback [as 别名]
class DatabaseMixin(DatabaseObjectGeneratorMixin):
"""Mixin for tests with database access."""
@classmethod
def setUpClass(cls):
super(DatabaseMixin, cls).setUpClass()
assert "fortesting" in str(engine), \
"Monkey patching of DB connection string failed"
drop_db()
init_db()
@classmethod
def tearDownClass(cls):
drop_db()
super(DatabaseMixin, cls).tearDownClass()
def setUp(self):
super(DatabaseMixin, self).setUp()
self.session = Session()
def tearDown(self):
self.session.rollback()
super(DatabaseMixin, self).tearDown()
def delete_data(self):
"""Delete all the data in the DB.
This is useful to call during tear down, for tests that rely on
starting from a clean DB.
"""
for table in itervalues(metadata.tables):
self.session.execute(table.delete())
self.session.commit()
@staticmethod
def add_fsobject(digest, content):
dbbackend = DBBackend()
fobj = dbbackend.create_file(digest)
fobj.write(content)
dbbackend.commit_file(fobj, digest)
def add_contest(self, **kwargs):
"""Create a contest and add it to the session"""
contest = self.get_contest(**kwargs)
self.session.add(contest)
return contest
def add_announcement(self, **kwargs):
"""Create an announcement and add it to the session"""
announcement = self.get_announcement(**kwargs)
self.session.add(announcement)
return announcement
def add_user(self, **kwargs):
"""Create a user and add it to the session"""
user = self.get_user(**kwargs)
self.session.add(user)
return user
def add_participation(self, **kwargs):
"""Create a participation and add it to the session"""
participation = self.get_participation(**kwargs)
self.session.add(participation)
return participation
def add_message(self, **kwargs):
"""Create a message and add it to the session"""
message = self.get_message(**kwargs)
self.session.add(message)
return message
def add_question(self, **kwargs):
"""Create a question and add it to the session"""
question = self.get_question(**kwargs)
self.session.add(question)
return question
def add_task(self, **kwargs):
"""Create a task and add it to the session"""
task = self.get_task(**kwargs)
self.session.add(task)
return task
def add_statement(self, task=None, **kwargs):
"""Create a statement and add it to the session"""
task = task if task is not None else self.add_task()
args = {
"task": task,
"digest": unique_digest(),
"language": unique_unicode_id(),
}
args.update(kwargs)
statement = Statement(**args)
self.session.add(statement)
return statement
def add_dataset(self, **kwargs):
"""Create a dataset and add it to the session"""
dataset = self.get_dataset(**kwargs)
#.........这里部分代码省略.........