コード例 #1
0
 def test_unique_key_dict_update(self):
     adict = {}
     print("1============")
     pprint(adict)
     stub_node_1 = StubNode('mary')
     name2 = unique_key_dict_update(adict, 'mary', stub_node_1)
     print("2============")
     pprint(adict)
     self.assertDictEqual(adict, dict(mary=stub_node_1))
     self.assertEqual(name2, 'mary')
     stub_node_2 = StubNode('mary')
     name3 = unique_key_dict_update(adict, 'mary', stub_node_2)
     print("3============")
     pprint(adict)
     self.assertEqual(len(adict), 3)
     self.assertTrue(len(name3), UUID_LENGTH + 4)
     for key, node in adict.items():
         self.assertTrue(key.startswith('mary'))
         self.assertTrue(len(key)in [4, UUID_LENGTH + 5])
         if not node.name.startswith('duplicate$'):
             # The name should always match the key
             self.assertEqual(node.name, key)
     for key in adict.iterkeys():
         self.assertEqual(key[:4], 'mary')
     stub_node_3 = StubNode('mary')
     name4 = unique_key_dict_update(adict, 'mary', stub_node_3)
     print("4============")
     pprint(adict)
     self.assertEqual(len(adict), 4)
     self.assertTrue(len(name4), UUID_LENGTH + 4)
     for key, node in adict.items():
         self.assertTrue(key.startswith('mary'))
         self.assertTrue(len(key) in [4, UUID_LENGTH + 5])
         if not node.name.startswith('duplicate$'):
             # The name should always match the key
             self.assertEqual(node.name, key)
     for key in adict.iterkeys():
         self.assertEqual(key[:4], 'mary')
コード例 #2
0
 def add(self, node):
     """Add node object to the universal dictionary, and the appropriate
     class set. Fix the name to avoid duplicates, if necessary.
     """
     unique_key_dict_update(self.nodes, node.key, node)