def test_copy(self): r = Registry(self.data_items) r_other = r.copy() self.assertListEqual(list(r_other), list(r)) # Registry descendants r = MyRegistry(self.data_items) r_other = r.copy() self.assertIsInstance(r_other, MyRegistry) self.assertListEqual(list(r_other), list(r))
def test_filterfalse(self): r = Registry(self.data_items) # result = r.filterfalse(lambda x: x.key > 5) self.assertIsInstance(result, GeneratorType) self.assertListEqual(list(result), self.data_items[:5]) # result = r.filterfalse('item.key > 5') self.assertListEqual(list(result), self.data_items[:5])
def test_create(self): r = Registry() # Simple self.assertDictEqual(r._reg, {}) # From items with self.assertRaises(TypeError): Registry(object) r = Registry(self.data_items) self.assertSequenceEqual(r._reg.keys(), self.dict_items.keys()) self.assertListEqual(list(r._reg.values()), list(self.dict_items.values()))
def test_dict_delete(self): i1 = self.data_items[0] r = Registry(self.data_items) self.assertIn(i1, r) del r[i1] self.assertNotIn(i1, r) r.store(i1) self.assertIn(i1, r) del r[i1.key] self.assertNotIn(i1, r)
def test_occurrence(self): r = Registry(self.data_items) expect = sum(1 for x in r if x.key > 5) # result = r.occurrence(lambda x: x.key > 5) self.assertIsInstance(result, int) self.assertEqual(result, expect) # result = r.occurrence('item.key > 5') self.assertEqual(result, expect)
def test_report(self): r = Registry(self.data_desc[:2]) expect = [(1, 'Item 01', "This is item 'Item 01'"), (2, 'Item 02', "This is item 'Item 02'")] # rpt = r.report(lambda x: (x.key, x.item.name, x.description)) self.assertIsInstance(rpt, GeneratorType) self.assertListEqual(list(rpt), expect) # rpt = list(r.report('item.key', 'item.item.name', 'item.description')) self.assertListEqual(rpt, expect)
def test_popitem(self): icopy = self.data_items.copy() r = Registry(self.data_items) self.assertListEqual(list(r), icopy) # last = icopy.pop() result = r.popitem() self.assertEqual(result, last) self.assertListEqual(list(r), icopy) first = icopy.pop(0) result = r.popitem(False) self.assertEqual(result, first) self.assertListEqual(list(r), icopy)
def test_dict_update(self): i1 = self.data_items[0] d1 = self.data_desc[0] r = Registry(self.data_items) self.assertEqual(r[i1.key], i1) r[i1] = d1 self.assertEqual(r[i1.key], d1)
def test_dict_access(self): i5 = self.data_items[4] r = Registry(self.data_items) self.assertEqual(r[i5], i5) self.assertEqual(r[i5.key], i5) with self.assertRaises(KeyError): r['NOT IN REGISTRY']
def __init__(self, *, parser: ConfigParser = None, manager: ChannelManager = None): """ Arguments: parser: ConfigParser instance to be used for bundle configuration. manager: ChannelManager to be used. """ self.log_context = None self.mngr: ChannelManager = manager self._ext_mngr: bool = manager is not None if manager is None: self.mngr = ChannelManager(zmq.Context.instance()) self.mngr.log_context = weakref.proxy(self) #: ConfigParser with service bundle configuration self.config: ConfigParser = \ ConfigParser(interpolation=ExtendedInterpolation()) if parser is None else parser #: List with ThreadControllers for all service instances in bundle self.services: List[ThreadController] = [] #: Registry with ServiceDescriptors for services that could be run self.available_services: Registry = Registry(get_service_desciptors()) # self.config[SECTION_LOCAL_ADDRESS] = {} self.config[SECTION_NODE_ADDRESS] = {} self.config[SECTION_NET_ADDRESS] = {} self.config[SECTION_SERVICE_UID] = {} self.config[SECTION_PEER_UID] = {} # Defaults self.config[DEFAULTSECT]['here'] = os.getcwd() # Assign Agent IDs for available services self.config[SECTION_SERVICE_UID].update( (sd.agent.name, sd.agent.uid.hex) for sd in self.available_services)
def test_in(self): nil = Item(100, "NOT IN REGISTRY") i1 = self.data_items[0] r = Registry(self.data_items) self.assertIn(i1, r) self.assertIn(i1.key, r) self.assertNotIn('NOT IN REGISTRY', r) self.assertNotIn(nil, r)
def test_store(self): i1 = self.data_items[0] d2 = self.data_desc[1] r = Registry() r.store(i1) self.assertDictEqual(r._reg, {i1.key: i1}) r.store(d2) self.assertDictEqual(r._reg, {i1.key: i1, d2.key: d2,}) with self.assertRaises(ValueError): r.store(i1)
def test_find(self): i5 = self.data_items[4] r = Registry(self.data_items) result = r.find(lambda x: x.key >= 5) self.assertIsInstance(result, Item) self.assertEqual(result, i5) self.assertIsNone(r.find(lambda x: x.key > 100)) self.assertEqual(r.find(lambda x: x.key > 100, 'DEFAULT'), 'DEFAULT') self.assertEqual(r.find('item.key >= 5'), i5) self.assertIsNone(r.find('item.key > 100')) self.assertEqual(r.find('item.key > 100', 'DEFAULT'), 'DEFAULT')
def test_get(self): i5 = self.data_items[4] r = Registry(self.data_items) self.assertEqual(r.get(i5), i5) self.assertEqual(r.get(i5.key), i5) self.assertIsNone(r.get('NOT IN REGISTRY')) self.assertEqual(r.get('NOT IN REGISTRY', i5), i5)
def test_contains(self): # Simple r = Registry(self.data_items) self.assertTrue(r.contains('item.key >= 5')) self.assertTrue(r.contains(lambda x: x.key >= 5)) self.assertFalse(r.contains('item.key > 100')) self.assertFalse(r.contains(lambda x: x.key > 100))
def test_pop(self): icopy = self.data_items.copy() i5 = icopy.pop(4) r = Registry(self.data_items) result = r.pop(i5.key) self.assertEqual(result, i5) self.assertListEqual(list(r), icopy) self.assertIsNone(r.pop('NOT IN REGISTRY')) self.assertListEqual(list(r), icopy) r = Registry(self.data_items) result = r.pop(i5) self.assertEqual(result, i5) self.assertListEqual(list(r), icopy)
def test_all(self): r = Registry(self.data_items) self.assertTrue(r.all(lambda x: x.name.startswith('Item'))) self.assertFalse(r.all(lambda x: '1' in x.name)) self.assertTrue(r.all("item.name.startswith('Item')")) self.assertFalse(r.all("'1' in item.name"))
def test_len(self): r = Registry(self.data_items) self.assertEqual(len(r), len(self.data_items))
def test_dict_iter(self): r = Registry(self.data_items) self.assertListEqual(list(r), list(self.dict_items.values()))
def test_remove(self): i1 = self.data_items[0] r = Registry(self.data_items) self.assertIn(i1, r) r.remove(i1) self.assertNotIn(i1, r)
def test_clear(self): r = Registry(self.data_items) r.clear() self.assertListEqual(list(r), []) self.assertEqual(len(r), 0)
def test_any(self): r = Registry(self.data_items) self.assertTrue(r.any(lambda x: '05' in x.name)) self.assertFalse(r.any(lambda x: x.name.startswith('XXX'))) self.assertTrue(r.any("'05' in item.name")) self.assertFalse(r.any("item.name.startswith('XXX')"))
def test_extend(self): i1 = self.data_items[0] # Single item r = Registry() r.extend(i1) self.assertListEqual(list(r), [i1]) # From list r = Registry(self.data_items[:5]) r.extend(self.data_items[5:]) self.assertListEqual(list(r), list(self.dict_items.values())) # From dict r = Registry() r.extend(self.dict_items) self.assertListEqual(list(r), list(self.dict_items.values())) # From registry r = Registry() r_other = Registry(self.data_items) r.extend(r_other) self.assertListEqual(list(r), list(self.dict_items.values()))
def test_repr(self): r = Registry(self.data_items) self.assertEqual(repr(r), """Registry([Item(key=1, name='Item 01'), Item(key=2, name='Item 02'), Item(key=3, name='Item 03'), Item(key=4, name='Item 04'), Item(key=5, name='Item 05'), Item(key=6, name='Item 06'), Item(key=7, name='Item 07'), Item(key=8, name='Item 08'), Item(key=9, name='Item 09'), Item(key=10, name='Item 10')])""")
def test_update(self): i1 = self.data_items[0] d1 = self.data_desc[0] r = Registry(self.data_items) # Single item self.assertEqual(r[i1.key], i1) r.update(d1) self.assertEqual(r[i1.key], d1) # From list r = Registry(self.data_items) r.update(self.data_desc) self.assertListEqual(list(r), list(self.dict_desc.values())) # From dict r = Registry(self.data_items) r.update(self.dict_desc) self.assertListEqual(list(r), list(self.dict_desc.values())) # From registry r = Registry(self.data_items) r_other = Registry(self.data_desc) r.update(r_other) self.assertListEqual(list(r), list(self.dict_desc.values()))