def test_select_kvps_with_key_error(self): self.assertEqual(jsutil.get_kvps(DOC, ['d01']), {}) self.assertEqual(jsutil.get_kvps(DOC, ['a01.a11']), {}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c11.c21.c31']), {}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c14.c31']), {}) self.assertEqual(jsutil.get_kvps(DOC, ['a01', 'c01.c11', 'c01.c13.c23']), {'a01': 1, 'c01': {'c11': 3}})
def test_select_kvps_with_key_error(self): self.assertEqual(jsutil.get_kvps(DOC, ["d01"]), {}) self.assertEqual(jsutil.get_kvps(DOC, ["a01.a11"]), {}) self.assertEqual(jsutil.get_kvps(DOC, ["c01.c11.c21.c31"]), {}) self.assertEqual(jsutil.get_kvps(DOC, ["c01.c14.c31"]), {}) self.assertEqual( jsutil.get_kvps(DOC, ["a01", "c01.c11", "c01.c13.c23"]), { "a01": 1, "c01": { "c11": 3 } }, )
def format(self, subject, *args, **kwargs): attributes = kwargs.get('attributes', None) if type(subject) is str: subject = json.loads(subject) elif not isinstance(subject, (list, tuple)) and not hasattr(subject, '__iter__'): doc = subject if isinstance(subject, dict) else subject.__dict__ keys = doc.keys() if not attributes or 'all' in attributes else attributes docs = jsutil.get_kvps(doc, keys) else: docs = [] for item in subject: doc = item if isinstance(item, dict) else item.__dict__ keys = doc.keys() if not attributes or 'all' in attributes else attributes docs.append(jsutil.get_kvps(doc, keys)) return json.dumps(docs, indent=4, sort_keys=True)
def test_execution_get_default_in_json(self): argv = ['execution', 'get', EXECUTION['id'], '-j'] content = self._get_execution(argv) self.assertEqual( json.loads(content), jsutil.get_kvps(EXECUTION, ['id', 'status', 'parameters', 'result']))
def test_execution_get_result_by_key_in_json(self): argv = [ 'execution', 'get', EXECUTION['id'], '-k', 'localhost.stdout', '-j' ] content = self._get_execution(argv) self.assertDictEqual( json.loads(content), jsutil.get_kvps(EXECUTION, ['result.localhost.stdout']))
def test_execution_get_result_by_key_in_json(self): argv = [ "execution", "get", EXECUTION["id"], "-k", "localhost.stdout", "-j" ] content = self._get_execution(argv) self.assertDictEqual( json.loads(content), jsutil.get_kvps(EXECUTION, ["result.localhost.stdout"]))
def format(self, subject, *args, **kwargs): attributes = kwargs.get("attributes", None) if type(subject) is str: subject = json.loads(subject) elif not isinstance(subject, (list, tuple)) and not hasattr( subject, "__iter__"): doc = subject if isinstance(subject, dict) else subject.__dict__ keys = (list(doc.keys()) if not attributes or "all" in attributes else attributes) docs = jsutil.get_kvps(doc, keys) else: docs = [] for item in subject: doc = item if isinstance(item, dict) else item.__dict__ keys = (list(doc.keys()) if not attributes or "all" in attributes else attributes) docs.append(jsutil.get_kvps(doc, keys)) return docs
def test_select_kvps(self): self.assertEqual(jsutil.get_kvps(DOC, ["a01"]), {"a01": 1}) self.assertEqual(jsutil.get_kvps(DOC, ["c01.c11"]), {"c01": { "c11": 3 }}) self.assertEqual(jsutil.get_kvps(DOC, ["c01.c13.c22"]), {"c01": { "c13": { "c22": 6 } }}) self.assertEqual(jsutil.get_kvps(DOC, ["c01.c13"]), {"c01": { "c13": { "c21": 5, "c22": 6 } }}) self.assertEqual(jsutil.get_kvps(DOC, ["c01.c14"]), {"c01": { "c14": [7, 8, 9] }}) self.assertEqual( jsutil.get_kvps(DOC, ["a01", "c01.c11", "c01.c13.c21"]), { "a01": 1, "c01": { "c11": 3, "c13": { "c21": 5 } } }, ) self.assertEqual( jsutil.get_kvps( DOC_IP_ADDRESS, ['ips."192.168.1.1"', 'ips."192.168.1.10".hostname']), { "ips": { '"192': { "168": { "1": { '1"': { "hostname": "router.domain.tld" }, '10"': { "hostname": "server.domain.tld" }, } } } } }, )
def test_select_kvps(self): self.assertEqual(jsutil.get_kvps(DOC, ['a01']), {'a01': 1}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c11']), {'c01': { 'c11': 3 }}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13.c22']), {'c01': { 'c13': { 'c22': 6 } }}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13']), {'c01': { 'c13': { 'c21': 5, 'c22': 6 } }}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c14']), {'c01': { 'c14': [7, 8, 9] }}) self.assertEqual( jsutil.get_kvps(DOC, ['a01', 'c01.c11', 'c01.c13.c21']), { 'a01': 1, 'c01': { 'c11': 3, 'c13': { 'c21': 5 } } })
def test_select_kvps(self): self.assertEqual(jsutil.get_kvps(DOC, ['a01']), {'a01': 1}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c11']), {'c01': { 'c11': 3 }}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13.c22']), {'c01': { 'c13': { 'c22': 6 } }}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13']), {'c01': { 'c13': { 'c21': 5, 'c22': 6 } }}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c14']), {'c01': { 'c14': [7, 8, 9] }}) self.assertEqual( jsutil.get_kvps(DOC, ['a01', 'c01.c11', 'c01.c13.c21']), { 'a01': 1, 'c01': { 'c11': 3, 'c13': { 'c21': 5 } } }) self.assertEqual( jsutil.get_kvps( DOC_IP_ADDRESS, ['ips."192.168.1.1"', 'ips."192.168.1.10".hostname']), { 'ips': { '"192': { '168': { '1': { '1"': { 'hostname': 'router.domain.tld' }, '10"': { 'hostname': 'server.domain.tld' } } } } } })
def test_select_kvps(self): self.assertEqual(jsutil.get_kvps(DOC, ['a01']), {'a01': 1}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c11']), {'c01': {'c11': 3}}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13.c22']), {'c01': {'c13': {'c22': 6}}}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13']), {'c01': {'c13': {'c21': 5, 'c22': 6}}}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c14']), {'c01': {'c14': [7, 8, 9]}}) self.assertEqual(jsutil.get_kvps(DOC, ['a01', 'c01.c11', 'c01.c13.c21']), {'a01': 1, 'c01': {'c11': 3, 'c13': {'c21': 5}}})
def test_execution_get_default_in_json(self): argv = ["execution", "get", EXECUTION["id"], "-j"] content = self._get_execution(argv) self.assertEqual( json.loads(content), jsutil.get_kvps( EXECUTION, [ "id", "action.ref", "context.user", "start_timestamp", "end_timestamp", "status", "parameters", "result", ], ), )
def test_select_kvps(self): self.assertEqual(jsutil.get_kvps(DOC, ['a01']), {'a01': 1}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c11']), {'c01': {'c11': 3}}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13.c22']), {'c01': {'c13': {'c22': 6}}}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c13']), {'c01': {'c13': {'c21': 5, 'c22': 6}}}) self.assertEqual(jsutil.get_kvps(DOC, ['c01.c14']), {'c01': {'c14': [7, 8, 9]}}) self.assertEqual(jsutil.get_kvps(DOC, ['a01', 'c01.c11', 'c01.c13.c21']), {'a01': 1, 'c01': {'c11': 3, 'c13': {'c21': 5}}}) self.assertEqual(jsutil.get_kvps(DOC_IP_ADDRESS, ['ips."192.168.1.1"', 'ips."192.168.1.10".hostname']), {'ips': {'"192': {'168': {'1': {'1"': {'hostname': 'router.domain.tld'}, '10"': {'hostname': 'server.domain.tld'}}}}}})
def test_execution_get_default_in_json(self): argv = ['execution', 'get', EXECUTION['id'], '-j'] content = self._get_execution(argv) self.assertEqual(json.loads(content), jsutil.get_kvps(EXECUTION, ['id', 'status', 'parameters', 'result']))
def test_execution_get_result_by_key_in_json(self): argv = ['execution', 'get', EXECUTION['id'], '-k', 'localhost.stdout', '-j'] content = self._get_execution(argv) self.assertDictEqual(json.loads(content), jsutil.get_kvps(EXECUTION, ['result.localhost.stdout']))