I have a function which converts a dataframe to a json object
def convert_to_json(dataframe):
output_json = json.dumps({"XYZ": dataframe.to_dict('records')}, default=str)
return output_json
Then in my unit test:
def test_convert_to_json(self):
test_data = {
'col_2': ['2018-03-02', '2018-03-01'],
'col_3': ['12345678', '12345678'],
'col_4': [31, 31],
'col_5': [0.035133, 0.035133]
}
test_df = pd.DataFrame(test_data)
test_json = xxx.convert_to_json(self.test_df)
expected_json = {"XYZ": [{"col_2": "2018-03-02", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}, {"col_2": "2018-03-01", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}]}
self.assertEqual(test_json, expected_json)
This test failed because of the quote:
Ran 1 test in 0.008s
FAILED (failures=1)
{'XYZ': [{'col_2': '2018-03-02',
'col_3': '12345678',
'col_4': 31,
'col_5': 0.035133},
{'col_2': '2018-03-01',
'col_3': '12345678',
'col_4': 31,
'col_5': 0.035133}]} != {"XYZ": [{"col_2": "2018-03-02", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}, {"col_2": "2018-03-01", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}]}
However, if I print out the value for test_json
and expected_json
, they both use double quotes:
{"XYZ": [{"col_2": "2018-03-02", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}, {"col_2": "2018-03-01", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}]}
{"XYZ": [{"col_2": "2018-03-02", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}, {"col_2": "2018-03-01", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}]}
I'm new to unit test, very confused about the code behavior, can someone help please? Thanks.
As @khelwood mentioned, you are comparing a string and a dict. I would suggest you to change the expected_json
variable to string (do it only if that is really what you are expecting to get from the tested function, of course).
Try this:
expected_json = '{"XYZ": [{"col_2": "2018-03-02", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}, {"col_2": "2018-03-01", "col_3": "12345678", "col_4": 31, "col_5": 0.035133}]}'
(Note the single quotes surrounding the value)