Changeset 5168

Show
Ignore:
Timestamp:
08/19/08 17:27:29 (5 months ago)
Author:
Gustavo
Message:

Corrected the tests for DBTest; now we're testing it indirectly, because it seems impossible to test TestCase descendants directly:

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tg/tests/test_testutil.py

    r5162 r5168  
    1010from tg.tests.fixtures import model 
    1111 
    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 this 
    16     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) 
    2112 
     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 
    2218 
    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): 
     19class BaseModelTest(DBTest): 
    3620    database = create_engine("sqlite:///:memory:") 
    3721    model = model 
    3822 
    3923 
    40 class TestDatabaseBaseTesting(TestCase): 
    41     """Test case for DBTest""" 
     24class TestGroup(BaseModelTest): 
     25    """Test case for the Group model. 
    4226     
    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. 
    4929     
    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) 
    5137        model.DBSession.flush() 
    5238        transaction.commit() 
    5339     
    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  
    1515    database = None 
    1616     
    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): 
    2818        assert self.model != None, "Database test cases must define the model" 
    2919        assert self.database != None, "Database test cases must define the "\ 
    3020                                      "database" 
    31      
    32     def setUp(self): 
     21        self.model.init_model(self.database) 
    3322        self.model.metadata.create_all(self.database) 
    3423     
    3524    def tearDown(self): 
    3625        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