Skip to content

Commit

Permalink
Test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Jun 1, 2020
1 parent 0910f76 commit 166fc48
Showing 1 changed file with 17 additions and 47 deletions.
64 changes: 17 additions & 47 deletions pony/orm/tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def set_hooks_to_do_nothing():
set_hooks_to_do_nothing()


def flush_for(*objects):
for obj in objects:
obj.flush()


class TestHooks(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand All @@ -79,15 +84,23 @@ def tearDown(self):
pass

@db_session
def test_1(self):
def test_1a(self):
p4 = Person(id=4, name='Bob', age=16)
p5 = Person(id=5, name='Lucy', age=23)
self.assertEqual(logged_events, [])
db.flush()
self.assertEqual(logged_events, ['BI_Bob', 'BI_Lucy', 'AI_Bob', 'AI_Lucy'])

@db_session
def test_2(self):
def test_1b(self):
p4 = Person(id=4, name='Bob', age=16)
p5 = Person(id=5, name='Lucy', age=23)
self.assertEqual(logged_events, [])
flush_for(p4, p5)
self.assertEqual(logged_events, ['BI_Bob', 'AI_Bob', 'BI_Lucy', 'AI_Lucy'])

@db_session
def test_2a(self):
p4 = Person(id=4, name='Bob', age=16)
p1 = Person[1] # auto-flush here
p2 = Person[2]
Expand All @@ -98,50 +111,7 @@ def test_2(self):
self.assertEqual(logged_events, ['BI_Bob', 'AI_Bob', 'BU_Mary', 'BI_Lucy', 'AU_Mary', 'AI_Lucy'])

@db_session
def test_3(self):
global do_before_insert
def do_before_insert(person):
some_person = Person.select().first() # should not cause infinite recursion
p4 = Person(id=4, name='Bob', age=16)
db.flush()


def flush_for(*objects):
for obj in objects:
obj.flush()


class ObjectFlushTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
setup_database(db)

@classmethod
def tearDownClass(cls):
teardown_database(db)

def setUp(self):
set_hooks_to_do_nothing()
with db_session:
db.execute('delete from Person')
p1 = Person(id=1, name='John', age=22)
p2 = Person(id=2, name='Mary', age=18)
p3 = Person(id=3, name='Mike', age=25)
logged_events[:] = []

def tearDown(self):
pass

@db_session
def test_1(self):
p4 = Person(id=4, name='Bob', age=16)
p5 = Person(id=5, name='Lucy', age=23)
self.assertEqual(logged_events, [])
flush_for(p4, p5)
self.assertEqual(logged_events, ['BI_Bob', 'AI_Bob', 'BI_Lucy', 'AI_Lucy'])

@db_session
def test_2(self):
def test_2b(self):
p4 = Person(id=4, name='Bob', age=16)
p1 = Person[1] # auto-flush here
p2 = Person[2]
Expand All @@ -157,7 +127,7 @@ def test_3(self):
def do_before_insert(person):
some_person = Person.select().first() # should not cause infinite recursion
p4 = Person(id=4, name='Bob', age=16)
p4.flush()
db.flush()


if __name__ == '__main__':
Expand Down

0 comments on commit 166fc48

Please sign in to comment.