Skip to content

Commit 91cdb50

Browse files
committed
Fix tests (added new fixtures, etc)
1 parent 682c495 commit 91cdb50

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

tests/conftest.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,26 @@ def builder_with_setup(builder: Builder):
2828
return builder
2929

3030

31+
@pytest.fixture
32+
def builder_with_test_files(builder: Builder):
33+
builder._write_directory = Path("tests/test_files/").absolute()
34+
35+
return builder
36+
37+
38+
@pytest.fixture
39+
def test_files():
40+
screen_path = Path("tests/test_files/test_bob.bob").absolute()
41+
dest_path = Path("tests/test_files/").absolute()
42+
43+
return screen_path, dest_path
44+
45+
3146
@pytest.fixture
3247
def example_json_map():
3348
# Create test json map with child json map
3449
test_map_child = JsonMap("test_child_bob.bob", exists=False)
35-
test_map = JsonMap("tests/test_files/test_bob.bob")
50+
test_map = JsonMap("test_bob.bob")
3651
test_map.children.append(test_map_child)
3752

3853
return test_map

tests/test_builder.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,18 @@ def test_write_json_map(builder):
176176
os.remove(dest_path)
177177

178178

179-
def test_generate_json_map(builder, example_json_map):
180-
screen_path = Path("tests/test_files/test_bob.bob")
181-
dest_path = Path("tests/test_files/")
179+
def test_generate_json_map(builder_with_test_files, example_json_map, test_files):
180+
screen_path, dest_path = test_files
182181

183182
# We don't want to access the _get_action_group function in this test
184183
with patch("techui_builder.builder._get_action_group") as mock_get_action_group:
185184
mock_xml = objectify.Element("action")
186185
mock_xml["file"] = "test_child_bob.bob"
187186
mock_get_action_group.return_value = mock_xml
188187

189-
test_json_map = builder._generate_json_map(screen_path, dest_path)
188+
test_json_map = builder_with_test_files._generate_json_map(
189+
screen_path.absolute(), dest_path
190+
)
190191

191192
assert test_json_map == example_json_map
192193

@@ -204,9 +205,10 @@ def test_generate_json_map(builder, example_json_map):
204205
# assert test_json_map == example_json_map
205206

206207

207-
def test_generate_json_map_get_macros(builder, example_json_map):
208-
screen_path = Path("tests/test_files/test_bob.bob")
209-
dest_path = Path("tests/test_files/")
208+
def test_generate_json_map_get_macros(
209+
builder_with_test_files, example_json_map, test_files
210+
):
211+
screen_path, dest_path = test_files
210212

211213
# Set a custom macro to test against
212214
example_json_map.children[0].macros = {"macro": "value"}
@@ -220,43 +222,49 @@ def test_generate_json_map_get_macros(builder, example_json_map):
220222
macros["macro"] = "value"
221223
mock_get_action_group.return_value = mock_xml
222224

223-
test_json_map = builder._generate_json_map(screen_path, dest_path)
225+
test_json_map = builder_with_test_files._generate_json_map(
226+
screen_path, dest_path
227+
)
224228

225229
assert test_json_map == example_json_map
226230

227231

228-
def test_generate_json_map_visited_node(builder, example_json_map):
229-
screen_path = Path("tests/test_files/test_bob.bob")
230-
dest_path = Path("tests/test_files/")
232+
def test_generate_json_map_visited_node(
233+
builder_with_test_files, example_json_map, test_files
234+
):
235+
screen_path, dest_path = test_files
231236

232237
visited = {screen_path}
233238
# Clear children as they will never be read
234239
example_json_map.children = []
235240
# Need to set this to true too
236241
example_json_map.duplicate = True
237242

238-
test_json_map = builder._generate_json_map(screen_path, dest_path, visited)
243+
test_json_map = builder_with_test_files._generate_json_map(
244+
screen_path, dest_path, visited
245+
)
239246

240247
assert test_json_map == example_json_map
241248

242249

243-
def test_generate_json_map_xml_parse_error(builder):
244-
screen_path = Path("tests/test_files/test_bob_bad.bob")
245-
dest_path = Path("tests/test_files/")
250+
def test_generate_json_map_xml_parse_error(builder_with_test_files, test_files):
251+
screen_path = Path("tests/test_files/test_bob_bad.bob").absolute()
252+
_, dest_path = test_files
246253

247-
test_json_map = builder._generate_json_map(screen_path, dest_path)
254+
test_json_map = builder_with_test_files._generate_json_map(screen_path, dest_path)
248255

249256
assert test_json_map.error.startswith("XML parse error:")
250257

251258

252-
def test_generate_json_map_other_exception(builder):
253-
screen_path = Path("tests/test_files/test_bob.bob")
254-
dest_path = Path("tests/test_files/")
259+
def test_generate_json_map_other_exception(builder_with_test_files, test_files):
260+
screen_path, dest_path = test_files
255261

256262
with patch("techui_builder.builder._get_action_group") as mock_get_action_group:
257263
mock_get_action_group.side_effect = Exception("Some exception")
258264

259-
test_json_map = builder._generate_json_map(screen_path, dest_path)
265+
test_json_map = builder_with_test_files._generate_json_map(
266+
screen_path, dest_path
267+
)
260268

261269
assert test_json_map.error != ""
262270

@@ -265,7 +273,7 @@ def test_serialise_json_map(example_json_map):
265273
json_ = _serialise_json_map(example_json_map) # type: ignore
266274

267275
assert json_ == {
268-
"file": "tests/test_files/test_bob.bob",
276+
"file": "test_bob.bob",
269277
"children": [{"file": "test_child_bob.bob", "exists": False}],
270278
}
271279

0 commit comments

Comments
 (0)