def test_missing(self): with mock_db_trans() as conn: user = self._create_user('*****@*****.**', conn) # Cannot find with invalid intid ref = wref.WeakRef(user) setattr(ref, '_v_entity_cache', None) setattr(ref, '_entity_id', -1) assert_that(ref(), is_(none())) assert_that(ref.make_missing_ntiid(), is_('tag:nextthought.com,2011-10:Missing')) # cannot find with invalid oid ref = wref.WeakRef(user) assert_that(ref, has_property('_entity_oid', not_none())) setattr(ref, '_v_entity_cache', None) setattr(ref, '_entity_oid', -1) assert_that(ref(), is_(none())) # Find it with oid of None (but valid intid) ref = wref.WeakRef(user) assert_that(ref, has_property('_entity_oid', not_none())) setattr(ref, '_v_entity_cache', None) setattr(ref, '_entity_oid', None) assert_that(ref(), is_(user)) # Caching can be controlled ref = wref.WeakRef(user) setattr(ref, '_entity_id', -1) setattr(ref, '_entity_oid', None) assert_that(ref(), is_(user)) # From cache assert_that(ref(allow_cached=False), is_(none())) # not from cache
def test_add_remove(self): with mock_db_trans() as conn: intids = component.getUtility(IIntIds) user = User('*****@*****.**') conn.add(user) addIntId(user) assert_that(intids.queryId(user), is_not(none())) removeIntId(user) assert_that(intids.queryId(user), is_(none()))
def test_no_dict(self): with mock_db_trans() as conn: user_1 = self._create_user('*****@*****.**', conn) for clazz in (wref.NoCachingArbitraryOrderableWeakRef, wref.ArbitraryOrderableWeakRef, wref.WeakRef): __traceback_info__ = clazz ref = clazz(user_1) self.assertFalse(hasattr(ref, '__dict__')) with self.assertRaises(AttributeError): setattr(ref, 'arbitrary_attribute', None)
def test_arbitrary_orderable_weakref(self): import operator with mock_db_trans() as conn: user_1 = self._create_user('*****@*****.**', conn) user_2 = self._create_user('*****@*****.**', conn) ref_1 = wref.ArbitraryOrderableWeakRef(user_1) ref_2 = wref.ArbitraryOrderableWeakRef(user_2) operator.lt(ref_1, ref_2) operator.gt(ref_1, ref_2)
def test_eq_ne(self): with mock_db_trans() as conn: user_1 = self._create_user('*****@*****.**', conn) user_2 = self._create_user('*****@*****.**', conn) ref_1 = wref.ArbitraryOrderableWeakRef(user_1) ref_2 = wref.ArbitraryOrderableWeakRef(user_2) assert_that(ref_1, is_(ref_1)) assert_that(ref_2, is_not(ref_1)) assert_that(ref_1, is_not(ref_2)) assert_that(ref_1 != ref_1, is_(False)) # pylint:disable=comparison-with-itself assert_that(ref_1 != ref_2, is_(True)) assert_that(ref_1 != object(), is_(True))
def test_in_btree(self): import warnings with mock_db_trans() as conn: user_1 = self._create_user('*****@*****.**', conn) user_2 = self._create_user('*****@*****.**', conn) for clazz in (wref.NoCachingArbitraryOrderableWeakRef, wref.ArbitraryOrderableWeakRef, wref.WeakRef): bt = BTrees.OOBTree.OOBTree() # pylint:disable=no-member ref_1 = clazz(user_1) ref_2 = clazz(user_2) with warnings.catch_warnings(record=True): bt[ref_1] = 1 bt[ref_2] = 2 assert_that(bt[ref_1], is_(1)) assert_that(bt[ref_2], is_(2))
def test_non_orderable_weakref_generates_warnings(self): import warnings import operator with mock_db_trans() as conn: user_1 = self._create_user('*****@*****.**', conn) user_2 = self._create_user('*****@*****.**', conn) ref_1 = wref.WeakRef(user_1) ref_2 = wref.WeakRef(user_2) with warnings.catch_warnings(record=True) as warns: operator.lt(ref_1, ref_2) assert_that(warns, has_length(1)) with warnings.catch_warnings(record=True) as warns: operator.gt(ref_1, ref_2) assert_that(warns, has_length(1))
def test_pickle(self): with mock_db_trans() as conn: user = self._create_user('*****@*****.**', conn) ref = wref.WeakRef(user) assert_that(ref, has_property('_v_entity_cache', user)) copy = pickle.loads(pickle.dumps(ref)) assert_that(copy, has_property('_v_entity_cache', none())) assert_that(copy(), is_(user)) assert_that(ref, is_(copy)) assert_that(copy, is_(ref)) assert_that(repr(copy), is_(repr(ref))) assert_that(hash(copy), is_(hash(ref))) assert_that(ref, verifiably_provides(IWeakRef)) assert_that(ref, verifiably_provides(ICachingWeakRef)) assert_that(ref, verifiably_provides(IWeakRefToMissing))
def test_no_caching(self): with mock_db_trans() as conn: user = self._create_user('*****@*****.**', conn) ref = wref.NoCachingArbitraryOrderableWeakRef(user) assert_that(ref(), is_not(none()))