def test_json_csv_keys_vals_vary(tmpdir): """Test that a JSON array of objects with varying keys and varying value types works as expected, i.e.: - the headers of the CSV are the union of all attributes of all objects in the JSON array: ``[{'a': ...}, {'b': ...}]`` yields ``'a,b\r\n'``; - the order of the JSON object attributes does not matter; - a JSON object value may be an array or a string: ``[{'a': ['x', 'y']}, {'a': 'z'}]`` yields ``'a,a\r\nx,y\r\nz,\r\n'``. """ json_array = json.loads(JSON_KEYS_VALS_VARY) headers = json_metadata_to_csv.fetch_keys(json_array) assert headers == KEYS_VALS_VARY_HEADERS first_json_obj, second_json_obj = json_array first_row = json_metadata_to_csv.object_to_row( json_metadata_to_csv.fix_encoding(first_json_obj), headers) assert first_row == KEYS_VALS_VARY_FIRST_ROW second_row = json_metadata_to_csv.object_to_row( json_metadata_to_csv.fix_encoding(second_json_obj), headers) assert second_row == KEYS_VALS_VARY_SECOND_ROW json_path = os.path.join(str(tmpdir), 'metadata.json') csv_path = os.path.join(str(tmpdir), 'metadata.csv') with open(json_path, 'w') as jsonfile: jsonfile.write(JSON_KEYS_VALS_VARY) job = Job("stub", "stub", ["", json_path]) json_metadata_to_csv.call([job]) with open(csv_path) as csvfile: csvdata = csvfile.read() assert CSV_KEYS_VALS_VARY == csvdata
def test_json_csv_conversion_with_int_val(tmpdir): json_path = os.path.join(str(tmpdir), 'metadata.json') csv_path = os.path.join(str(tmpdir), 'metadata.csv') with open(json_path, 'w') as jsonfile: jsonfile.write(JSON_INT_VAL) job = Job("stub", "stub", ["", json_path]) json_metadata_to_csv.call([job]) with open(csv_path) as csvfile: csvdata = csvfile.read() assert csvdata == CSV_INT_VAL
def test_json_csv_conversion_with_repeated_columns(tmpdir): json_path = os.path.join(str(tmpdir), 'metadata.json') csv_path = os.path.join(str(tmpdir), 'metadata.csv') with open(json_path, 'w') as jsonfile: jsonfile.write(JSON_MULTICOLUMN) job = Job("stub", "stub", ["", json_path]) json_metadata_to_csv.call([job]) with open(csv_path) as csvfile: csvdata = csvfile.read() assert csvdata == CSV_MULTICOLUMN
def test_json_csv_with_nested_null_data(tmpdir): json_path = os.path.join(str(tmpdir), 'metadata.json') csv_path = os.path.join(str(tmpdir), 'metadata.csv') with open(json_path, 'w') as jsonfile: jsonfile.write(JSON_NESTED_NULL) job = Job("stub", "stub", ["", json_path]) json_metadata_to_csv.call([job]) with open(csv_path) as csvfile: csvdata = csvfile.read() assert csvdata == CSV_NESTED_NULL
def test_json_csv_conversion(tmpdir): json_path = os.path.join(str(tmpdir), "metadata.json") csv_path = os.path.join(str(tmpdir), "metadata.csv") with open(json_path, "w") as jsonfile: jsonfile.write(JSON) job = Job("stub", "stub", ["", json_path]) json_metadata_to_csv.call([job]) with open(csv_path) as csvfile: csvdata = csvfile.read() assert csvdata == CSV