Coverage for jsonurl_data_test.py: 92%

38 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-06 11:15 +0000

1""" 

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

3""" 

4 

5import json 

6from pathlib import Path 

7 

8import pytest 

9 

10import jsonurl_py as jsonurl 

11 

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

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

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

15 

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

17 JSON_TEST_DATA = json.load(f) 

18 

19 

20def _param_values(): 

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

22 

23 

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

25def test(name: str): 

26 global JSON_TEST_DATA 

27 item = JSON_TEST_DATA[name] 

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

29 kw = {} 

30 val = item.get("implied_array") 

31 if val is not None: 

32 kw["implied_list"] = val 

33 val = item.get("implied_object") 

34 if val is not None: 

35 kw["implied_dict"] = val 

36 val = item.get("aqf") 

37 if val is not None: 

38 kw["aqf"] = val 

39 if type == "roundtrip": 

40 text = item["text"] 

41 data = item["data"] 

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

43 elif type == "load": 

44 text = item["text"] 

45 data = item["data"] 

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

47 elif type == "fail": 47 ↛ 52line 47 didn't jump to line 52 because the condition on line 47 was always true

48 text = item["text"] 

49 with pytest.raises(jsonurl.ParseError): 

50 jsonurl.loads(text, **kw) 

51 else: 

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