dabo Commit
Revision 2613
Date: 2007-01-06 17:18:59 -0800 (Sat, 06 Jan 2007)
Author: Paul

Changed:
U   branches/paul_sandbox/dabo/db/test/test_dCursorMixin.py

Log:
Added some unit tests for the memento system (still need more, like testing
deletions, additions, and child records). Added MySQL to the backends to
test (I set up a dabo_unittest database on my public MySQL server for this).

Interestingly, one of my tests fails for MySQL, but not the memento tests.
Seems that the automatically-generated DataStructure property returns 
different formats for sqlite than for MySQL, when they should be 
consistent. I'll enter a bug on the Issue Tracker for this. But this shows
that unit testing is already helping us!



Diff:
Modified: branches/paul_sandbox/dabo/db/test/test_dCursorMixin.py
===================================================================
--- branches/paul_sandbox/dabo/db/test/test_dCursorMixin.py     2007-01-06 
21:48:10 UTC (rev 2612)
+++ branches/paul_sandbox/dabo/db/test/test_dCursorMixin.py     2007-01-07 
01:18:59 UTC (rev 2613)
@@ -1,16 +1,21 @@
 import unittest
 import dabo
+from dabo.lib import getRandomUUID
 
 
-
 class Test_dCursorMixin(object):
        def setUp(self):
                cur = self.cur
-               cur.UserSQL = "select * from test"
+               self.temp_table_name = "unittest_%s" % 
getRandomUUID().replace("-", "_")
+               self.createSchema()
+               cur.UserSQL = "select * from %s" % self.temp_table_name
                cur.KeyField = "pk"
-               cur.Table = "test"
+               cur.Table = self.temp_table_name
                cur.requery()
 
+       def tearDown(self):
+               self.cur = None
+
        def createSchema(self):
                """Create the test schema. Override in subclasses."""
                pass
@@ -23,28 +28,84 @@
 
        def testDataStructure(self):
                ds = self.cur.DataStructure
-               self.assertEqual(ds[0], ("pk", "I", True, "test", "pk", None))
-               self.assertEqual(ds[1], ("cField", "C", False, "test", 
"cField", None))
+               self.assertEqual(ds[0], ("pk", "I", True, self.temp_table_name, 
"pk", None))
+               self.assertEqual(ds[1], ("cField", "C", False, 
self.temp_table_name, "cField", None))
 
+       def testMementos(self):
+               cur = self.cur
 
+               # With a new requery, mementos and new records should be empty.
+               self.assertEqual(cur._mementos, {})
+               self.assertEqual(cur._newRecords, {})
+       
+               priorVal = cur.Record.cField
+
+               # Make a change that is the same as the prior value:
+               cur.Record.cField = priorVal
+               self.assertEqual(priorVal, cur.Record.cField)
+               self.assertEqual(cur._mementos, {})
+               self.assertEqual(cur._newRecords, {})
+
+               # Make a change that is different:
+               cur.Record.cField = "New test value"
+               self.assertEqual(cur._mementos, {cur.Record.pk: {"cField": 
priorVal}})
+               self.assertEqual(cur.isChanged(), True)
+
+               # Change it back:
+               cur.Record.cField = priorVal
+               self.assertEqual(cur._mementos, {})
+               self.assertEqual(cur.isChanged(), False)
+
+               # Make a change that is different and cancel:
+               cur.Record.cField = "New test value"
+               cur.cancel()
+               self.assertEqual(cur._mementos, {})
+
+
 class Test_dCursorMixin_sqlite(Test_dCursorMixin, unittest.TestCase):
        def setUp(self):
                con = dabo.db.dConnection(DbType="SQLite", Database=":memory:")
                self.cur = con.getDaboCursor()
-               self.createSchema()
                super(Test_dCursorMixin_sqlite, self).setUp()
 
        def createSchema(self):
-               """Creates the test database schema for the tests."""
                cur = self.cur
+               tableName = self.temp_table_name
                cur.executescript("""
-create table test (pk INTEGER PRIMARY KEY AUTOINCREMENT, cField CHAR, iField 
INT, nField DECIMAL (8,2));
-insert into test (cField, iField, nField) values ("Paul Keith McNett", 23, 
23.23);
-insert into test (cField, iField, nField) values ("Edward Leafe", 42, 42.42);
-insert into test (cField, iField, nField) values ("Carl Karsten", 10223, 
23032.76);""")
+create table %s (pk INTEGER PRIMARY KEY AUTOINCREMENT, cField CHAR, iField 
INT, nField DECIMAL (8,2));
+insert into %s (cField, iField, nField) values ("Paul Keith McNett", 23, 
23.23);
+insert into %s (cField, iField, nField) values ("Edward Leafe", 42, 42.42);
+insert into %s (cField, iField, nField) values ("Carl Karsten", 10223, 
23032.76);
+""" % (tableName, tableName, tableName, tableName, ))
 
+
+class Test_dCursorMixin_mysql(Test_dCursorMixin, unittest.TestCase):
+       def setUp(self):
+               con = dabo.db.dConnection(DbType="MySQL", User="dabo_unittest", 
+                               Password="T30T35DB4K30Z45I67N60", 
Database="dabo_unittest",
+                               Host="paulmcnett.com")
+               self.cur = con.getDaboCursor()
+               super(Test_dCursorMixin_mysql, self).setUp()
+
        def tearDown(self):
-               self.cur = None
+               self.cur.execute("drop table %s" % self.temp_table_name)
+               super(Test_dCursorMixin_mysql, self).tearDown()
 
+       def createSchema(self):
+               cur = self.cur
+               cur.execute("""
+create table %s (pk INTEGER PRIMARY KEY AUTO_INCREMENT, cField CHAR (32), 
iField INT, nField DECIMAL (8,2))
+""" % self.temp_table_name)
+               cur.execute("""         
+insert into %s (cField, iField, nField) values ("Paul Keith McNett", 23, 23.23)
+""" % self.temp_table_name)
+               cur.execute("""         
+insert into %s (cField, iField, nField) values ("Edward Leafe", 42, 42.42)
+""" % self.temp_table_name)
+               cur.execute("""         
+insert into %s (cField, iField, nField) values ("Carl Karsten", 10223, 
23032.76)
+""" % self.temp_table_name)
+
+
 if __name__ == "__main__":
        unittest.main()




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev

Reply via email to