Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1764: TurboGears-1.5-DBTest-SQLAlchemy.patch

File TurboGears-1.5-DBTest-SQLAlchemy.patch, 2.2 KB (added by kvdb, 3 years ago)
  • turbogears/testutil.py

     
    165165 
    166166 
    167167class DBTest(unittest.TestCase): 
     168    """A database enabled unit testing class. 
    168169 
     170    Creates and destroys your database before and after each unit test, 
     171    based on the configuration defined in test.cfg.  This class supports 
     172    both SQLAlchemy and SQLObject configurations. 
     173 
     174    """ 
    169175    model = None 
    170176 
     177    def setUp(self): 
     178        if turbogears.config.server.get("sqlalchemy.dburi"): 
     179            self._sa_setUp() 
     180        elif turbogears.config.server.get("sqlobject.dburi"): 
     181            self._so_setUp() 
     182        else: 
     183            raise Exception("Unable to find sqlalchemy or sqlobject dburi") 
     184 
     185    def tearDown(self): 
     186        if turbogears.config.server.get("sqlalchemy.dburi"): 
     187            self._sa_tearDown() 
     188        elif turbogears.config.server.get("sqlobject.dburi"): 
     189            self._so_tearDown() 
     190 
     191    ## SQLAlchemy specific methods 
     192 
     193    def _sa_setUp(self): 
     194        database.get_engine() 
     195        database.metadata.create_all() 
     196 
     197    def _sa_tearDown(self): 
     198        database.metadata.drop_all() 
     199 
     200    ## SQLObject specific methods 
     201 
    171202    def _get_soClasses(self): 
    172203        try: 
    173204            return [self.model.__dict__[x] for x in self.model.soClasses] 
    174205        except AttributeError: 
    175206            return self.model.__dict__.values() 
    176207 
    177     def setUp(self): 
     208    def _so_setUp(self): 
    178209        if not self.model: 
    179210            self.model = get_model() 
    180211            if not self.model: 
    181                 raise "Unable to run database tests without a model" 
    182  
     212                raise Exception("Unable to run database tests without a model") 
    183213        for item in self._get_soClasses(): 
    184214            if isinstance(item, types.TypeType) and issubclass(item, 
    185215                sqlobject.SQLObject) and item != sqlobject.SQLObject \ 
    186216                and item != InheritableSQLObject: 
    187217                item.createTable(ifNotExists=True) 
    188218 
    189     def tearDown(self): 
     219    def _so_tearDown(self): 
    190220        database.rollback_all() 
    191221        for item in reversed(self._get_soClasses()): 
    192222            if isinstance(item, types.TypeType) and issubclass(item,