-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_pytest_plugin.py
More file actions
129 lines (102 loc) · 3.42 KB
/
test_pytest_plugin.py
File metadata and controls
129 lines (102 loc) · 3.42 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Test the pytest plugin."""
# LICENSE HEADER MANAGED BY add-license-header
# Copyright (c) 2023-2025 Blue Brain Project, EPFL.
#
# This file is part of dir-content-diff.
# See https://github.com/BlueBrain/dir-content-diff for further info.
#
# SPDX-License-Identifier: Apache-2.0
# LICENSE HEADER MANAGED BY add-license-header
# pylint: disable=missing-function-docstring
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
import pytest
@pytest.fixture
def tmp_conftest(ref_tree, res_tree_equal, ref_csv, res_csv_equal):
"""Create a temporary conftest file."""
return f"""
from pathlib import Path
import pytest
@pytest.fixture
def ref_path():
return Path("{ref_tree}")
@pytest.fixture
def res_path():
return Path("{res_tree_equal}")
"""
@pytest.mark.parametrize(
"do_export, export_suffix",
[
[False, None],
[False, "_CMD_SUFFIX"],
[True, None],
[True, "_CMD_SUFFIX"],
],
)
def test_export_formatted_data(
ref_tree,
res_tree_equal,
ref_csv,
res_csv_equal,
tmp_conftest,
pytester,
do_export,
export_suffix,
registry_reseter,
):
"""Test that the formatted files are properly exported."""
args = []
if export_suffix is None:
suffix = "_FORMATTED"
else:
suffix = export_suffix
expected_dir = f"""res_path.with_name(res_path.name + "{suffix}")"""
if not do_export:
tester = f"""assert not {expected_dir}.exists()"""
else:
args.append("--dcd-export-formatted-data")
if export_suffix is not None:
args.append("--dcd-export-suffix")
args.append(export_suffix)
tester = """assert sorted(expected_dir.iterdir()) == [
(expected_dir / "file").with_suffix(ext)
for ext in [".csv", ".ini", ".json", ".xml", ".yaml"]
]"""
expected_dir_str = f"""expected_dir = {expected_dir}"""
remover = """rmtree(expected_dir, ignore_errors=True)"""
# create a temporary conftest.py file
pytester.makeconftest(tmp_conftest)
# create a temporary pytest test file
pytester.makepyfile(
f"""
from shutil import rmtree
import dir_content_diff
import dir_content_diff.comparators.pandas
from dir_content_diff import assert_equal_trees
dir_content_diff.reset_comparators()
dir_content_diff.comparators.pandas.register()
def test_export_formatted_data_default(ref_path, res_path):
{expected_dir_str}
{remover}
assert_equal_trees(ref_path, res_path)
{tester}
def test_export_formatted_data_no_suffix(ref_path, res_path):
expected_dir = res_path.with_name(res_path.name + "_FORMATTED")
{remover}
assert_equal_trees(ref_path, res_path, export_formatted_files={do_export})
{tester}
def test_export_formatted_data_suffix(ref_path, res_path):
expected_dir = res_path.with_name(res_path.name + "{suffix}")
{remover}
assert_equal_trees(
ref_path,
res_path,
export_formatted_files="{suffix if do_export else False}",
)
{tester}
"""
)
# run all tests with pytest
result = pytester.runpytest(*args)
# check that all 3 tests passed
result.assert_outcomes(passed=3)