def atomic_create_record(self, fields): """Create a record and set field contents atomically.""" if not fields: raise DataError("Cannot create an empty record") l = len(fields) tupletype = type(()) if self.locking: self.start_write() try: r = wgdb.create_raw_record(self._db, l) for i in range(l): if type(fields[i]) == tupletype: data = fields[i][0] extarg = fields[i][1:] else: data = fields[i] extarg = () if isinstance(data, Record): data = data.get__rec() fargs = (self._db, r, i, data) + extarg wgdb.set_new_field(*fargs) finally: if self.locking: self.end_write() return self._new_record(r)
def test_creation(self): """Tests record creation and low level scanning to retrieve records from the database.""" rec = wgdb.create_record(self.d, 3) self.assertTrue(wgdb.is_record(rec)) l = wgdb.get_record_len(self.d, rec) self.assertEqual(l, 3) rec2 = wgdb.create_raw_record(self.d, 678) self.assertTrue(wgdb.is_record(rec2)) l = wgdb.get_record_len(self.d, rec2) self.assertEqual(l, 678) # wgdb module only allows comparing records by contents # so we need to use recognizable data for this test. wgdb.set_field(self.d, rec, 0, 99531179) wgdb.set_field(self.d, rec2, 0, 55498756) # XXX: the following relies on certain assumptions on memory # management of WhiteDB. By the API description, the records # are not necessarily fetched in order of creation, it is just # useful for the current test case that it happens to be the case. # cand = wgdb.get_first_record(self.d) self.assertEqual(wgdb.get_field(self.d, cand, 0), 99531179) cand = wgdb.get_next_record(self.d, cand) self.assertEqual(wgdb.get_field(self.d, cand, 0), 55498756) # This, however, should always work correctly wgdb.delete_record(self.d, rec) cand = wgdb.get_first_record(self.d) self.assertEqual(wgdb.get_field(self.d, cand, 0), 55498756)