Changeset 5168
- Timestamp:
- 08/19/08 17:27:29 (5 months ago)
- Files:
-
- trunk/tg/tests/test_testutil.py (modified) (1 diff)
- trunk/tg/testutil.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tg/tests/test_testutil.py
r5162 r5168 10 10 from tg.tests.fixtures import model 11 11 12 class BaseDBTest(DBTest):13 """This is the same as DBTest, except that its parent is not called.14 15 It just sets the parameter call_dad to False, so that we can test this16 class; it'd be impossible to test it otherwise.17 18 """19 def __init__(self, *args, **kwargs):20 super(BaseDBTest, self).__init__(False, *args, **kwargs)21 12 13 # Ideally, we would have defined several different descendants of DBTest, 14 # in order to test its behavior in different situations, but there seem to be 15 # a problem in unittests and grand-grandchildren of TestCase won't work. You 16 # may try this code if you want: http://paste.turbogears.org/paste/4721 17 # or http://paste.turbogears.org/paste/4724 22 18 23 class EmptyTestCase(BaseDBTest): 24 pass 25 26 27 class DatabaseYesModelNo(BaseDBTest): 28 database = create_engine("sqlite:///:memory:") 29 30 31 class DatabaseNoModelYes(BaseDBTest): 32 model = model 33 34 35 class ValidDBTest(BaseDBTest): 19 class BaseModelTest(DBTest): 36 20 database = create_engine("sqlite:///:memory:") 37 21 model = model 38 22 39 23 40 class Test DatabaseBaseTesting(TestCase):41 """Test case for DBTest"""24 class TestGroup(BaseModelTest): 25 """Test case for the Group model. 42 26 43 def _create_row(self, value, test): 44 model.init_model(ValidDBTest.database) 45 test.setUp() 46 47 grp = model.Group() 48 grp.group_name = value 27 This should tell us whether the setUp() and tearDown() of DBTest work as 28 expected. 49 29 50 model.DBSession.save(grp) 30 """ 31 32 def test_group_creation(self): 33 group = model.Group() 34 group.group_name = u"turbogears" 35 group.display_name = u"The TurboGears Team" 36 model.DBSession.save(group) 51 37 model.DBSession.flush() 52 38 transaction.commit() 53 39 54 def test_no_database_no_model(self): 55 """The database and model to be tested must be defined""" 56 self.assertRaises(AssertionError, EmptyTestCase) 57 58 def test_no_database(self): 59 """The database to be tested must be defined""" 60 self.assertRaises(AssertionError, DatabaseYesModelNo) 61 62 def test_no_model(self): 63 """The model to be tested must be defined""" 64 self.assertRaises(AssertionError, DatabaseNoModelYes) 65 66 def test_valid_descendant(self): 67 """Everything is OK if and only if both the database and the model 68 are defined""" 69 ValidDBTest() 70 71 def test_setup(self): 72 """After the setup I must be able to insert rows""" 73 test = ValidDBTest() 74 self._create_row("developers", test) 75 76 def test_teardown(self): 77 """After the tearDown the tables must not exist""" 78 test = ValidDBTest() 79 self._create_row("directors", test) 80 test.tearDown() 81 82 self.assertRaises(DBAPIError, 83 model.DBSession.query(model.Group).filter( 84 model.Group.group_name=="directors").first 85 ) 86 40 def test_this_group_was_already_removed(self): 41 group = model.Group() 42 group.group_name = u"turbogears" 43 group.display_name = u"The TurboGears Team" 44 model.DBSession.save(group) 45 model.DBSession.flush() 46 transaction.commit() trunk/tg/testutil.py
r5161 r5168 15 15 database = None 16 16 17 def __init__(self, call_dad=True, *args, **kwargs): 18 """Create a test case to test the model 19 20 @param call_dad: The only way to test this class is by _not_ 21 calling unittest.TestCase.__init__() while running the test suite, 22 and this parameter enable us to do so. 23 @type call_dad: C{bool} 24 25 """ 26 if call_dad: 27 super(DBTest, self).__init__(*args, **kwargs) 17 def setUp(self): 28 18 assert self.model != None, "Database test cases must define the model" 29 19 assert self.database != None, "Database test cases must define the "\ 30 20 "database" 31 32 def setUp(self): 21 self.model.init_model(self.database) 33 22 self.model.metadata.create_all(self.database) 34 23 35 24 def tearDown(self): 36 25 self.model.metadata.drop_all(self.database) 37 self.model.metadata.clear() 38 try: 39 self.model.metadata.dispose() 40 except AttributeError: # not threadlocal 41 if self.model.metadata.bind: 42 self.model.metadata.bind.dispose() 43 self.model._engine = None 26