[Python] assertEqual compare json objects raise error because of 'quote' in Python

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página