def testupdate(self): a = stupid_dict(self.sampleA) c = stupid_dict(self.sampleC) a.update(c) self.assert_( a["A"] == 1) self.assert_( a["B"] == "b")
def testupdate(self): a = stupid_dict(self.sampleA) c = stupid_dict(self.sampleC) a.update(c) self.assert_(a["A"] == 1) self.assert_(a["B"] == "b")
def test__eq__(self): d1 = stupid_dict(self.sampleC) d2 = stupid_dict(( ( "B", "b", ), ( "D", 4, ), )) self.assertEqual(d1, d2)
def testvalues(self): d = stupid_dict(self.sampleB) self.assertEqual(d.values(), [ "A", "B", "C", ])
def testkeys(self): d = stupid_dict(self.sampleB) self.assert_(d.keys() == [ 1, 2, 3, ])
def select_for_update(self, dbclass, key): """ This method works like L{select_by_primary_key} above, except that it doesn't select anything but returns a dummy object (an empty dbobj) that will allow setting attributes, yielding proper UPDATE statements. Note that supplying a primary key that does not exist will go unnoticed: The UPDATE statements won't create an error, they just won't affect any rows. This method is primarily ment for transaction based (i.e. www) applications. """ if type(key) != TupleType: key = (key, ) primary_key = keys.primary_key(dbclass) if len(key) != len(primary_key.key_attributes): msg = "The primary key for %s must have %i elements." % \ ( repr(dbclass), len(primary_key.key_attributes), ) raise IllegalPrimaryKey(msg) info = stupid_dict() for property, value in zip(primary_key.attributes(), key): info[property.column] = value return dbclass.__from_result__(self, info)
def select_for_update(self, dbclass, key): """ This method works like L{select_by_primary_key} above, except that it doesn't select anything but returns a dummy object (an empty dbobj) that will allow setting attributes, yielding proper UPDATE statements. Note that supplying a primary key that does not exist will go unnoticed: The UPDATE statements won't create an error, they just won't affect any rows. This method is primarily ment for transaction based (i.e. www) applications. """ if type(key) != TupleType: key = ( key, ) primary_key = keys.primary_key(dbclass) if len(key) != len(primary_key.key_attributes): msg = "The primary key for %s must have %i elements." % \ ( repr(dbclass), len(primary_key.key_attributes), ) raise IllegalPrimaryKey(msg) info = stupid_dict() for property, value in zip(primary_key.attributes(), key): info[property.column] = value return dbclass.__from_result__(self, info)
def update( self, relation, column, sql_literal, where, ): """ Updates are stored in a list and executed on calls to commit() or to flush_updates() to join updates to the same row into a single SQL command. @param relation: The relation to be updated @param column: sql.column Name of the column to be updated @param sql_literal: sql literal of the value to be stored. @param where: where clause that would select (or update in this case) the desired row from relation """ key = ( relation, where, ) data = self._updates.get(key, stupid_dict()) data[column] = sql_literal self._updates[key] = data
def rollback(self): """ Undo the changes you made to the database since the last commit() """ self._updates = stupid_dict() db = getattr(self._conn, "db", None) if db is not None: db.rollback()
def test__iter__(self): d = stupid_dict(self.sampleC) l = [] for key in d: l.append(key) self.assertEqual(l, ["B", "D"])
def test__delitem__(self): d = stupid_dict(self.sampleC) del d["B"] self.assertEqual(d.data, [("D", 4,)]) self.assertRaises(KeyError, d.__delitem__, "X")
def test__setitem__(self): d = stupid_dict(self.sampleA) d["D"] = 4 self.assert_(( "D", 4, ) in d.data)
def test__getitem__(self): for sample in self.samples: a = stupid_dict(sample) if type(sample) == DictType: items = sample.items() else: items = sample for key, value in items: self.assertEqual(a[key], value)
def test__delitem__(self): d = stupid_dict(self.sampleC) del d["B"] self.assertEqual(d.data, [( "D", 4, )]) self.assertRaises(KeyError, d.__delitem__, "X")
def test__init__(self): self.assertRaises(ValueError, stupid_dict, ( ( 1, 2, ), 1, 2, )) self.assertRaises(ValueError, stupid_dict, "kacke") d = stupid_dict({"a": 1, "b": 2})
def next(self): if hasattr(self, "rows"): if len(self.rows) == 0: tpl = None else: tpl = self.rows.pop() else: tpl = self.cursor.fetchone() if tpl is None: raise StopIteration else: info = stupid_dict(zip(self.columns, tpl)) return self.dbclass.__from_result__(self.ds, info)
def __select_columns__(cls): """ A list of columns to select from the elation to construct one of these. """ # The use of the stupid_dict class has become neccessary, because # sql._part instances are not hashable. columns = stupid_dict() for property in cls.__dbproperties__(): if property.__select_this_column__(): columns[property.column] = 0 columns = list(columns.keys()) return columns
def update(self, relation, column, sql_literal, where, ): """ Updates are stored in a list and executed on calls to commit() or to flush_updates() to join updates to the same row into a single SQL command. @param relation: The relation to be updated @param column: sql.column Name of the column to be updated @param sql_literal: sql literal of the value to be stored. @param where: where clause that would select (or update in this case) the desired row from relation """ key = ( relation, where, ) data = self._updates.get(key, stupid_dict()) data[column] = sql_literal self._updates[key] = data
def testitems(self): d = stupid_dict(self.sampleB) self.assertEqual(d.items(), list(self.sampleB))
def testhas_key(self): d = stupid_dict(self.sampleB) self.assert_(d.has_key(1)) self.assert_(not d.has_key(0))
def test__contains__(self): d = stupid_dict(self.sampleC) self.assert_("B" in d) self.assert_(not "X" in d)
def test__setitem__(self): d = stupid_dict(self.sampleA) d["D"] = 4 self.assert_( ("D", 4,) in d.data )
def testclear(self): d = stupid_dict(self.sampleB) d.clear() self.assertEqual(len(d), 0)
def test__eq__(self): d1 = stupid_dict(self.sampleC) d2 = stupid_dict((("B", "b",), ("D", 4,),)) self.assertEqual(d1, d2)
def testcopy(self): d = stupid_dict(self.sampleB) e = d.copy() self.assert_(id(d) != id(e) and d == e)
def testsetdefault(self): d = stupid_dict(self.sampleB) d.setdefault("X") self.assert_(d[0] == "X") self.assert_(d[1] == "A")
def test__init__(self): self.assertRaises(ValueError, stupid_dict, ( (1, 2,), 1, 2, )) self.assertRaises(ValueError, stupid_dict, "kacke") d = stupid_dict({"a": 1, "b": 2})
def test__len__(self): for a in self.samples: d = stupid_dict(a) self.assertEqual(len(d), len(a))
def test__repr__(self): d = stupid_dict(self.sampleB) self.assertEqual(repr(d), "stupid_dict(%s)" % repr(list(self.sampleB)))
def testget(self): d = stupid_dict(self.sampleB) self.assertEqual(d.get(1), "A") self.assertEqual(d.get(0, "X"), "X") self.assertEqual(d.get(0), None)
def __init__(self): self._conn = None self._updates = stupid_dict() self._update_cursor = None self._debug = 0