def test_enumeration(self): """test enumeration and iteration""" _uids = [] mapper = UUIDMapper() # add ten random pairs: for i in range(10): uid = uuid.uuid4() _uids.append(str(uid)) mapper.add(uid) assert len(mapper) == 10 == len(mapper.keys()) assert len(mapper.items()) == len(mapper.values()) == 10 assert len(list(mapper.iteritems())) == 10 for uid in _uids: assert uid in mapper assert uid in mapper.keys() assert uid in mapper.iterkeys() assert mapper.get(uid) in mapper.values() assert mapper.get(uid) in mapper.itervalues() assert (uid, mapper.get(uid)) in mapper.items() assert (uid, mapper.get(uid)) in mapper.iteritems()
def test_add_remove(self): """Test add/remove and containment/get/length""" mapper = UUIDMapper() # add with pre-calculated docid uid, docid = uuid.uuid4(), 12345 rv = mapper.add(uid, docid) assert uid in mapper assert rv == (str(uid), docid) assert mapper.get(uid) == docid assert len(mapper) == 1 # Cannot add duplicate: self.assertRaises(KeyError, mapper.add, uid) self.assertRaises(KeyError, mapper.add, str(uid)) # add with generation of docid uid2 = uuid.uuid4() rv = mapper.add(uid2) assert len(mapper) == 2 docid2 = rv[1] assert rv == (str(uid2), docid2) assert uid2 in mapper assert docid2 in mapper assert mapper.get(uid2) == docid2 assert mapper.get(docid2) == str(uid2) # remove one by UUID mapper.remove(uid2) assert uid2 not in mapper assert docid2 not in mapper assert len(mapper) == 1 mapper.remove(docid) assert docid not in mapper assert uid not in mapper assert str(uid) not in mapper assert mapper.get(uid, None) is None assert len(mapper) == 0 # re-add okay: mapper.add(uid, docid) assert uid in mapper and docid in mapper assert len(mapper) == 1