Пример #1
0
 def test_namemap(self):
     k = ITEMS.keys()[0]
     nonames = BaseCollection(ITEMS)
     assert nonames.name_for(k) is None
     assert nonames.byname() is None
     withnames = BaseCollection(ITEMS, NAMES)
     assert withnames.name_for(k) == withnames[k].getId()
     assert withnames.uid_for(withnames[k].getId()) == k
     assert withnames.all_names(k) == (withnames[k].getId(),)
     self.assertIsInstance(withnames.byname(), BaseNamedCollection)
     self.assertIsInstance(withnames._name_to_uid, dict)
     self.assertIsInstance(withnames._uid_to_names, dict)
Пример #2
0
 def test_set_union(self):
     collection1 = BaseCollection(ITEMS, NAMES)
     collection2 = BaseCollection(ITEMS2)
     collection3 = collection1.union(collection2)
     self.assertIsInstance(collection3, BaseCollection)
     assert len(collection3) == len(collection2) == 3
     assert len(collection3) - 1 == len(collection1)
     for k in collection1:
         assert k in collection3
         assert collection3.name_for(k) is None  # c2 had no names
     for k in collection2:
         assert k in collection3
         assert collection3.name_for(k) is None
     # de-dupe means not strictly concatentation (length):
     assert len(collection1) + len(collection2) > len(collection3)
     # operator and union() equivalent:
     self.assertEqual(
         set((collection1 | collection2).keys()),
         set(collection1.union(collection2).keys())
         )
     self.assertEqual(
         set((collection1 + collection2).keys()),
         set(collection1.union(collection2).keys())
         )
     # now test with a namemap:
     namemap = NAMES.copy()
     uid = str(uuid.uuid3(NS_PKG, 'item3'))
     namemap['item3'] = uid
     collection4 = BaseCollection(ITEMS2, namemap)
     collection5 = collection1.union(collection4)
     assert collection5.uid_for('item3') == uid
     assert collection4.name_for(uid) == 'item3'