Coverage for jsonurl_data_test.py: 93%

38 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-20 21:12 +0000

1""" 

2Run tests from https://github.com/cdleonard/jsonurl-test-data 

3""" 

4import json 

5from pathlib import Path 

6 

7import pytest 

8 

9import jsonurl_py as jsonurl 

10 

11data_path = Path(__file__).parent / "test-data/test_data.json" 

12if not data_path.exists(): 12 ↛ 13line 12 didn't jump to line 13, because the condition on line 12 was never true

13 pytest.skip("Test data not found", allow_module_level=True) 

14 

15with data_path.open("r") as f: 

16 JSON_TEST_DATA = json.load(f) 

17 

18 

19def _param_values(): 

20 return [name for name in JSON_TEST_DATA if name != "$schema"] 

21 

22 

23@pytest.mark.parametrize("name", _param_values()) 

24def test(name: str): 

25 global JSON_TEST_DATA 

26 item = JSON_TEST_DATA[name] 

27 type = item.get("type", "roundtrip") 

28 kw = {} 

29 val = item.get("implied_array") 

30 if val is not None: 

31 kw["implied_list"] = val 

32 val = item.get("implied_object") 

33 if val is not None: 

34 kw["implied_dict"] = val 

35 val = item.get("aqf") 

36 if val is not None: 

37 kw["aqf"] = val 

38 if type == "roundtrip": 

39 text = item["text"] 

40 data = item["data"] 

41 assert jsonurl.loads(text, **kw) == data and jsonurl.dumps(data, **kw) == text 

42 elif type == "load": 

43 text = item["text"] 

44 data = item["data"] 

45 assert jsonurl.loads(text, **kw) == data 

46 elif type == "fail": 46 ↛ 51line 46 didn't jump to line 51, because the condition on line 46 was never false

47 text = item["text"] 

48 with pytest.raises(jsonurl.ParseError): 

49 jsonurl.loads(text, **kw) 

50 else: 

51 raise ValueError("unknown data item type={type!r}")