Skip to content

Commit c67656d

Browse files
committed
Refactoring tests.
1 parent 4084c5c commit c67656d

7 files changed

Lines changed: 92 additions & 134 deletions

File tree

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
attr_utils
22
attrs
33
chemistry_tools[formulae]
4-
domdf_python_tools>=0.4.8
4+
domdf_python_tools>=0.4.9
55
enum_tools
66
lxml
77
pandas

tests/common.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# 3rd party
99
import pytest
1010
from _pytest.mark import MarkDecorator
11+
from domdf_python_tools.testing import testing_boolean_values
1112
from domdf_python_tools.utils import Len
1213

1314
whitespace = " \t\n\r"
@@ -28,37 +29,7 @@ def count(stop: int, start: int = 0, step: int = 1) -> MarkDecorator:
2829
return pytest.mark.parametrize("count", range(start, stop, step))
2930

3031

31-
true_false_strings = [
32-
(True, True),
33-
("True", True),
34-
("true", True),
35-
("tRUe", True),
36-
('y', True),
37-
('Y', True),
38-
("YES", True),
39-
("yes", True),
40-
("Yes", True),
41-
("yEs", True),
42-
("ON", True),
43-
("on", True),
44-
('1', True),
45-
(1, True),
46-
(-1, True),
47-
(False, False),
48-
("False", False),
49-
("false", False),
50-
("falSE", False),
51-
('n', False),
52-
('N', False),
53-
("NO", False),
54-
("no", False),
55-
("nO", False),
56-
("OFF", False),
57-
("off", False),
58-
("oFF", False),
59-
('0', False),
60-
(0, False),
61-
]
32+
true_false_strings = testing_boolean_values(extra_truthy=[-1]).mark.args[1]
6233

6334
_test_strings = [
6435
("foo", "foo"),

tests/test_utils.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
# 3rd party
55
import pytest
6+
from domdf_python_tools.testing import count, testing_boolean_values, whitespace_perms
67

78
# this package
89
from mh_utils.utils import as_path, camel_to_snake, element_to_bool
9-
from tests.common import count, true_false_strings, whitespace_perms
1010

1111

1212
class TestAsPath:
@@ -65,12 +65,9 @@ def test_as_path_no_whitespace(
6565

6666
class TestElementToBool:
6767

68-
@pytest.mark.parametrize(
69-
"obj, expects",
70-
true_false_strings,
71-
)
72-
def test_element_to_bool(self, obj, expects):
73-
assert element_to_bool(obj) == expects
68+
@testing_boolean_values(extra_truthy=[-1])
69+
def test_element_to_bool(self, boolean_string, expected_boolean):
70+
assert element_to_bool(boolean_string) == expected_boolean
7471

7572
@pytest.mark.parametrize(
7673
"obj, expects",

tests/test_worklist_parser/test_attribute.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,6 @@
66
from mh_utils.worklist_parser.enums import AttributeType
77

88

9-
def test_creation():
10-
data = Attribute(
11-
attribute_id=42,
12-
attribute_type=AttributeType.SystemDefined,
13-
field_type=7,
14-
system_name=" the system_name",
15-
header_name="the header_name ",
16-
data_type=1,
17-
default_data_value=" the default_data_value\t",
18-
reorder_id=22,
19-
show_hide_status="False", # type: ignore
20-
column_width=250,
21-
)
22-
23-
assert data.attribute_id == 42
24-
assert data.attribute_type == AttributeType.SystemDefined
25-
assert data.field_type == 7
26-
assert data.system_name == "the system_name"
27-
assert data.header_name == "the header_name"
28-
assert data.data_type == 1
29-
assert data.default_data_value == "the default_data_value"
30-
assert data.reorder_id == 22
31-
assert data.show_hide_status is False
32-
assert data.column_width == 250
33-
34-
359
class FakeXMLElement:
3610

3711
def __init__(self):
@@ -47,9 +21,30 @@ def __init__(self):
4721
self.ColumnWidth = "250"
4822

4923

50-
def test_from_xml():
51-
element = FakeXMLElement()
52-
data = Attribute.from_xml(element)
24+
data_from_init = Attribute(
25+
attribute_id=42,
26+
attribute_type=AttributeType.SystemDefined,
27+
field_type=7,
28+
system_name=" the system_name",
29+
header_name="the header_name ",
30+
data_type=1,
31+
default_data_value=" the default_data_value\t",
32+
reorder_id=22,
33+
show_hide_status="False", # type: ignore
34+
column_width=250,
35+
)
36+
37+
element = FakeXMLElement()
38+
data_from_element = data = Attribute.from_xml(element)
39+
40+
41+
@pytest.mark.parametrize(
42+
"data", [
43+
pytest.param(data_from_init, id="from init"),
44+
pytest.param(data_from_element, id="from element"),
45+
]
46+
)
47+
def test_attribute(data):
5348

5449
assert data.attribute_id == 42
5550
assert data.attribute_type == AttributeType.SystemDefined

tests/test_worklist_parser/test_job_data.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
11
# stdlib
22
from uuid import UUID
33

4+
# 3rd party
5+
import pytest
6+
47
# this package
58
from mh_utils.worklist_parser.classes import JobData
69
from tests.test_worklist_parser.test_parser import FakeSampleElement
710

811

9-
def test_creation():
12+
@pytest.mark.parametrize(
13+
"id, job_type, run_status",
14+
[
15+
("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}", 7, 1),
16+
("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}", 7, 1),
17+
(UUID("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}"), "7", "1"),
18+
]
19+
)
20+
def test_creation(id, job_type, run_status):
1021
data = JobData(
11-
id="{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}",
12-
job_type=7,
13-
run_status=1,
22+
id=id,
23+
job_type=job_type,
24+
run_status=run_status,
1425
)
1526

1627
assert data.id == UUID("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}")
1728
assert data.job_type == 7
1829
assert data.run_status == 1
1930
assert data.sample_info == {}
2031

21-
data = JobData(
22-
id="{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}",
23-
job_type=7,
24-
run_status=1,
25-
sample_info={"foo": "a string"},
26-
)
27-
28-
assert data.id == UUID("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}")
29-
assert data.job_type == 7
30-
assert data.run_status == 1
31-
assert data.sample_info == {"foo": "a string"}
3232

33+
@pytest.mark.parametrize(
34+
"id, job_type, run_status, sample_info",
35+
[
36+
("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}", 7, 1, {"foo": "a string"}),
37+
(UUID("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}"), "7", "1", {"foo": "a string"}),
38+
]
39+
)
40+
def test_creation_sample_info(id, job_type, run_status, sample_info):
3341
data = JobData(
34-
id=UUID("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}"),
35-
job_type="7", # type: ignore
36-
run_status="1", # type: ignore
37-
sample_info={"foo": "a string"},
42+
id=id,
43+
job_type=job_type,
44+
run_status=run_status,
45+
sample_info=sample_info,
3846
)
3947

4048
assert data.id == UUID("{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}")
@@ -65,40 +73,36 @@ def test_from_xml():
6573
assert data.run_status == 1
6674

6775

68-
def test_dict():
69-
data = JobData(
76+
@pytest.fixture()
77+
def sample_jobdata():
78+
return JobData(
7079
id="{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}",
7180
job_type=7,
7281
run_status=1,
7382
sample_info={"foo": "a string"},
7483
)
7584

76-
assert dict(data) == {
85+
86+
def test_dict(sample_jobdata):
87+
assert dict(sample_jobdata) == {
7788
"id": "B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C".lower(),
7889
"job_type": 7,
7990
"run_status": 1,
8091
"sample_info": {"foo": "a string"},
8192
}
8293

8394

84-
def test_repr():
85-
data = JobData(
86-
id="{B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C}",
87-
job_type=7,
88-
run_status=1,
89-
sample_info={"foo": "a string"},
90-
)
91-
92-
assert str(data).startswith("JobData(")
93-
assert str(data).endswith(")")
94-
assert str(data) == (
95+
def test_repr(sample_jobdata):
96+
assert str(sample_jobdata).startswith("JobData(")
97+
assert str(sample_jobdata).endswith(")")
98+
assert str(sample_jobdata) == (
9599
"JobData("
96100
f"id='{'B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C'.lower()}', "
97101
"job_type=7, run_status=1)"
98102
)
99-
assert repr(data).startswith("JobData(")
100-
assert repr(data).endswith(")")
101-
assert repr(data) == (
103+
assert repr(sample_jobdata).startswith("JobData(")
104+
assert repr(sample_jobdata).endswith(")")
105+
assert repr(sample_jobdata) == (
102106
"JobData("
103107
f"id='{'B1F6E4D5-A300-40DF-8FB0-2A26FD8B8C0C'.lower()}', "
104108
"job_type=7, run_status=1)"

tests/test_worklist_parser/test_macro.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from mh_utils.worklist_parser.classes import Macro
66

77

8-
def test_creation():
9-
data = Macro(
8+
@pytest.fixture()
9+
def sample_macro():
10+
return Macro(
1011
project_name="the project_name",
1112
procedure_name="the procedure_name",
1213
input_parameter="the input_parameter",
@@ -15,12 +16,15 @@ def test_creation():
1516
display_string="the display_string",
1617
)
1718

18-
assert data.project_name == "the project_name"
19-
assert data.procedure_name == "the procedure_name"
20-
assert data.input_parameter == "the input_parameter"
21-
assert data.output_data_type == 42
22-
assert data.output_parameter == "the output_parameter"
23-
assert data.display_string == "the display_string"
19+
20+
def test_creation(sample_macro):
21+
22+
assert sample_macro.project_name == "the project_name"
23+
assert sample_macro.procedure_name == "the procedure_name"
24+
assert sample_macro.input_parameter == "the input_parameter"
25+
assert sample_macro.output_data_type == 42
26+
assert sample_macro.output_parameter == "the output_parameter"
27+
assert sample_macro.display_string == "the display_string"
2428

2529

2630
class FakeXMLElement:
@@ -46,18 +50,11 @@ def test_from_xml():
4650
assert data.display_string == "the display_string"
4751

4852

49-
def test_undefined_and_repr():
50-
macro = Macro(
51-
project_name="the project_name",
52-
procedure_name="the procedure_name",
53-
input_parameter="the input_parameter",
54-
output_data_type=42,
55-
output_parameter="the output_parameter",
56-
display_string="the display_string",
57-
)
58-
assert not macro.undefined
53+
def test_undefined_and_repr(sample_macro):
54+
55+
assert not sample_macro.undefined
5956

60-
assert str(macro) == (
57+
assert str(sample_macro) == (
6158
"Macro("
6259
"project_name='the project_name', "
6360
"procedure_name='the procedure_name', "
@@ -67,7 +64,7 @@ def test_undefined_and_repr():
6764
"display_string='the display_string')"
6865
)
6966

70-
assert repr(macro) == (
67+
assert repr(sample_macro) == (
7168
"Macro("
7269
"project_name='the project_name', "
7370
"procedure_name='the procedure_name', "

tests/test_worklist_parser/test_parser.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
# 3rd party
77
import pytest
8+
from domdf_python_tools.testing import count, whitespace_perms
89

910
# this package
1011
from mh_utils.utils import camel_to_snake
1112
from mh_utils.worklist_parser.classes import Macro
1213
from mh_utils.worklist_parser.parser import parse_datetime, parse_params, parse_sample_info
13-
from tests.common import _test_strings, count, true_false_strings, whitespace_perms
14+
from tests.common import _test_strings, true_false_strings
1415

1516

1617
class FakeMacroElement:
@@ -82,7 +83,7 @@ class __TestParseParams_str:
8283
def test_parse_params(self, value, expects):
8384
e = FakeParamsElement()
8485
setattr(e, self.param_under_test, value)
85-
params = parse_params(e)
86+
params = parse_params(e) # type: ignore
8687
assert params[self.param_dict_name] == expects
8788
assert isinstance(params[self.param_dict_name], self.param_type)
8889

@@ -427,14 +428,7 @@ def test_whitespace(self, char: str, count: int):
427428
@count(100)
428429
@whitespace_perms()
429430
@dates
430-
def test(
431-
self,
432-
whitespace_pos,
433-
count,
434-
char,
435-
date,
436-
expects,
437-
):
431+
def test(self, whitespace_pos, count, char, date, expects):
438432

439433
if whitespace_pos == "left":
440434
with_whitespace = f"{char * count}{date}"

0 commit comments

Comments
 (0)