Changeset 2649

Show
Ignore:
Timestamp:
01/10/2007 09:29:12 PM (2 years ago)
Author:
paul
Message:

Added testChildren unit test to trunk bizobj.

Fixed IsAdding? to return False if there aren't any records.

Fixed the saveAll() and save() routines in my branch to actually save the
child records, including new ones. For the first time, my branch works with
all my applications, so after some final testing tomorrow I'll probably do a
big honking merge into trunk.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/paul_sandbox/dabo/biz/dBizobj.py

    r2643 r2649  
    257257 
    258258        useTransact = startTransaction or topLevel 
    259         old_currentCursorKey = self.__currentCursorKey 
    260259        old_pk = getattr(self.Record, self.KeyField, None) 
    261  
    262         for key, cursor in self.__cursors.iteritems(): 
    263             if useTransact: 
    264                 # Tell the cursor to begin a transaction, if needed. 
    265                 cursor.beginTransaction() 
    266             self._CurrentCursor = key 
    267             changed_keys = list(set(cursor._mementos.keys() + cursor._newRecords.keys())) 
    268             for pk in changed_keys: 
    269                 self._moveToPK(pk) 
    270                 try: 
    271                     self.save(startTransaction=False, topLevel=False) 
    272                 except dException.ConnectionLostException, e: 
    273                     self._CurrentCursor = old_currentCursorKey 
    274                     self._moveToPK(old_pk) 
    275                     raise dException.ConnectionLostException, e 
    276                 except dException.DBQueryException, e: 
    277                     # Something failed; reset things. 
    278                     if useTransact: 
    279                         cursor.rollbackTransaction() 
    280                     # Pass the exception to the UI 
    281                     self._CurrentCursor = old_currentCursorKey 
    282                     self._moveToPK(old_pk) 
    283                     raise dException.DBQueryException, e 
    284                 except dException.dException, e: 
    285                     if useTransact: 
    286                         cursor.rollbackTransaction() 
    287                     self._CurrentCursor = old_currentCursorKey 
    288                     self._moveToPK(old_pk) 
    289                     raise 
    290  
    291             if useTransact: 
    292                 cursor.commitTransaction() 
    293  
    294         self._CurrentCursor = old_currentCursorKey 
     260        cursor = self._CurrentCursor 
     261 
     262        if useTransact: 
     263            # Tell the cursor to begin a transaction, if needed. 
     264            cursor.beginTransaction() 
     265         
     266        changed_rows = self.getChangedRows() 
     267        for row in changed_rows: 
     268            self._moveToRowNum(row) 
     269            try: 
     270                self.save(startTransaction=False, topLevel=False) 
     271            except dException.ConnectionLostException, e: 
     272                self._moveToPK(old_pk) 
     273                raise dException.ConnectionLostException, e 
     274            except dException.DBQueryException, e: 
     275                # Something failed; reset things. 
     276                if useTransact: 
     277                    cursor.rollbackTransaction() 
     278                # Pass the exception to the UI 
     279                self._moveToPK(old_pk) 
     280                raise dException.DBQueryException, e 
     281            except dException.dException, e: 
     282                if useTransact: 
     283                    cursor.rollbackTransaction() 
     284                self._moveToPK(old_pk) 
     285                raise 
     286 
     287        if useTransact: 
     288            cursor.commitTransaction() 
     289 
    295290        if old_pk is not None: 
    296291            self._moveToPK(old_pk) 
     
    954949            # identified until the record is saved and a permanent PK is obtained. 
    955950            cursor.genTempAutoPK() 
    956         cursor.setNewFlag() 
    957951        # Fill in the link to the parent record 
    958952        if self.Parent and self.FillLinkFromParent and self.LinkField: 
    959953            self.setParentFK() 
    960954        cursor.setDefaults(self.DefaultValues) 
     955        cursor.setNewFlag() 
    961956 
    962957        # Call the custom hook method 
     
    10991094            # Let the child know the current dependent PK 
    11001095            child.setCurrentParent(pk) 
    1101             if not child.isChanged() and child.RequeryWithParent: 
    1102                 child.requery() 
    1103  
     1096            if child.RequeryWithParent: 
     1097                if not child.isChanged(): 
     1098                    child.requery() 
     1099     
    11041100        self.afterChildRequery() 
    11051101 
  • branches/paul_sandbox/dabo/biz/test/test_dBizobj.py

    r2643 r2649  
    201201        bizChild.DataSource = self.temp_child_table_name 
    202202        bizChild.LinkField = "parent_fk" 
     203        bizChild.FillLinkFromParent = True 
    203204         
    204205        bizMain.addChild(bizChild) 
     
    255256 
    256257        # Add a new child record: 
     258        self.assertEqual(bizChild.IsAdding, False) 
    257259        bizChild.new() 
     260        self.assertEqual(bizChild.IsAdding, True) 
    258261        self.assertEqual(bizChild.RowCount, 1) 
    259262        self.assertEqual(bizChild.isAnyChanged(), True) 
     
    262265        self.assertEqual(bizMain.isChanged(), True) 
    263266        bizChild.Record.cInvNum = "IN99991" 
     267 
     268        bizMain.prior() 
     269        self.assertEqual(bizChild.IsAdding, False) 
     270        bizMain.next() 
     271        self.assertEqual(bizChild.IsAdding, True) 
     272 
     273        self.assertEqual(bizChild.RowCount, 1) 
     274        self.assertEqual(bizChild.isAnyChanged(), True) 
     275        self.assertEqual(bizChild.isChanged(), True) 
     276        self.assertEqual(bizMain.isAnyChanged(), True) 
     277        self.assertEqual(bizMain.isChanged(), True) 
     278        self.assertEqual(bizChild.Record.cInvNum, "IN99991") 
     279 
    264280        bizMain.saveAll() 
     281 
     282        self.assertEqual(bizMain.RowCount, 3) 
     283        self.assertEqual(bizMain.RowNumber, 2) 
     284        self.assertEqual(bizChild.RowCount, 1) 
     285        self.assertEqual(bizChild.Record.cInvNum, "IN99991") 
     286        self.assertEqual(bizChild.IsAdding, False) 
    265287        bizMain.requery() 
    266288        self.assertEqual(bizMain.RowCount, 3) 
  • branches/paul_sandbox/dabo/db/dCursorMixin.py

    r2643 r2649  
    984984        # Make sure that there is data to save 
    985985        if self.RowCount <= 0: 
    986             raise dException.dException, _("No data to save") 
     986            raise dException.NoRecordsException, _("No data to save") 
    987987        # Make sure that there is a PK 
    988988        self.checkPK() 
     
    10311031        newrec = self._newRecords.has_key(rec[self.KeyField]) 
    10321032        diff = self.getRecordStatus(row) 
    1033  
    10341033        if diff or newrec: 
    10351034            if newrec: 
     
    19631962    def _getIsAdding(self): 
    19641963        """ Return True if the current record is a new record.""" 
     1964        if self.RowCount <= 0: 
     1965            return False 
    19651966        return self._newRecords.has_key(self._records[self.RowNumber][self.KeyField]) 
    19661967     
  • trunk/dabo/biz/test/test_dBizobj.py

    r2640 r2649  
    2323    def setUp(self): 
    2424        biz = self.biz 
    25         self.temp_table_name = "unittest%s" % getRandomUUID().replace("-", "")[-20:] 
     25        uniqueName = getRandomUUID().replace("-", "")[-20:] 
     26        self.temp_table_name = "unittest%s" % uniqueName 
     27        self.temp_child_table_name = "ut_child%s" % uniqueName 
    2628        self.createSchema() 
    2729        biz.UserSQL = "select * from %s" % self.temp_table_name 
     
    138140    ## - End property unit tests - 
    139141 
     142    def testChildren(self): 
     143        bizMain = self.biz 
     144        bizChild = dabo.biz.dBizobj(self.con) 
     145        bizChild.UserSQL = "select * from %s" % self.temp_child_table_name 
     146        bizChild.KeyField = "pk" 
     147        bizChild.DataSource = self.temp_child_table_name 
     148        bizChild.LinkField = "parent_fk" 
     149         
     150        bizMain.addChild(bizChild) 
     151        bizMain.requery() 
     152 
     153        # At this point bizMain should be at row 0, and bizChild should have 
     154        # two records, and be on row 0 
     155        self.assertEqual(bizMain.Record.pk, 1) 
     156        self.assertEqual(bizMain.RowNumber, 0) 
     157        self.assertEqual(bizChild.RowCount, 2) 
     158        self.assertEqual(bizChild.RowNumber, 0) 
     159        self.assertEqual(bizChild.Record.pk, 1) 
     160        self.assertEqual(bizChild.Record.parent_fk, 1) 
     161 
     162        bizMain.next() 
     163         
     164        # At this point bizMain should be at row 1, and bizChild should have 
     165        # zero records. 
     166        self.assertEqual(bizMain.Record.pk, 2) 
     167        self.assertEqual(bizMain.RowNumber, 1) 
     168        self.assertEqual(bizChild.RowCount, 0) 
     169        self.assertEqual(bizChild.RowNumber, 0) 
     170         
     171        # Trying to get the field value from the nonexistent record should raise 
     172        # dException.NoRecordsException: 
     173        def testGetField(): 
     174            return bizChild.Record.pk 
     175        def testSetField(): 
     176            bizChild.Record.pk = 23 
     177        self.assertRaises(dabo.dException.NoRecordsException, testGetField) 
     178        self.assertRaises(dabo.dException.NoRecordsException, testSetField) 
     179 
     180        bizMain.next() 
     181 
     182        # At this point bizMain should be at row 2, and bizChild should have 
     183        # one record, and be on row 0. 
     184        self.assertEqual(bizMain.Record.pk, 3) 
     185        self.assertEqual(bizMain.RowNumber, 2) 
     186        self.assertEqual(bizChild.RowCount, 1) 
     187        self.assertEqual(bizChild.RowNumber, 0) 
     188        self.assertEqual(bizChild.Record.pk, 3) 
     189        self.assertEqual(bizChild.Record.parent_fk, 3) 
     190         
     191        # Try a delete, which takes effect immediately without need to save: 
     192        bizChild.delete() 
     193        self.assertEqual(bizMain.RowNumber, 2) 
     194        self.assertEqual(bizChild.RowCount, 0) 
     195        self.assertEqual(bizChild.isAnyChanged(), False) 
     196        self.assertEqual(bizMain.isAnyChanged(), False) 
     197        bizMain.prior() 
     198        self.assertEqual(bizChild.RowCount, 0) 
     199        bizMain.next() 
     200        self.assertEqual(bizChild.RowCount, 0) 
     201 
     202        # Add a new child record: 
     203        self.assertEqual(bizChild.IsAdding, False) 
     204        bizChild.new() 
     205        self.assertEqual(bizChild.IsAdding, True) 
     206        self.assertEqual(bizChild.RowCount, 1) 
     207        self.assertEqual(bizChild.isAnyChanged(), True) 
     208        self.assertEqual(bizChild.isChanged(), True) 
     209        self.assertEqual(bizMain.isAnyChanged(), True) 
     210        self.assertEqual(bizMain.isChanged(), True) 
     211        bizChild.Record.cInvNum = "IN99991" 
     212 
     213        bizMain.prior() 
     214        self.assertEqual(bizChild.IsAdding, False) 
     215        bizMain.next() 
     216        self.assertEqual(bizChild.IsAdding, True) 
     217 
     218        self.assertEqual(bizChild.RowCount, 1) 
     219        self.assertEqual(bizChild.isAnyChanged(), True) 
     220        self.assertEqual(bizChild.isChanged(), True) 
     221        self.assertEqual(bizMain.isAnyChanged(), True) 
     222        self.assertEqual(bizMain.isChanged(), True) 
     223        self.assertEqual(bizChild.Record.cInvNum, "IN99991") 
     224 
     225        bizMain.saveAll() 
     226        bizMain.requery() 
     227        self.assertEqual(bizMain.RowCount, 3) 
     228        self.assertEqual(bizMain.RowNumber, 2) 
     229        self.assertEqual(bizChild.RowCount, 1) 
     230        self.assertEqual(bizChild.Record.cInvNum, "IN99991") 
     231        bizMain.prior() 
     232        bizMain.next() 
     233        self.assertEqual(bizChild.RowCount, 1) 
     234        self.assertEqual(bizChild.Record.cInvNum, "IN99991") 
     235 
    140236 
    141237class Test_dBizobj_sqlite(Test_dBizobj, sqlite_unittest): 
    142238    def setUp(self): 
    143         con = dabo.db.dConnection(DbType="SQLite", Database=":memory:") 
    144         self.biz = dabo.biz.dBizobj(con) 
     239        self.con = dabo.db.dConnection(DbType="SQLite", Database=":memory:") 
     240        self.biz = dabo.biz.dBizobj(self.con) 
    145241        super(Test_dBizobj_sqlite, self).setUp() 
    146242 
     
    148244        biz = self.biz 
    149245        tableName = self.temp_table_name 
     246        childTableName = self.temp_child_table_name 
    150247        biz._CurrentCursor.executescript(""" 
    151 create table %s (pk INTEGER PRIMARY KEY AUTOINCREMENT, cField CHAR, iField INT, nField DECIMAL (8,2)); 
    152 insert into %s (cField, iField, nField) values ("Paul Keith McNett", 23, 23.23); 
    153 insert into %s (cField, iField, nField) values ("Edward Leafe", 42, 42.42); 
    154 insert into %s (cField, iField, nField) values ("Carl Karsten", 10223, 23032.76); 
    155 """ % (tableName, tableName, tableName, tableName, )) 
     248create table %(tableName)s (pk INTEGER PRIMARY KEY AUTOINCREMENT, cField CHAR, iField INT, nField DECIMAL (8,2)); 
     249insert into %(tableName)s (cField, iField, nField) values ("Paul Keith McNett", 23, 23.23); 
     250insert into %(tableName)s (cField, iField, nField) values ("Edward Leafe", 42, 42.42); 
     251insert into %(tableName)s (cField, iField, nField) values ("Carl Karsten", 10223, 23032.76); 
     252 
     253create table %(childTableName)s (pk INTEGER PRIMARY KEY AUTOINCREMENT, parent_fk INT, cInvNum CHAR); 
     254insert into %(childTableName)s (parent_fk, cInvNum) values (1, "IN00023"); 
     255insert into %(childTableName)s (parent_fk, cInvNum) values (1, "IN00455"); 
     256insert into %(childTableName)s (parent_fk, cInvNum) values (3, "IN00024"); 
     257""" % locals()) 
    156258 
    157259 
    158260class Test_dBizobj_mysql(Test_dBizobj, mysql_unittest): 
    159261    def setUp(self): 
    160         con = dabo.db.dConnection(DbType="MySQL", User="dabo_unittest",  
     262        self.con = dabo.db.dConnection(DbType="MySQL", User="dabo_unittest",  
    161263                password="T30T35DB4K30Z45I67N60", Database="dabo_unittest", 
    162264                Host="paulmcnett.com") 
    163         self.biz = dabo.biz.dBizobj(con) 
     265        self.biz = dabo.biz.dBizobj(self.con) 
    164266        super(Test_dBizobj_mysql, self).setUp() 
    165267 
     
    171273        biz = self.biz 
    172274        cur = biz._CurrentCursor 
     275        tableName = self.temp_table_name 
     276        childTableName = self.temp_child_table_name 
    173277        cur.execute(""" 
    174278create table %s (pk INTEGER PRIMARY KEY AUTO_INCREMENT, cField CHAR (32), iField INT, nField DECIMAL (8,2)) 
    175 """ % self.temp_table_name) 
     279""" % tableName) 
    176280        cur.execute("""      
    177281insert into %s (cField, iField, nField) values ("Paul Keith McNett", 23, 23.23) 
    178 """ % self.temp_table_name) 
     282""" % tableName) 
    179283        cur.execute("""      
    180284insert into %s (cField, iField, nField) values ("Edward Leafe", 42, 42.42) 
    181 """ % self.temp_table_name) 
     285""" % tableName) 
    182286        cur.execute("""      
    183287insert into %s (cField, iField, nField) values ("Carl Karsten", 10223, 23032.76) 
    184 """ % self.temp_table_name) 
     288""" % tableName) 
     289 
     290        cur.execute(""" 
     291create table %s (pk INTEGER PRIMARY KEY AUTO_INCREMENT, parent_fk INT, cInvNum CHAR (16)) 
     292""" % childTableName) 
     293        cur.execute(""" 
     294insert into %s (parent_fk, cInvNum) values (1, "IN00023") 
     295""" % childTableName) 
     296        cur.execute(""" 
     297insert into %s (parent_fk, cInvNum) values (1, "IN00455") 
     298""" % childTableName) 
     299        cur.execute(""" 
     300insert into %s (parent_fk, cInvNum) values (3, "IN00024") 
     301""" % childTableName) 
    185302 
    186303 
  • trunk/dabo/db/dCursorMixin.py

    r2643 r2649  
    18781878    def _getIsAdding(self): 
    18791879        """ Return True if the current record is a new record.""" 
     1880        if self.RowCount <= 0: 
     1881            return False 
    18801882        return self._records[self.RowNumber].has_key(kons.CURSOR_NEWFLAG) 
    18811883