-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_json_fixs.py
76 lines (66 loc) · 2.3 KB
/
test_json_fixs.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Tests
To run: pytest
"""
# pylint: disable=C0103,R0915,C0301,C0411
import pytest
import json
from utils import get_fixed_json
bad_json = """
{ "topics":[
{"topicID": 1, "score": 0.3, "explanation": "The article"},
{"topicID": 11, "score": 0, "explanation": "The article ."},
{"topicID": 12, "score": 0, "explanation": "The article ."}, ],
"primary_topic":
{ "topic_id" : 5, "score": 0.5, "explanation": "The article ." },
"secondary_topic":
{ "topic_id" : 1, "score": 0.3, "explanation": " potential connection to the topic." }
}
"""
bad_json1 = """
{ "topics":[
{"topicID": 1, "score": 0.3, "explanation": "The article"},
{"topicID": 11, "score": 0, "explanation": "The article ."},
{"topicID": 12, "score": 0, "explanation": "The article ."},
],
"primary_topic":
{ "topic_id" : 5, "score": 0.5, "explanation": "The article ." },
"secondary_topic":
{ "topic_id" : 1, "score": 0.3, "explanation": " potential connection to the topic." }
}
"""
bad_json2 = """
{ "topics":[
{"topicID": 1, "score": 0.3, "explanation": "The article"},
{"topicID": 11, "score": 0, "explanation": "The article ."},
{"topicID": 12, "score": 0, "explanation": "The article ."}, ],
"primary_topic":
{ "topic_id" : 5, "score": 0.5, "explanation": "The article ." },
"secondary_topic":
{ "topic_id" : 1, "score": 0.3, "explanation": " potential connection to the topic." }
}
"""
bad_json3 = """This is your json:
{ "topics":[
{"topicID": 1, "score": 0.3, "explanation": "The article"},
{"topicID": 11, "score": 0, "explanation": "The article ."},
{"topicID": 12, "score": 0, "explanation": "The article ."},
],
"primary_topic":
{ "topic_id" : 5, "score": 0.5, "explanation": "The article ." },
"secondary_topic":
{ "topic_id" : 1, "score": 0.3, "explanation": " potential connection to the topic." }
}
Feel free to contact me if any questions.
"""
def check_bad_json(s : str):
with pytest.raises(Exception):
_ = json.loads(s)
ok_json = json.loads(get_fixed_json(s))
assert ok_json is not None
def test_json_fix():
"""Test for fix json"""
check_bad_json(bad_json)
check_bad_json(bad_json1)
check_bad_json(bad_json2)
check_bad_json(bad_json3)