def test_eq_ne(self): cases = ( ( { 'node': { 'key': 'key1' } }, { 'node': { 'key': 'key2' } }, False, ), ( { 'node': { 'key': 'key1' } }, { 'node': { 'key': 'key1' } }, True, ), ( { 'node': { 'key': 'key1', 'value': 'val1' } }, { 'node': { 'key': 'key1', 'value': 'val2' } }, False, ), ) for node1, node2, expected_res in cases: res1 = etcd.EtcdKeysResult(**node1) res2 = etcd.EtcdKeysResult(**node2) self.assertEqual(res1 == res2, expected_res)
def test_get_subtree_2_level(self): leaf0 = { 'key': "/test/leaf0", 'value': "hello1", 'expiration': None, 'ttl': None, 'modifiedIndex': 5, 'createdIndex': 1, 'newKey': False, 'dir': False, } leaf1 = { 'key': "/test/leaf1", 'value': "hello2", 'expiration': None, 'ttl': None, 'modifiedIndex': 6, 'createdIndex': 2, 'newKey': False, 'dir': False, } testnode = { "node": { 'key': "/test/", 'expiration': None, 'ttl': None, 'modifiedIndex': 6, 'createdIndex': 2, 'newKey': False, 'dir': True, 'nodes': [leaf0, leaf1] } } result = etcd.EtcdKeysResult(**testnode) for k, v in testnode['node'].items(): if not hasattr(result, k): continue self.assertEqual(v, getattr(result, k)) self.assertListEqual(result._children, testnode['node']['nodes']) subtree = list(result.get_subtree(leaves_only=True)) self.assertEqual("/test/leaf0", subtree[0].key) self.assertEqual("/test/leaf1", subtree[1].key) self.assertEqual(len(subtree), 2) self.assertListEqual(subtree, list(result.leaves)) subtree = list(result.get_subtree(leaves_only=False)) self.assertEqual("/test/", subtree[0].key) self.assertEqual("/test/leaf0", subtree[1].key) self.assertEqual("/test/leaf1", subtree[2].key) self.assertEqual(len(subtree), 3)
def test_get_subtree_3_level(self): leaf0 = { 'key': "/test/mid0/leaf0", 'value': "hello1", } leaf1 = { 'key': "/test/mid0/leaf1", 'value': "hello2", } leaf2 = { 'key': "/test/mid1/leaf2", 'value': "hello1", } leaf3 = { 'key': "/test/mid1/leaf3", 'value': "hello2", } mid0 = {'key': "/test/mid0/", 'dir': True, 'nodes': [leaf0, leaf1]} mid1 = {'key': "/test/mid1/", 'dir': True, 'nodes': [leaf2, leaf3]} testnode = { "node": { 'key': "/test/", 'dir': True, 'nodes': [mid0, mid1] } } result = etcd.EtcdKeysResult(**testnode) for k, v in testnode['node'].items(): if not hasattr(result, k): continue self.assertEqual(v, getattr(result, k)) self.assertListEqual(result._children, testnode['node']['nodes']) subtree = list(result.get_subtree(leaves_only=True)) self.assertEqual("/test/mid0/leaf0", subtree[0].key) self.assertEqual("/test/mid0/leaf1", subtree[1].key) self.assertEqual("/test/mid1/leaf2", subtree[2].key) self.assertEqual("/test/mid1/leaf3", subtree[3].key) self.assertEqual(len(subtree), 4) self.assertListEqual(subtree, list(result.leaves)) subtree = list(result.get_subtree(leaves_only=False)) self.assertEqual("/test/", subtree[0].key) self.assertEqual("/test/mid0/", subtree[1].key) self.assertEqual("/test/mid0/leaf0", subtree[2].key) self.assertEqual("/test/mid0/leaf1", subtree[3].key) self.assertEqual("/test/mid1/", subtree[4].key) self.assertEqual("/test/mid1/leaf2", subtree[5].key) self.assertEqual("/test/mid1/leaf3", subtree[6].key) self.assertEqual(len(subtree), 7)
def test_get_subtree_1_level(self): response = { "node": { 'key': "/test", 'value': "hello", 'expiration': None, 'ttl': None, 'modifiedIndex': 5, 'createdIndex': 1, 'newKey': False, 'dir': False, } } result = etcd.EtcdKeysResult(**response) for k, v in response['node'].items(): self.assertEqual(v, getattr(result, k)) self.assertListEqual([result], list(result.get_subtree(leaves_only=False))) self.assertListEqual([result], list(result.get_subtree(leaves_only=True))) self.assertListEqual([result], list(result.leaves))