-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_config.py
157 lines (109 loc) · 4.53 KB
/
test_config.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
from collections.abc import Callable
import pytest
import yaml
from wimsey import config
@pytest.fixture
def test_suite():
return [
{"test": "mean_should", "column": "a", "be_exactly": 34},
{"test": "type_should", "column": "y", "be_one_of": ["int64", "float64"]},
]
def throw_import_error(*args, **kwargs):
raise ImportError
def test_collect_tests_returns_list_of_callable_functions(test_suite):
actual = config.collect_tests(test_suite)
assert all(isinstance(i, Callable) for i in actual)
def test_collect_tests_returns_friendly_error_when_required_value_not_give(test_suite):
test_suite[0].pop("column")
with pytest.raises(TypeError, match="column"):
config.collect_tests(test_suite)
def test_collect_tests_returns_friendly_error_when_no_test_is_given(test_suite):
test_suite[1].pop("test")
with pytest.raises(ValueError, match="test"):
config.collect_tests(test_suite)
def test_collect_tests_returns_input_when_input_is_already_test_functions(test_suite):
initial = config.collect_tests(test_suite)
actual = config.collect_tests(initial)
assert actual == initial
def test_read_config_parses_yaml(monkeypatch, test_suite):
class DummyOpenFile:
def __enter__(self, *args, **kwargs):
return self
def __exit__(self, *args, **kwargs):
...
def read(self, *args, **kwargs):
return yaml.dump(test_suite)
def open_file_patch(*args, **kwargs):
return DummyOpenFile()
monkeypatch.setattr(config.fsspec, "open", open_file_patch)
actual = config.read_config("file.yaml")
assert all(isinstance(i, Callable) for i in actual)
def test_read_config_parses_yaml_with_test_section(monkeypatch, test_suite):
class DummyOpenFile:
def __enter__(self, *args, **kwargs):
return self
def __exit__(self, *args, **kwargs):
...
def read(self, *args, **kwargs):
return yaml.dump({"cool": ["some", "cool", "stuff"], "tests": test_suite})
def open_file_patch(*args, **kwargs):
return DummyOpenFile()
monkeypatch.setattr(config.fsspec, "open", open_file_patch)
actual = config.read_config("file.yaml")
assert all(isinstance(i, Callable) for i in actual)
def test_read_config_parses_yaml_with_only_one_test(monkeypatch, test_suite):
class DummyOpenFile:
def __enter__(self, *args, **kwargs):
return self
def __exit__(self, *args, **kwargs):
...
def read(self, *args, **kwargs):
return yaml.dump(test_suite[0])
def open_file_patch(*args, **kwargs):
return DummyOpenFile()
monkeypatch.setattr(config.fsspec, "open", open_file_patch)
actual = config.read_config("file.yaml")
assert all(isinstance(i, Callable) for i in actual)
def test_read_config_parses_json(monkeypatch, test_suite):
class DummyOpenFile:
def __enter__(self, *args, **kwargs):
return self
def __exit__(self, *args, **kwargs):
...
def read(self, *args, **kwargs):
return json.dumps(test_suite)
def open_file_patch(*args, **kwargs):
return DummyOpenFile()
monkeypatch.setattr(config.fsspec, "open", open_file_patch)
actual = config.read_config("file.json")
assert all(isinstance(i, Callable) for i in actual)
def test_friendly_message_is_raised_when_yaml_is_unimportable(test_suite, monkeypatch):
class DummyOpenFile:
def __enter__(self, *args, **kwargs):
return self
def __exit__(self, *args, **kwargs):
...
def read(self, *args, **kwargs):
return json.dumps(test_suite)
def open_file_patch(*args, **kwargs):
return DummyOpenFile()
monkeypatch.setattr(config.fsspec, "open", open_file_patch)
monkeypatch.setattr(config, "collect_tests", throw_import_error)
with pytest.raises(ImportError, match="pip install pyyaml"):
config.read_config("file.yaml")
def test_friendly_message_is_raised_when_yaml_does_not_return_contents(
test_suite, monkeypatch
):
class DummyOpenFile:
def __enter__(self, *args, **kwargs):
return self
def __exit__(self, *args, **kwargs):
...
def read(self, *args, **kwargs):
return "dsafasdfasdf"
def open_file_patch(*args, **kwargs):
return DummyOpenFile()
monkeypatch.setattr(config.fsspec, "open", open_file_patch)
with pytest.raises(ValueError, match="json/yaml"):
config.read_config("file.yaml")