def test_format_json_no_dot_notation(self): mapping = OrderedDict() mapping["one"] = 1 mapping["two"] = 2 mapping["three"] = 3 formatted = format_json(mapping) results = json.dumps(formatted) self.assertEqual(results, '{"one": "1", "two": "2", "three": "3"}')
def test_format_json_values_are_strings(self): mapping = OrderedDict() mapping["one"] = 1 mapping["two"] = "ste" mapping["three"] = None formatted = format_json(mapping) self.assertEqual(formatted["one"], str(mapping["one"])) self.assertEqual(formatted["two"], str(mapping["two"])) self.assertEqual(formatted["three"], "")
def test_format_json_values_with_dot_notation(self): mapping = OrderedDict() mapping["one.a.b"] = 1 mapping["one.a.c"] = 2 mapping["two.a.b.c.d"] = 3 mapping["three"] = 3 formatted = format_json(mapping) self.assertEqual(formatted["one"]["a"]["b"], "1") self.assertEqual(formatted["one"]["a"]["c"], "2") self.assertEqual(formatted["three"], "3") self.assertEqual(formatted["two"]["a"]["b"]["c"]["d"], "3")
def generate_json(tenant, output_file, task_info, extract_args): """ Write data extract to JSON file. @param tenant: Requestor's tenant ID @param output_file: File pathname of extract file @param task_info: Task information for recording stats @param extract_args: Arguments specific to generate_json """ query = extract_args[TaskConstants.TASK_QUERIES][QueryType.QUERY] with EdCoreDBConnection(tenant=tenant) as connection, open(output_file, 'w') as json_file: results = connection.get_result(query) # There should only be one result in the list if len(results) is 1: formatted = format_json(results[0]) json.dump(formatted, json_file, indent=4) insert_extract_stats(task_info, {Constants.STATUS: ExtractStatus.GENERATED_JSON}) else: insert_extract_stats(task_info, {Constants.STATUS: ExtractStatus.FAILED, Constants.INFO: "Results length is: " + str(len(results))})
def test_format_json_empty_input(self): mapping = {} formatted = format_json(mapping) self.assertIsInstance(formatted, OrderedDict) self.assertEqual(len(formatted.keys()), 0)