-
Notifications
You must be signed in to change notification settings - Fork 80
/
test_consistency.py
174 lines (136 loc) · 4.85 KB
/
test_consistency.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
164
165
166
167
168
169
170
171
172
173
174
#
# Copyright 2020-2021 Valkyrie Systems Corporation
#
# Licensed under MIT License. See LICENSE.
#
import itertools
import pytest
import sarpy.consistency.consistency as con
class DummyConsistency(con.ConsistencyChecker):
"""A ConsistencyChecker used for unit testing and code coverage"""
def __init__(self):
super(DummyConsistency, self).__init__()
def check_need_pass(self):
with self.need('need pass'):
assert True
def check_need_fail(self):
with self.need('need fail'):
assert False
def check_need_both(self):
with self.need('need pass'):
assert True
with self.need('need fail'):
assert False
def check_need_fail_nodetails(self):
with self.need():
assert False
def check_pre_need_pass(self):
with self.precondition():
assert True
with self.need('need pass'):
assert True
def check_nopre_need_pass(self):
with self.precondition():
assert False
with self.need('need pass'):
assert True
def check_want_pass(self):
with self.want('want pass'):
assert True
def check_want_fail(self):
with self.want('want fail'):
assert False
def check_pre_want_pass(self):
with self.precondition():
assert True
with self.want('want pass'):
assert True
def check_nopre_want_pass(self):
with self.precondition():
assert False
with self.want('want pass'):
assert True
def check_exception(self):
raise ValueError
@pytest.fixture
def dummycon():
"""Fixture which initializes a DummyConsistency object
Yields
------
DummyConsistency object
"""
import ast
import os
import _pytest.assertion.rewrite
base, _ = os.path.splitext(__file__) # python2 can return the '*.pyc' file
with open(base + '.py', 'r') as fd:
source = fd.read()
tree = ast.parse(source)
try:
_pytest.assertion.rewrite.rewrite_asserts(tree)
except TypeError as e:
_pytest.assertion.rewrite.rewrite_asserts(tree, source)
co = compile(tree, __file__, 'exec', dont_inherit=True)
ns = {}
exec(co, ns)
cover_con = ns['DummyConsistency']()
yield cover_con
def test_all(dummycon, capsys):
dummycon.check()
assert len(dummycon.all()) == 11
assert len(dummycon.failures()) == 5
num_checks_by_part = [len(x) for x in (dummycon.passes(), dummycon.skips(), dummycon.failures())]
assert all(x > 0 for x in num_checks_by_part)
assert sum(num_checks_by_part) == len(dummycon.all())
failures = dummycon.failures()
details = itertools.chain.from_iterable([value['details'] for value in failures.values()])
passed = [item for item in details if item['passed']]
assert passed
failures = dummycon.failures(omit_passed_sub=True)
details = itertools.chain.from_iterable([value['details'] for value in failures.values()])
passed = [item for item in details if item['passed']]
assert not passed
dummycon.print_result()
captured = capsys.readouterr()
assert '\x1b' in captured.out
dummycon.print_result(color=False)
captured2 = capsys.readouterr()
assert '\x1b' not in captured2.out
dummycon.print_result(include_passed_checks=True, skip_detail=True, fail_detail=True, pass_detail=True)
captured3 = capsys.readouterr()
assert 'Skip' in captured3.out
assert 'check_nopre_want_pass' in captured3.out
assert 'check_want_pass' in captured3.out
def test_one(dummycon):
dummycon.check('check_need_pass')
assert not dummycon.failures()
def test_multiple(dummycon):
dummycon.check(['check_need_pass', 'check_need_fail'])
assert set(dummycon.failures()) == {'check_need_fail'}
assert set(dummycon.all()).difference(dummycon.failures()) == {'check_need_pass'}
def test_check_with_ignore_pattern(dummycon):
# all checks must start with check_
dummycon.check(ignore_patterns=['check_'])
assert set(dummycon.all()) == set()
@pytest.mark.parametrize('should_ignore', [True, False])
def test_check_with_ignore_specific(dummycon, should_ignore):
test_name = 'check_exception'
ignore_patterns = [test_name] if should_ignore else []
dummycon.check(ignore_patterns=ignore_patterns)
was_tested = (test_name in dummycon.all())
assert was_tested != should_ignore
def test_invalid(dummycon):
with pytest.raises(ValueError):
dummycon.check('this_does_not_exist')
def test_approx():
apx = con.Approx(10.0, atol=.1, rtol=0)
assert apx == 10.0
assert apx == 10.01
assert not apx != 10.01
assert apx > 10.01
assert apx >= 10.01
assert apx >= 0
assert not apx <= 0
assert apx < 10.01
assert apx <= 10.01
assert repr(apx) == "10.0 ± 0.1"