Changeset 2649
- Timestamp:
- 01/10/2007 09:29:12 PM (2 years ago)
- Files:
-
- branches/paul_sandbox/dabo/biz/dBizobj.py (modified) (3 diffs)
- branches/paul_sandbox/dabo/biz/test/test_dBizobj.py (modified) (3 diffs)
- branches/paul_sandbox/dabo/db/dCursorMixin.py (modified) (3 diffs)
- trunk/dabo/biz/test/test_dBizobj.py (modified) (4 diffs)
- trunk/dabo/db/dCursorMixin.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/paul_sandbox/dabo/biz/dBizobj.py
r2643 r2649 257 257 258 258 useTransact = startTransaction or topLevel 259 old_currentCursorKey = self.__currentCursorKey260 259 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 295 290 if old_pk is not None: 296 291 self._moveToPK(old_pk) … … 954 949 # identified until the record is saved and a permanent PK is obtained. 955 950 cursor.genTempAutoPK() 956 cursor.setNewFlag()957 951 # Fill in the link to the parent record 958 952 if self.Parent and self.FillLinkFromParent and self.LinkField: 959 953 self.setParentFK() 960 954 cursor.setDefaults(self.DefaultValues) 955 cursor.setNewFlag() 961 956 962 957 # Call the custom hook method … … 1099 1094 # Let the child know the current dependent PK 1100 1095 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 1104 1100 self.afterChildRequery() 1105 1101 branches/paul_sandbox/dabo/biz/test/test_dBizobj.py
r2643 r2649 201 201 bizChild.DataSource = self.temp_child_table_name 202 202 bizChild.LinkField = "parent_fk" 203 bizChild.FillLinkFromParent = True 203 204 204 205 bizMain.addChild(bizChild) … … 255 256 256 257 # Add a new child record: 258 self.assertEqual(bizChild.IsAdding, False) 257 259 bizChild.new() 260 self.assertEqual(bizChild.IsAdding, True) 258 261 self.assertEqual(bizChild.RowCount, 1) 259 262 self.assertEqual(bizChild.isAnyChanged(), True) … … 262 265 self.assertEqual(bizMain.isChanged(), True) 263 266 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 264 280 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) 265 287 bizMain.requery() 266 288 self.assertEqual(bizMain.RowCount, 3) branches/paul_sandbox/dabo/db/dCursorMixin.py
r2643 r2649 984 984 # Make sure that there is data to save 985 985 if self.RowCount <= 0: 986 raise dException. dException, _("No data to save")986 raise dException.NoRecordsException, _("No data to save") 987 987 # Make sure that there is a PK 988 988 self.checkPK() … … 1031 1031 newrec = self._newRecords.has_key(rec[self.KeyField]) 1032 1032 diff = self.getRecordStatus(row) 1033 1034 1033 if diff or newrec: 1035 1034 if newrec: … … 1963 1962 def _getIsAdding(self): 1964 1963 """ Return True if the current record is a new record.""" 1964 if self.RowCount <= 0: 1965 return False 1965 1966 return self._newRecords.has_key(self._records[self.RowNumber][self.KeyField]) 1966 1967 trunk/dabo/biz/test/test_dBizobj.py
r2640 r2649 23 23 def setUp(self): 24 24 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 26 28 self.createSchema() 27 29 biz.UserSQL = "select * from %s" % self.temp_table_name … … 138 140 ## - End property unit tests - 139 141 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 140 236 141 237 class Test_dBizobj_sqlite(Test_dBizobj, sqlite_unittest): 142 238 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) 145 241 super(Test_dBizobj_sqlite, self).setUp() 146 242 … … 148 244 biz = self.biz 149 245 tableName = self.temp_table_name 246 childTableName = self.temp_child_table_name 150 247 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, )) 248 create table %(tableName)s (pk INTEGER PRIMARY KEY AUTOINCREMENT, cField CHAR, iField INT, nField DECIMAL (8,2)); 249 insert into %(tableName)s (cField, iField, nField) values ("Paul Keith McNett", 23, 23.23); 250 insert into %(tableName)s (cField, iField, nField) values ("Edward Leafe", 42, 42.42); 251 insert into %(tableName)s (cField, iField, nField) values ("Carl Karsten", 10223, 23032.76); 252 253 create table %(childTableName)s (pk INTEGER PRIMARY KEY AUTOINCREMENT, parent_fk INT, cInvNum CHAR); 254 insert into %(childTableName)s (parent_fk, cInvNum) values (1, "IN00023"); 255 insert into %(childTableName)s (parent_fk, cInvNum) values (1, "IN00455"); 256 insert into %(childTableName)s (parent_fk, cInvNum) values (3, "IN00024"); 257 """ % locals()) 156 258 157 259 158 260 class Test_dBizobj_mysql(Test_dBizobj, mysql_unittest): 159 261 def setUp(self): 160 con = dabo.db.dConnection(DbType="MySQL", User="dabo_unittest",262 self.con = dabo.db.dConnection(DbType="MySQL", User="dabo_unittest", 161 263 password="T30T35DB4K30Z45I67N60", Database="dabo_unittest", 162 264 Host="paulmcnett.com") 163 self.biz = dabo.biz.dBizobj( con)265 self.biz = dabo.biz.dBizobj(self.con) 164 266 super(Test_dBizobj_mysql, self).setUp() 165 267 … … 171 273 biz = self.biz 172 274 cur = biz._CurrentCursor 275 tableName = self.temp_table_name 276 childTableName = self.temp_child_table_name 173 277 cur.execute(""" 174 278 create table %s (pk INTEGER PRIMARY KEY AUTO_INCREMENT, cField CHAR (32), iField INT, nField DECIMAL (8,2)) 175 """ % self.temp_table_name)279 """ % tableName) 176 280 cur.execute(""" 177 281 insert into %s (cField, iField, nField) values ("Paul Keith McNett", 23, 23.23) 178 """ % self.temp_table_name)282 """ % tableName) 179 283 cur.execute(""" 180 284 insert into %s (cField, iField, nField) values ("Edward Leafe", 42, 42.42) 181 """ % self.temp_table_name)285 """ % tableName) 182 286 cur.execute(""" 183 287 insert into %s (cField, iField, nField) values ("Carl Karsten", 10223, 23032.76) 184 """ % self.temp_table_name) 288 """ % tableName) 289 290 cur.execute(""" 291 create table %s (pk INTEGER PRIMARY KEY AUTO_INCREMENT, parent_fk INT, cInvNum CHAR (16)) 292 """ % childTableName) 293 cur.execute(""" 294 insert into %s (parent_fk, cInvNum) values (1, "IN00023") 295 """ % childTableName) 296 cur.execute(""" 297 insert into %s (parent_fk, cInvNum) values (1, "IN00455") 298 """ % childTableName) 299 cur.execute(""" 300 insert into %s (parent_fk, cInvNum) values (3, "IN00024") 301 """ % childTableName) 185 302 186 303 trunk/dabo/db/dCursorMixin.py
r2643 r2649 1878 1878 def _getIsAdding(self): 1879 1879 """ Return True if the current record is a new record.""" 1880 if self.RowCount <= 0: 1881 return False 1880 1882 return self._records[self.RowNumber].has_key(kons.CURSOR_NEWFLAG) 1881 1883
