summaryrefslogtreecommitdiff
path: root/test/base
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-06-05 17:25:51 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-06-05 17:25:51 +0000
commit120dcee5a71187d4bebfe50aedbbefb09184cac1 (patch)
treef2a090a510c8df405d0b1bef2936bafa3511be07 /test/base
parentf8314ef9ff08af5f104731de402d6e6bd8c043f3 (diff)
downloadsqlalchemy-120dcee5a71187d4bebfe50aedbbefb09184cac1.tar.gz
reorganized unit tests into subdirectories
Diffstat (limited to 'test/base')
-rw-r--r--test/base/__init__.py0
-rw-r--r--test/base/alltests.py21
-rw-r--r--test/base/attributes.py171
-rw-r--r--test/base/dependency.py142
-rw-r--r--test/base/historyarray.py71
5 files changed, 405 insertions, 0 deletions
diff --git a/test/base/__init__.py b/test/base/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/test/base/__init__.py
diff --git a/test/base/alltests.py b/test/base/alltests.py
new file mode 100644
index 000000000..2ef5b8794
--- /dev/null
+++ b/test/base/alltests.py
@@ -0,0 +1,21 @@
+import testbase
+import unittest
+
+def suite():
+ modules_to_test = (
+ # core utilities
+ 'base.historyarray',
+ 'base.attributes',
+ 'base.dependency',
+ )
+ alltests = unittest.TestSuite()
+ for name in modules_to_test:
+ mod = __import__(name)
+ for token in name.split('.')[1:]:
+ mod = getattr(mod, token)
+ alltests.addTest(unittest.findTestCases(mod, suiteClass=None))
+ return alltests
+
+
+if __name__ == '__main__':
+ testbase.runTests(suite())
diff --git a/test/base/attributes.py b/test/base/attributes.py
new file mode 100644
index 000000000..bff864fa6
--- /dev/null
+++ b/test/base/attributes.py
@@ -0,0 +1,171 @@
+from testbase import PersistTest
+import sqlalchemy.util as util
+import sqlalchemy.attributes as attributes
+import unittest, sys, os
+import pickle
+
+
+class MyTest(object):pass
+
+class AttributesTest(PersistTest):
+ """tests for the attributes.py module, which deals with tracking attribute changes on an object."""
+ def testbasic(self):
+ class User(object):pass
+ manager = attributes.AttributeManager()
+ manager.register_attribute(User, 'user_id', uselist = False)
+ manager.register_attribute(User, 'user_name', uselist = False)
+ manager.register_attribute(User, 'email_address', uselist = False)
+
+ u = User()
+ print repr(u.__dict__)
+
+ u.user_id = 7
+ u.user_name = 'john'
+ u.email_address = 'lala@123.com'
+
+ print repr(u.__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == 'lala@123.com')
+ manager.commit(u)
+ print repr(u.__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == 'lala@123.com')
+
+ u.user_name = 'heythere'
+ u.email_address = 'foo@bar.com'
+ print repr(u.__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'heythere' and u.email_address == 'foo@bar.com')
+
+ manager.rollback(u)
+ print repr(u.__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == 'lala@123.com')
+
+ def testpickleness(self):
+ manager = attributes.AttributeManager()
+ manager.register_attribute(MyTest, 'user_id', uselist = False)
+ manager.register_attribute(MyTest, 'user_name', uselist = False)
+ manager.register_attribute(MyTest, 'email_address', uselist = False)
+ x = MyTest()
+ x.user_id=7
+ pickle.dumps(x)
+
+ def testlist(self):
+ class User(object):pass
+ class Address(object):pass
+ manager = attributes.AttributeManager()
+ manager.register_attribute(User, 'user_id', uselist = False)
+ manager.register_attribute(User, 'user_name', uselist = False)
+ manager.register_attribute(User, 'addresses', uselist = True)
+ manager.register_attribute(Address, 'address_id', uselist = False)
+ manager.register_attribute(Address, 'email_address', uselist = False)
+
+ u = User()
+ print repr(u.__dict__)
+
+ u.user_id = 7
+ u.user_name = 'john'
+ u.addresses = []
+ a = Address()
+ a.address_id = 10
+ a.email_address = 'lala@123.com'
+ u.addresses.append(a)
+
+ print repr(u.__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'john' and u.addresses[0].email_address == 'lala@123.com')
+ manager.commit(u, a)
+ print repr(u.__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'john' and u.addresses[0].email_address == 'lala@123.com')
+
+ u.user_name = 'heythere'
+ a = Address()
+ a.address_id = 11
+ a.email_address = 'foo@bar.com'
+ u.addresses.append(a)
+ print repr(u.__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'heythere' and u.addresses[0].email_address == 'lala@123.com' and u.addresses[1].email_address == 'foo@bar.com')
+
+ manager.rollback(u, a)
+ print repr(u.__dict__)
+ print repr(u.addresses[0].__dict__)
+ self.assert_(u.user_id == 7 and u.user_name == 'john' and u.addresses[0].email_address == 'lala@123.com')
+ self.assert_(len(u.addresses.unchanged_items()) == 1)
+
+ def testbackref(self):
+ class Student(object):pass
+ class Course(object):pass
+ manager = attributes.AttributeManager()
+ manager.register_attribute(Student, 'courses', uselist=True, extension=attributes.GenericBackrefExtension('students'))
+ manager.register_attribute(Course, 'students', uselist=True, extension=attributes.GenericBackrefExtension('courses'))
+
+ s = Student()
+ c = Course()
+ s.courses.append(c)
+ self.assert_(c.students == [s])
+ s.courses.remove(c)
+ self.assert_(c.students == [])
+
+ (s1, s2, s3) = (Student(), Student(), Student())
+ c.students = [s1, s2, s3]
+ self.assert_(s2.courses == [c])
+ self.assert_(s1.courses == [c])
+ s1.courses.remove(c)
+ self.assert_(c.students == [s2,s3])
+
+
+ class Post(object):pass
+ class Blog(object):pass
+
+ manager.register_attribute(Post, 'blog', uselist=False, extension=attributes.GenericBackrefExtension('posts'))
+ manager.register_attribute(Blog, 'posts', uselist=True, extension=attributes.GenericBackrefExtension('blog'))
+ b = Blog()
+ (p1, p2, p3) = (Post(), Post(), Post())
+ b.posts.append(p1)
+ b.posts.append(p2)
+ b.posts.append(p3)
+ self.assert_(b.posts == [p1, p2, p3])
+ self.assert_(p2.blog is b)
+
+ p3.blog = None
+ self.assert_(b.posts == [p1, p2])
+ p4 = Post()
+ p4.blog = b
+ self.assert_(b.posts == [p1, p2, p4])
+
+
+ class Port(object):pass
+ class Jack(object):pass
+ manager.register_attribute(Port, 'jack', uselist=False, extension=attributes.GenericBackrefExtension('port'))
+ manager.register_attribute(Jack, 'port', uselist=False, extension=attributes.GenericBackrefExtension('jack'))
+ p = Port()
+ j = Jack()
+ p.jack = j
+ self.assert_(j.port is p)
+ self.assert_(p.jack is not None)
+
+ j.port = None
+ self.assert_(p.jack is None)
+
+ def testinheritance(self):
+ """tests that attributes are polymorphic"""
+ class Foo(object):pass
+ class Bar(Foo):pass
+
+ manager = attributes.AttributeManager()
+
+ def func1():
+ return "this is the foo attr"
+ def func2():
+ return "this is the bar attr"
+ def func3():
+ return "this is the shared attr"
+ manager.register_attribute(Foo, 'element', uselist=False, callable_=lambda o:func1)
+ manager.register_attribute(Foo, 'element2', uselist=False, callable_=lambda o:func3)
+ manager.register_attribute(Bar, 'element', uselist=False, callable_=lambda o:func2)
+
+ x = Foo()
+ y = Bar()
+ assert x.element == 'this is the foo attr'
+ assert y.element == 'this is the bar attr'
+ assert x.element2 == 'this is the shared attr'
+ assert y.element2 == 'this is the shared attr'
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/base/dependency.py b/test/base/dependency.py
new file mode 100644
index 000000000..d2b5bd698
--- /dev/null
+++ b/test/base/dependency.py
@@ -0,0 +1,142 @@
+from testbase import PersistTest
+import sqlalchemy.orm.topological as topological
+import unittest, sys, os
+
+
+# TODO: need assertion conditions in this suite
+
+
+class DependencySorter(topological.QueueDependencySorter):pass
+
+class thingy(object):
+ def __init__(self, name):
+ self.name = name
+ def __repr__(self):
+ return "thingy(%d, %s)" % (id(self), self.name)
+ def __str__(self):
+ return repr(self)
+
+class DependencySortTest(PersistTest):
+ def testsort(self):
+ rootnode = thingy('root')
+ node2 = thingy('node2')
+ node3 = thingy('node3')
+ node4 = thingy('node4')
+ subnode1 = thingy('subnode1')
+ subnode2 = thingy('subnode2')
+ subnode3 = thingy('subnode3')
+ subnode4 = thingy('subnode4')
+ subsubnode1 = thingy('subsubnode1')
+ tuples = [
+ (subnode3, subsubnode1),
+ (node2, subnode1),
+ (node2, subnode2),
+ (rootnode, node2),
+ (rootnode, node3),
+ (rootnode, node4),
+ (node4, subnode3),
+ (node4, subnode4)
+ ]
+ head = DependencySorter(tuples, []).sort()
+ print "\n" + str(head)
+
+ def testsort2(self):
+ node1 = thingy('node1')
+ node2 = thingy('node2')
+ node3 = thingy('node3')
+ node4 = thingy('node4')
+ node5 = thingy('node5')
+ node6 = thingy('node6')
+ node7 = thingy('node7')
+ tuples = [
+ (node1, node2),
+ (node3, node4),
+ (node4, node5),
+ (node5, node6),
+ (node6, node2)
+ ]
+ head = DependencySorter(tuples, [node7]).sort()
+ print "\n" + str(head)
+
+ def testsort3(self):
+ ['Mapper|Keyword|keywords,Mapper|IKAssociation|itemkeywords', 'Mapper|Item|items,Mapper|IKAssociation|itemkeywords']
+ node1 = thingy('keywords')
+ node2 = thingy('itemkeyowrds')
+ node3 = thingy('items')
+ tuples = [
+ (node1, node2),
+ (node3, node2),
+ (node1,node3)
+ ]
+ head1 = DependencySorter(tuples, [node1, node2, node3]).sort()
+ head2 = DependencySorter(tuples, [node3, node1, node2]).sort()
+ head3 = DependencySorter(tuples, [node3, node2, node1]).sort()
+
+ # TODO: figure out a "node == node2" function
+ #self.assert_(str(head1) == str(head2) == str(head3))
+ print "\n" + str(head1)
+ print "\n" + str(head2)
+ print "\n" + str(head3)
+
+ def testsort4(self):
+ node1 = thingy('keywords')
+ node2 = thingy('itemkeyowrds')
+ node3 = thingy('items')
+ node4 = thingy('hoho')
+ tuples = [
+ (node1, node2),
+ (node4, node1),
+ (node1, node3),
+ (node3, node2)
+ ]
+ head = DependencySorter(tuples, []).sort()
+ print "\n" + str(head)
+
+ def testsort5(self):
+ # this one, depenending on the weather,
+# thingy(5780972, node4) (idself=5781292, idparent=None)
+# thingy(5780876, node1) (idself=5781068, idparent=5781292)
+# thingy(5780908, node2) (idself=5781164, idparent=5781068)
+# thingy(5780940, node3) (idself=5781228, idparent=5781164)
+
+ node1 = thingy('node1') #thingy('00B94190')
+ node2 = thingy('node2') #thingy('00B94990')
+ node3 = thingy('node3') #thingy('00B9A9B0')
+ node4 = thingy('node4') #thingy('00B4F210')
+ tuples = [
+ (node4, node1),
+ (node1, node2),
+ (node4, node3),
+ (node2, node3),
+ (node4, node2),
+ (node3, node3)
+ ]
+ allitems = [
+ node1,
+ node2,
+ node3,
+ node4
+ ]
+ head = DependencySorter(tuples, allitems).sort()
+ print "\n" + str(head)
+
+ def testcircular(self):
+ node1 = thingy('node1')
+ node2 = thingy('node2')
+ node3 = thingy('node3')
+ node4 = thingy('node4')
+ node5 = thingy('node5')
+ tuples = [
+ (node4, node5),
+ (node5, node4),
+ (node1, node2),
+ (node2, node3),
+ (node3, node1),
+ (node4, node1)
+ ]
+ head = DependencySorter(tuples, []).sort(allow_all_cycles=True)
+ print "\n" + str(head)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/base/historyarray.py b/test/base/historyarray.py
new file mode 100644
index 000000000..9fc270432
--- /dev/null
+++ b/test/base/historyarray.py
@@ -0,0 +1,71 @@
+from testbase import PersistTest
+import sqlalchemy.util as util
+import unittest, sys, os
+
+class HistoryArrayTest(PersistTest):
+ def testadd(self):
+ a = util.HistoryArraySet()
+ a.append('hi')
+ self.assert_(a == ['hi'])
+ self.assert_(a.added_items() == ['hi'])
+
+ def testremove(self):
+ a = util.HistoryArraySet()
+ a.append('hi')
+ a.commit()
+ self.assert_(a == ['hi'])
+ self.assert_(a.added_items() == [])
+ a.remove('hi')
+ self.assert_(a == [])
+ self.assert_(a.deleted_items() == ['hi'])
+
+ def testremoveadded(self):
+ a = util.HistoryArraySet()
+ a.append('hi')
+ a.remove('hi')
+ self.assert_(a.added_items() == [])
+ self.assert_(a.deleted_items() == [])
+ self.assert_(a == [])
+
+ def testaddedremoved(self):
+ a = util.HistoryArraySet()
+ a.append('hi')
+ a.commit()
+ a.remove('hi')
+ self.assert_(a.deleted_items() == ['hi'])
+ a.append('hi')
+ self.assert_(a.added_items() == [])
+ self.assert_(a.deleted_items() == [])
+ self.assert_(a == ['hi'])
+
+ def testrollback(self):
+ a = util.HistoryArraySet()
+ a.append('hi')
+ a.append('there')
+ a.append('yo')
+ a.commit()
+ before = repr(a.data)
+ print repr(a.data)
+ a.remove('there')
+ a.append('lala')
+ a.remove('yo')
+ a.append('yo')
+ after = repr(a.data)
+ print repr(a.data)
+ a.rollback()
+ print repr(a.data)
+ self.assert_(before == repr(a.data))
+
+ def testarray(self):
+ a = util.HistoryArraySet()
+ a.append('hi')
+ a.append('there')
+ self.assert_(a[0] == 'hi' and a[1] == 'there')
+ del a[1]
+ self.assert_(a == ['hi'])
+ a.append('hi')
+ a.append('there')
+ a[3:4] = ['yo', 'hi']
+ self.assert_(a == ['hi', 'there', 'yo'])
+if __name__ == "__main__":
+ unittest.main() \ No newline at end of file