]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- moved selectresults test from orm to ext package
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 22 Sep 2006 00:33:44 +0000 (00:33 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 22 Sep 2006 00:33:44 +0000 (00:33 +0000)
- renamed objectstore test suite to unitofwork
- added additional "eagerdegrade" tests to mapper, to test fixes from #308

test/ext/alltests.py
test/ext/selectresults.py [moved from test/orm/selectresults.py with 100% similarity]
test/orm/alltests.py
test/orm/mapper.py
test/orm/session.py
test/orm/unitofwork.py [moved from test/orm/objectstore.py with 97% similarity]

index ee6ece89cdb2486e2b9d09a38843a1af7ebb526f..f1c631ba43df74c9848eba878c9ccfbccca48d07 100644 (file)
@@ -2,7 +2,7 @@ import testbase
 import unittest, doctest
 
 def suite():
-    unittest_modules = ['ext.activemapper']
+    unittest_modules = ['ext.activemapper', 'ext.selectresults']
     doctest_modules = ['sqlalchemy.ext.sqlsoup']
 
     alltests = unittest.TestSuite()
index f99df43b0407bab22fb2e8f03ed87ff121dbf1dd..814b2d760bfe89f4561e241294919112fbef3f31 100644 (file)
@@ -4,14 +4,13 @@ import unittest
 def suite():
     modules_to_test = (
         'orm.mapper',
-        'orm.selectresults',
         'orm.lazytest1',
         'orm.eagertest1',
         'orm.eagertest2',
         
         'orm.sessioncontext', 
-        'orm.objectstore',
-       'orm.session',
+        'orm.unitofwork',
+        'orm.session',
         'orm.cascade',
         'orm.relationships',
         'orm.association',
index bf16cdfc672d5d576f133541fc0c18a09213c868..257e90cf51df2973dcffc76c4c1699367ce7ff84 100644 (file)
@@ -7,6 +7,8 @@ from sqlalchemy.ext.sessioncontext import SessionContext
 from tables import *
 import tables
 
+"""tests general mapper operations with an emphasis on selecting/loading"""
+
 user_result = [{'user_id' : 7}, {'user_id' : 8}, {'user_id' : 9}]
 user_address_result = [
 {'user_id' : 7, 'addresses' : (Address, [{'address_id' : 1}])},
@@ -410,6 +412,37 @@ class MapperTest(MapperSuperTest):
             self.assert_result(l, User, *user_address_result)
         self.assert_sql_count(db, go, 4)
         
+        clear_mappers()
+        
+        # test with a deeper set of eager loads.  when we first load the three
+        # users, they will have no addresses or orders.  the number of lazy loads when
+        # traversing the whole thing will be three for the addresses and three for the 
+        # orders.
+        usermapper = mapper(User, users,
+            properties = {
+                'addresses':relation(mapper(Address, addresses), lazy=False),
+                'orders': relation(mapper(Order, orders, properties = {
+                    'items' : relation(mapper(Item, orderitems, properties = {
+                        'keywords' : relation(mapper(Keyword, keywords), itemkeywords, lazy=False)
+                    }), lazy=False)
+                }), lazy=False)
+            })
+
+        # first test straight eager load, 1 statement
+        def go():
+            l = usermapper.query(sess).select()
+            self.assert_result(l, User, *user_all_result)
+        self.assert_sql_count(db, go, 1)
+        
+        # then select just from users.  run it into instances.
+        # then assert the data, which will launch 6 more lazy loads
+        def go():
+            r = users.select().execute()
+            l = usermapper.instances(r, sess)
+            self.assert_result(l, User, *user_all_result)
+        self.assert_sql_count(db, go, 7)
+        
+        
     def testlazyoptions(self):
         """tests that an eager relation can be upgraded to a lazy relation via the options method"""
         sess = create_session()
index 263e5c9f724116454f77d65b4931ce2a2f0dff90..4ffd6a69941bab7b8b70d9270c40b161b40a8e8e 100644 (file)
@@ -122,10 +122,5 @@ class CascadingOrphanDeletionTest(AssertMixin):
         assert item.id is None
         assert attr.id is None
 
-
-
-
-
-
 if __name__ == "__main__":    
     testbase.main()
similarity index 97%
rename from test/orm/objectstore.py
rename to test/orm/unitofwork.py
index ec47749bd06cd8a69cbb13a5903f344f438b635d..c3744abec3f82a3c46e70e524953ea1acd195417 100644 (file)
@@ -9,7 +9,9 @@ import sqlalchemy.ext.assignmapper as assignmapper
 from tables import *
 import tables
 
-class SessionTest(AssertMixin):
+"""tests unitofwork operations"""
+
+class UnitOfWorkTest(AssertMixin):
     def setUpAll(self):
         global ctx, assign_mapper
         ctx = SessionContext(create_session)
@@ -22,15 +24,15 @@ class SessionTest(AssertMixin):
         ctx.current.clear()
         clear_mappers()
 
-class HistoryTest(SessionTest):
+class HistoryTest(UnitOfWorkTest):
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         users.create()
         addresses.create()
     def tearDownAll(self):
         addresses.drop()
         users.drop()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
         
     def testattr(self):
         """tests the rolling back of scalar and list attributes.  this kind of thing
@@ -83,9 +85,9 @@ class HistoryTest(SessionTest):
         u = s.query(m).select()[0]
         print u.addresses[0].user
 
-class CustomAttrTest(SessionTest):
+class CustomAttrTest(UnitOfWorkTest):
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         global sometable, metadata, someothertable
         metadata = BoundMetaData(testbase.db)
         sometable = Table('sometable', metadata,
@@ -109,11 +111,11 @@ class CustomAttrTest(SessionTest):
         f = Foo()
         assert isinstance(f.bars.data, MyList)
     def tearDownAll(self):
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
             
-class VersioningTest(SessionTest):
+class VersioningTest(UnitOfWorkTest):
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         ctx.current.clear()
         global version_table
         version_table = Table('version_test', db,
@@ -123,10 +125,10 @@ class VersioningTest(SessionTest):
         ).create()
     def tearDownAll(self):
         version_table.drop()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
     def tearDown(self):
         version_table.delete().execute()
-        SessionTest.tearDown(self)
+        UnitOfWorkTest.tearDown(self)
     
     @testbase.unsupported('mysql', 'mssql')
     def testbasic(self):
@@ -209,9 +211,9 @@ class VersioningTest(SessionTest):
         assert f1s2.id == f1s1.id
         assert f1s2.value == f1s1.value
         
-class UnicodeTest(SessionTest):
+class UnicodeTest(UnitOfWorkTest):
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         global metadata, uni_table, uni_table2
         metadata = BoundMetaData(testbase.db)
         uni_table = Table('uni_test', metadata,
@@ -223,7 +225,7 @@ class UnicodeTest(SessionTest):
         metadata.create_all()
     def tearDownAll(self):
         metadata.drop_all()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
     def tearDown(self):
         clear_mappers()
         for t in metadata.table_iterator(reverse=True):
@@ -260,10 +262,10 @@ class UnicodeTest(SessionTest):
         t1 = ctx.current.query(Test).get_by(id=t1.id)
         assert len(t1.t2s) == 2
         
-class PKTest(SessionTest):
+class PKTest(UnitOfWorkTest):
     @testbase.unsupported('mssql')
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         global table
         global table2
         global table3
@@ -294,7 +296,7 @@ class PKTest(SessionTest):
         table.drop()
         table2.drop()
         table3.drop()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
         
     # not support on sqlite since sqlite's auto-pk generation only works with
     # single column primary keys    
@@ -337,11 +339,11 @@ class PKTest(SessionTest):
         e.data = 'some more data'
         ctx.current.flush()
 
-class ForeignPKTest(SessionTest):
+class ForeignPKTest(UnitOfWorkTest):
     """tests mapper detection of the relationship direction when parent/child tables are joined on their
     primary keys"""
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         global metadata, people, peoplesites
         metadata = BoundMetaData(testbase.db)
         people = Table("people", metadata,
@@ -358,7 +360,7 @@ class ForeignPKTest(SessionTest):
         metadata.create_all()
     def tearDownAll(self):
         metadata.drop_all()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
     def testbasic(self):
         class PersonSite(object):pass
         class Person(object):pass
@@ -380,10 +382,10 @@ class ForeignPKTest(SessionTest):
         ctx.current.flush()
         assert people.count(people.c.person=='im the key').scalar() == peoplesites.count(peoplesites.c.person=='im the key').scalar() == 1
         
-class PrivateAttrTest(SessionTest):
+class PrivateAttrTest(UnitOfWorkTest):
     """tests various things to do with private=True mappers"""
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         global a_table, b_table
         a_table = Table('a',testbase.db,
             Column('a_id', Integer, Sequence('next_a_id'), primary_key=True),
@@ -397,7 +399,7 @@ class PrivateAttrTest(SessionTest):
     def tearDownAll(self):
         b_table.drop()
         a_table.drop()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
     
     def testsinglecommit(self):
         """tests that a commit of a single object deletes private relationships"""
@@ -454,12 +456,12 @@ class PrivateAttrTest(SessionTest):
         ctx.current.flush()
         assert b in sess.identity_map.values()
                 
-class DefaultTest(SessionTest):
+class DefaultTest(UnitOfWorkTest):
     """tests that when saving objects whose table contains DefaultGenerators, either python-side, preexec or database-side,
     the newly saved instances receive all the default values either through a post-fetch or getting the pre-exec'ed 
     defaults back from the engine."""
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         use_string_defaults = db.engine.__module__.endswith('postgres') or db.engine.__module__.endswith('oracle') or db.engine.__module__.endswith('sqlite')
 
         if use_string_defaults:
@@ -479,7 +481,7 @@ class DefaultTest(SessionTest):
         self.table.create()
     def tearDownAll(self):
         self.table.drop()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
     def setUp(self):
         self.table = Table('default_test', db)
     def testinsert(self):
@@ -529,14 +531,14 @@ class DefaultTest(SessionTest):
         ctx.current.flush()
         self.assert_(h1.foober == 'im the update')
         
-class SaveTest(SessionTest):
+class SaveTest(UnitOfWorkTest):
 
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         tables.create()
     def tearDownAll(self):
         tables.drop()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
         
     def setUp(self):
         keywords.insert().execute(
@@ -554,7 +556,7 @@ class SaveTest(SessionTest):
 
         #self.assert_(len(ctx.current.new) == 0)
         #self.assert_(len(ctx.current.dirty) == 0)
-        SessionTest.tearDown(self)
+        UnitOfWorkTest.tearDown(self)
 
     def testbasic(self):
         # save two users
@@ -1242,7 +1244,7 @@ class SaveTest(SessionTest):
         u.newyork_addresses.append(b)
         ctx.current.flush()
     
-class SaveTest2(SessionTest):
+class SaveTest2(UnitOfWorkTest):
 
     def setUp(self):
         ctx.current.clear()
@@ -1268,7 +1270,7 @@ class SaveTest2(SessionTest):
     def tearDown(self):
         self.addresses.drop()
         self.users.drop()
-        SessionTest.tearDown(self)
+        UnitOfWorkTest.tearDown(self)
     
     def testbackwardsnonmatch(self):
         m = mapper(Address, self.addresses, properties = dict(
@@ -1324,10 +1326,10 @@ class SaveTest2(SessionTest):
                         ]
         )
 
-class SaveTest3(SessionTest):
+class SaveTest3(UnitOfWorkTest):
 
     def setUpAll(self):
-        SessionTest.setUpAll(self)
+        UnitOfWorkTest.setUpAll(self)
         global metadata, t1, t2, t3
         metadata = testbase.metadata
         t1 = Table('items', metadata,
@@ -1348,7 +1350,7 @@ class SaveTest3(SessionTest):
         metadata.create_all()
     def tearDownAll(self):
         metadata.drop_all()
-        SessionTest.tearDownAll(self)
+        UnitOfWorkTest.tearDownAll(self)
 
     def setUp(self):
         pass