Skip to content

Commit 90a5810

Browse files
committed
[test/parser_] Move file content
Moving file content in test_parser_json and test_parser_yaml to increase readability.
1 parent 32c84c3 commit 90a5810

File tree

2 files changed

+157
-136
lines changed

2 files changed

+157
-136
lines changed

test/test_parser_json.py

Lines changed: 98 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,100 @@
1212
from odml.tools.parser_utils import ParserException, InvalidVersionException
1313

1414

15+
_INVALID_ATTRIBUTE_HANDLING_DOC = """
16+
{
17+
"Document": {
18+
"id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
19+
"%s": "i_do_not_exist_on_doc_level",
20+
"sections": [
21+
{
22+
"id": "51f3c79c-a7d7-471e-be16-e085caa851fb",
23+
"%s": "i_do_not_exist_on_sec_level",
24+
"type": "test",
25+
"name": "sec",
26+
"sections": [],
27+
"properties": [
28+
{
29+
"id": "c5aed35a-d9ff-437d-82d6-68f0cda8ea94",
30+
"%s": "i_do_not_exist_on_prop_level",
31+
"name": "prop",
32+
"value": [
33+
1,
34+
2,
35+
3
36+
],
37+
"type": "int"
38+
}
39+
]
40+
}
41+
]
42+
},
43+
"odml-version": "1.1"
44+
}
45+
""".strip()
46+
47+
_SEC_CREATION_ERROR_DOC = """
48+
{
49+
"Document": {
50+
"id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
51+
"sections": [
52+
{
53+
"id": "1113c79c-a7d7-471e-be16-e085caa851fb",
54+
"name": "sec",
55+
"type": "test",
56+
"sections": [
57+
{
58+
"id": "1213c79c-a7d7-471e-be16-e085caa851fb",
59+
"name": "%s",
60+
"type": "test"
61+
},
62+
{
63+
"id": [
64+
"I-am-so-kaputt"
65+
],
66+
"name": "%s",
67+
"type": "test"
68+
}
69+
]
70+
}
71+
]
72+
},
73+
"odml-version": "1.1"
74+
}
75+
""".strip()
76+
77+
_PROP_CREATION_ERROR_DOC = """
78+
{
79+
"Document": {
80+
"id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
81+
"sections": [
82+
{
83+
"id": "51f3c79c-a7d7-471e-be16-e085caa851fb",
84+
"type": "test",
85+
"name": "sec",
86+
"properties": [
87+
{
88+
"id": "121ed35a-d9ff-437d-82d6-68f0cda8ea94",
89+
"name": "valid_prop"
90+
},
91+
{
92+
"id": "122ed35a-d9ff-437d-82d6-68f0cda8ea94",
93+
"name": "invalid_prop",
94+
"value": [
95+
"a",
96+
"b"
97+
],
98+
"type": "int"
99+
}
100+
]
101+
}
102+
]
103+
},
104+
"odml-version": "1.1"
105+
}
106+
""".strip()
107+
108+
15109
class TestJSONParser(unittest.TestCase):
16110

17111
def setUp(self):
@@ -82,36 +176,7 @@ def test_invalid_attribute_handling(self):
82176
inv_sec_attr = "invalid_sec_attr"
83177
inv_prop_attr = "invalid_prop_attr"
84178

85-
file_content = """{
86-
"Document": {
87-
"id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
88-
"%s": "i_do_not_exist_on_doc_level",
89-
"sections": [
90-
{
91-
"id": "51f3c79c-a7d7-471e-be16-e085caa851fb",
92-
"%s": "i_do_not_exist_on_sec_level",
93-
"type": "test",
94-
"name": "sec",
95-
"sections": [],
96-
"properties": [
97-
{
98-
"id": "c5aed35a-d9ff-437d-82d6-68f0cda8ea94",
99-
"%s": "i_do_not_exist_on_prop_level",
100-
"name": "prop",
101-
"value": [
102-
1,
103-
2,
104-
3
105-
],
106-
"type": "int"
107-
}
108-
]
109-
}
110-
]
111-
},
112-
"odml-version": "1.1"
113-
}
114-
""" % (inv_doc_attr, inv_sec_attr, inv_prop_attr)
179+
file_content = _INVALID_ATTRIBUTE_HANDLING_DOC % (inv_doc_attr, inv_sec_attr, inv_prop_attr)
115180
parsed_doc = self._prepare_doc(filename, file_content)
116181

117182
# Test ParserException on default parse
@@ -138,34 +203,7 @@ def test_sec_creation_error(self):
138203
valid = "valid_sec"
139204
invalid = "invalid_sec"
140205

141-
file_content = """{
142-
"Document": {
143-
"id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
144-
"sections": [
145-
{
146-
"id": "1113c79c-a7d7-471e-be16-e085caa851fb",
147-
"name": "sec",
148-
"type": "test",
149-
"sections": [
150-
{
151-
"id": "1213c79c-a7d7-471e-be16-e085caa851fb",
152-
"name": "%s",
153-
"type": "test"
154-
},
155-
{
156-
"id": [
157-
"I-am-so-kaputt"
158-
],
159-
"name": "%s",
160-
"type": "test"
161-
}
162-
]
163-
}
164-
]
165-
},
166-
"odml-version": "1.1"
167-
}
168-
""" % (valid, invalid)
206+
file_content = _SEC_CREATION_ERROR_DOC % (valid, invalid)
169207
parsed_doc = self._prepare_doc(filename, file_content)
170208

171209
# Test ParserException on default parse
@@ -189,36 +227,8 @@ def test_sec_creation_error(self):
189227

190228
def test_prop_creation_error(self):
191229
filename = "broken_property.yaml"
192-
file_content = """{
193-
"Document": {
194-
"id": "6af2a3ec-9f3a-42d6-a59d-95f3ccbaa383",
195-
"sections": [
196-
{
197-
"id": "51f3c79c-a7d7-471e-be16-e085caa851fb",
198-
"type": "test",
199-
"name": "sec",
200-
"properties": [
201-
{
202-
"id": "121ed35a-d9ff-437d-82d6-68f0cda8ea94",
203-
"name": "valid_prop"
204-
},
205-
{
206-
"id": "122ed35a-d9ff-437d-82d6-68f0cda8ea94",
207-
"name": "invalid_prop",
208-
"value": [
209-
"a",
210-
"b"
211-
],
212-
"type": "int"
213-
}
214-
]
215-
}
216-
]
217-
},
218-
"odml-version": "1.1"
219-
}
220-
"""
221-
parsed_doc = self._prepare_doc(filename, file_content)
230+
231+
parsed_doc = self._prepare_doc(filename, _PROP_CREATION_ERROR_DOC)
222232

223233
# Test ParserException on default parse
224234
exc_msg = "Property not created"

test/test_parser_yaml.py

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,60 @@
1212
from odml.tools.parser_utils import ParserException, InvalidVersionException
1313

1414

15+
_INVALID_ATTRIBUTE_HANDLING_DOC = """
16+
Document:
17+
id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
18+
%s: i_do_not_exist_on_doc_level
19+
sections:
20+
- id: d4f3120a-c02f-4102-a9fe-2e8b77d1d0d2
21+
name: sec
22+
%s: i_do_not_exist_on_sec_level
23+
properties:
24+
- id: 18ad5176-2b46-4a08-9b85-eafd6b14fab7
25+
name: prop
26+
value: []
27+
%s: i_do_not_exist_on_prop_level
28+
sections: []
29+
type: test
30+
odml-version: '1.1'
31+
""".strip()
32+
33+
_SEC_CREATION_ERROR_DOC = """
34+
Document:
35+
id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
36+
sections:
37+
- id: 1111a20a-c02f-4102-a9fe-2e8b77d1d0d2
38+
name: sec
39+
sections:
40+
- id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
41+
name: %s
42+
type: test
43+
- id: [I-am-so-kaputt]
44+
name: %s
45+
type: test
46+
odml-version: '1.1'
47+
""".strip()
48+
49+
_PROP_CREATION_ERROR_DOC = """
50+
Document:
51+
id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
52+
sections:
53+
- id: 1111a20a-c02f-4102-a9fe-2e8b77d1d0d2
54+
name: sec
55+
properties:
56+
- id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
57+
name: valid_prop
58+
- id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
59+
name: invalid_prop
60+
type: int
61+
values:
62+
- 'a'
63+
- 'b'
64+
type: test
65+
odml-version: '1.1'
66+
""".strip()
67+
68+
1569
class TestYAMLParser(unittest.TestCase):
1670

1771
def setUp(self):
@@ -82,22 +136,7 @@ def test_invalid_attribute_handling(self):
82136
inv_sec_attr = "invalid_sec_attr"
83137
inv_prop_attr = "invalid_prop_attr"
84138

85-
file_content = """Document:
86-
id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
87-
%s: i_do_not_exist_on_doc_level
88-
sections:
89-
- id: d4f3120a-c02f-4102-a9fe-2e8b77d1d0d2
90-
name: sec
91-
%s: i_do_not_exist_on_sec_level
92-
properties:
93-
- id: 18ad5176-2b46-4a08-9b85-eafd6b14fab7
94-
name: prop
95-
value: []
96-
%s: i_do_not_exist_on_prop_level
97-
sections: []
98-
type: test
99-
odml-version: '1.1'
100-
""" % (inv_doc_attr, inv_sec_attr, inv_prop_attr)
139+
file_content = _INVALID_ATTRIBUTE_HANDLING_DOC % (inv_doc_attr, inv_sec_attr, inv_prop_attr)
101140

102141
parsed_doc = self._prepare_doc(filename, file_content)
103142

@@ -125,20 +164,8 @@ def test_sec_creation_error(self):
125164
valid = "valid_sec"
126165
invalid = "invalid_sec"
127166

128-
file_content = """Document:
129-
id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
130-
sections:
131-
- id: 1111a20a-c02f-4102-a9fe-2e8b77d1d0d2
132-
name: sec
133-
sections:
134-
- id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
135-
name: %s
136-
type: test
137-
- id: [I-am-so-kaputt]
138-
name: %s
139-
type: test
140-
odml-version: '1.1'
141-
""" % (valid, invalid)
167+
file_content = _SEC_CREATION_ERROR_DOC % (valid, invalid)
168+
142169
parsed_doc = self._prepare_doc(filename, file_content)
143170

144171
# Test ParserException on default parse
@@ -162,24 +189,8 @@ def test_sec_creation_error(self):
162189

163190
def test_prop_creation_error(self):
164191
filename = "broken_property.yaml"
165-
file_content = """Document:
166-
id: 82408bdb-1d9d-4fa9-b4dd-ad78831c797c
167-
sections:
168-
- id: 1111a20a-c02f-4102-a9fe-2e8b77d1d0d2
169-
name: sec
170-
properties:
171-
- id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
172-
name: valid_prop
173-
- id: 1121a20a-c02f-4102-a9fe-2e8b77d1d0d2
174-
name: invalid_prop
175-
type: int
176-
values:
177-
- 'a'
178-
- 'b'
179-
type: test
180-
odml-version: '1.1'
181-
"""
182-
parsed_doc = self._prepare_doc(filename, file_content)
192+
193+
parsed_doc = self._prepare_doc(filename, _PROP_CREATION_ERROR_DOC)
183194

184195
# Test ParserException on default parse
185196
exc_msg = "Property not created"

0 commit comments

Comments
 (0)