forked from argoverse/argoverse-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_json_utils.py
40 lines (28 loc) · 1.24 KB
/
test_json_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# <Copyright 2019, Argo AI, LLC. Released under the MIT license.>
"""Unit tests for JSON utility functions."""
import json
import pathlib
from argoverse.utils.json_utils import read_json_file, save_json_dict
_TEST_DIR = pathlib.Path(__file__).parent
def test_read_json_file() -> None:
"""Test reading from a JSON file.
Load a file that has the following dictionary saved as JSON:
dict = {'a':1,'b':'2','c':[9,8,7,6,5,'d','c','b','a']}
"""
fpath = _TEST_DIR / "test_data/json_read_test_file.json"
json_data = read_json_file(fpath)
gt_dict = {"a": 1, "b": "2", "c": [9, 8, 7, 6, 5, "d", "c", "b", "a"]}
assert gt_dict == json_data
def test_save_json_dict() -> None:
"""Test saving a dictionary to a JSON file."""
json_fpath = _TEST_DIR / "test_data/json_save_test_file.json"
intended_dict = {"a": 1, "b": None, "c": 9999, "d": "abc"}
save_json_dict(json_fpath, intended_dict)
# verify it was saved correctly
with open(json_fpath, "rb") as f:
loaded_dict = json.load(f)
assert intended_dict == loaded_dict
# make sure key sets are identical
assert set(intended_dict.keys()) == set(loaded_dict.keys())
for k in intended_dict.keys():
assert intended_dict[k] == loaded_dict[k]