def test_setdefault(self): data, wim = self._fixture() o1 = self.Data() oid1 = id(o1) assert o1 not in wim res1 = wim.setdefault(o1, 123) assert o1 in wim assert oid1 in wim.by_id eq_(res1, 123) id_keys = set(wim.by_id.keys()) res2 = wim.setdefault(o1, 456) assert o1 in wim assert oid1 in wim.by_id eq_(res2, 123) assert set(wim.by_id.keys()) == id_keys del wim[o1] assert o1 not in wim assert oid1 not in wim.by_id ne_(set(wim.by_id.keys()), id_keys) res3 = wim.setdefault(o1, 789) assert o1 in wim assert oid1 in wim.by_id eq_(res3, 789) eq_(set(wim.by_id.keys()), id_keys)
def register(self, cls, canary): original_init = cls.__init__ attributes.register_class(cls) ne_(cls.__init__, original_init) manager = attributes.manager_of_class(cls) def on_init(state, instance, args, kwargs): canary.append((cls, 'on_init', type(instance))) manager.events.add_listener('on_init', on_init)
def test_pop_default(self): data, wim = self._fixture() needle = data[-1] value = wim[needle] x = wim.pop(needle, 123) ne_(x, 123) eq_(x, value) assert needle not in wim assert id(needle) not in wim.by_id eq_(len(data), (len(wim) + 1)) n2 = self.Data() y = wim.pop(n2, 456) eq_(y, 456) assert n2 not in wim assert id(n2) not in wim.by_id eq_(len(data), (len(wim) + 1))
def test_nativeext_interfaceexact(self): class A(object): __sa_instrumentation_manager__ = sa.orm.interfaces.InstrumentationManager attributes.register_class(A) ne_(type(attributes.manager_of_class(A)), attributes.ClassManager)
def test_basic_sanity(self): IdentitySet = util.IdentitySet o1, o2, o3 = object(), object(), object() ids = IdentitySet([o1]) ids.discard(o1) ids.discard(o1) ids.add(o1) ids.remove(o1) self.assertRaises(KeyError, ids.remove, o1) eq_(ids.copy(), ids) # explicit __eq__ and __ne__ tests assert ids != None assert not(ids == None) ne_(ids, IdentitySet([o1,o2,o3])) ids.clear() assert o1 not in ids ids.add(o2) assert o2 in ids eq_(ids.pop(), o2) ids.add(o1) eq_(len(ids), 1) isuper = IdentitySet([o1,o2]) assert ids < isuper assert ids.issubset(isuper) assert isuper.issuperset(ids) assert isuper > ids eq_(ids.union(isuper), isuper) eq_(ids | isuper, isuper) eq_(isuper - ids, IdentitySet([o2])) eq_(isuper.difference(ids), IdentitySet([o2])) eq_(ids.intersection(isuper), IdentitySet([o1])) eq_(ids & isuper, IdentitySet([o1])) eq_(ids.symmetric_difference(isuper), IdentitySet([o2])) eq_(ids ^ isuper, IdentitySet([o2])) ids.update(isuper) ids |= isuper ids.difference_update(isuper) ids -= isuper ids.intersection_update(isuper) ids &= isuper ids.symmetric_difference_update(isuper) ids ^= isuper ids.update('foobar') try: ids |= 'foobar' assert False except TypeError: assert True try: s = set([o1,o2]) s |= ids assert False except TypeError: assert True self.assertRaises(TypeError, cmp, ids) self.assertRaises(TypeError, hash, ids)
def test_basic_sanity(self): IdentitySet = util.IdentitySet o1, o2, o3 = object(), object(), object() ids = IdentitySet([o1]) ids.discard(o1) ids.discard(o1) ids.add(o1) ids.remove(o1) self.assertRaises(KeyError, ids.remove, o1) eq_(ids.copy(), ids) # explicit __eq__ and __ne__ tests assert ids != None assert not (ids == None) ne_(ids, IdentitySet([o1, o2, o3])) ids.clear() assert o1 not in ids ids.add(o2) assert o2 in ids eq_(ids.pop(), o2) ids.add(o1) eq_(len(ids), 1) isuper = IdentitySet([o1, o2]) assert ids < isuper assert ids.issubset(isuper) assert isuper.issuperset(ids) assert isuper > ids eq_(ids.union(isuper), isuper) eq_(ids | isuper, isuper) eq_(isuper - ids, IdentitySet([o2])) eq_(isuper.difference(ids), IdentitySet([o2])) eq_(ids.intersection(isuper), IdentitySet([o1])) eq_(ids & isuper, IdentitySet([o1])) eq_(ids.symmetric_difference(isuper), IdentitySet([o2])) eq_(ids ^ isuper, IdentitySet([o2])) ids.update(isuper) ids |= isuper ids.difference_update(isuper) ids -= isuper ids.intersection_update(isuper) ids &= isuper ids.symmetric_difference_update(isuper) ids ^= isuper ids.update('foobar') try: ids |= 'foobar' assert False except TypeError: assert True try: s = set([o1, o2]) s |= ids assert False except TypeError: assert True self.assertRaises(TypeError, cmp, ids) self.assertRaises(TypeError, hash, ids)