-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
163 lines (126 loc) · 4.81 KB
/
__init__.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
158
159
160
161
162
163
import json
import os
from contextlib import AbstractContextManager
from pelican.util import settings
def is_subset_dict(subset, superset):
return subset.items() <= superset.items()
def read(basename):
with open(os.path.join("tests", "fixtures", f"{basename}.json")) as f:
return json.load(f)
class OverrideSettings(AbstractContextManager):
def __init__(self, **kwargs):
self.new = kwargs
self.old = {}
for key in self.new:
self.old[key] = getattr(settings, key)
def __enter__(self):
for key, value in self.new.items():
setattr(settings, key, value)
def __exit__(self, *args):
for key, value in self.old.items():
setattr(settings, key, value)
# I'm not sure if the below can be accomplished with pytest, so using unittest with pytest-subtests. At first glance,
# we would need to auto-generate parametrized `test_` functions, with the module as a bound variable.
class FieldCoverageTests:
def test_passing(self):
# Ensure the child class is configured.
assert self.passing
for item in self.passing:
with self.subTest(item=item):
result = self.module.calculate(item, "key")
assert result == {
"name": self.module.name,
"result": True,
"value": None,
"reason": None,
"version": 1.0,
}
def test_failing(self):
# Ensure the child class is configured.
assert self.failing
for params in self.failing:
item = params[0]
reason = params[1]
return_value = params[2] if len(params) > 2 else None
with self.subTest(item=item):
result = self.module.calculate(item, "key")
assert result == {
"name": self.module.name,
"result": False,
"value": return_value,
"reason": reason,
"version": 1.0,
}
class FieldQualityTests:
passing_kwargs = {}
failing_kwargs = {}
method = "calculate"
def setUp(self):
self.method = getattr(self.module, self.method)
def test_passing(self):
for value in self.passing:
with self.subTest(value=value):
result = self.method({"xxx": value}, "xxx", **self.passing_kwargs)
assert result == {
"name": self.module.name,
"result": True,
"value": None,
"reason": None,
"version": 1.0,
}
def test_failing(self):
for params in self.failing:
value = params[0]
reason = params[1]
return_value = params[2] if len(params) > 2 else value
with self.subTest(value=value):
result = self.method({"xxx": value}, "xxx", **self.failing_kwargs)
assert result == {
"name": self.module.name,
"result": False,
"value": return_value,
"reason": reason,
"version": 1.0,
}
class CompiledReleaseTests:
maxDiff = None
passing_kwargs = {}
failing_kwargs = {}
method = "calculate"
def setUp(self):
self.method = getattr(self.module, self.method)
def test_skipping(self):
skipping = self.skipping
skipping.append(({}, skipping[0][1]))
for item, reason in skipping:
with self.subTest(item=item):
result = self.method(item, **self.passing_kwargs)
assert result == {
"result": None,
"meta": {"reason": reason},
"application_count": None,
"pass_count": None,
"version": 1.0,
}
def test_passing(self):
for item, meta, count in self.passing:
with self.subTest(item=item):
result = self.method(item, **self.passing_kwargs)
assert result == {
"result": True,
"meta": meta,
"application_count": count,
"pass_count": count,
"version": 1.0,
}
def test_failing(self):
for item, meta, application_count, pass_count in self.failing:
with self.subTest(item=item):
result = self.method(item, **self.failing_kwargs)
assert result == {
"result": False,
"meta": meta,
"application_count": application_count,
"pass_count": pass_count,
"version": 1.0,
}