-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_bounding_box.py
More file actions
212 lines (186 loc) · 7.06 KB
/
test_bounding_box.py
File metadata and controls
212 lines (186 loc) · 7.06 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Test the bounding_box module."""
import re
import pytest
from data_morph.bounds.bounding_box import BoundingBox
from data_morph.bounds.interval import Interval
@pytest.mark.bounds
class TestBoundingBox:
"""Test the BoundingBox class."""
@pytest.mark.input_validation
@pytest.mark.parametrize(
('x_bounds', 'y_bounds'),
[
(None, None),
([0, 1], None),
(None, [0, 1]),
(Interval([0, 1]), None),
(None, Interval([0, 1])),
],
ids=[
'neither',
'just x',
'just y',
'just x with Interval',
'just y with Interval',
],
)
def test_input_validation_bounds(self, x_bounds, y_bounds):
"""Test that the __init__() method checks for valid bounds."""
with pytest.raises(ValueError, match='BoundingBox requires bounds'):
_ = BoundingBox(x_bounds, y_bounds)
@pytest.mark.input_validation
@pytest.mark.parametrize('inclusive', ['ss', [True, True, False], [1, 2]])
def test_input_validation_inclusive(self, inclusive):
"""Test that the __init__() method checks for valid inclusive value work."""
with pytest.raises(ValueError, match='inclusive must be'):
_ = BoundingBox([0, 1], [0, 1], inclusive)
@pytest.mark.parametrize(
('x_bounds', 'y_bounds', 'inclusive', 'expected'),
[
([0, 1], [0, 1], True, (True, True)),
([0, 1], [0, 1], False, (False, False)),
(Interval([0, 1], True), Interval([0, 1], True), True, (True, True)),
(Interval([0, 1], True), Interval([0, 1], True), False, (True, True)),
(Interval([0, 1], True), [0, 1], True, (True, True)),
([0, 1], Interval([0, 1], True), True, (True, True)),
(Interval([0, 1], True), [0, 1], False, (True, False)),
([0, 1], Interval([0, 1], True), False, (False, True)),
],
)
def test_init(self, x_bounds, y_bounds, inclusive, expected):
"""Test that the __init__() method is working."""
bbox = BoundingBox(x_bounds, y_bounds, inclusive)
assert bbox.x_bounds._inclusive == expected[0]
assert bbox.y_bounds._inclusive == expected[1]
# make sure the bounds were cloned
if isinstance(x_bounds, Interval):
assert bbox.x_bounds == x_bounds
bbox.adjust_bounds(x=2)
assert bbox.x_bounds != x_bounds
if isinstance(y_bounds, Interval):
assert bbox.y_bounds == y_bounds
bbox.adjust_bounds(y=2)
assert bbox.y_bounds != y_bounds
@pytest.mark.parametrize(
('value', 'inclusive', 'expected'),
[
([1, 1], True, True),
([1, 1], False, True),
([0, 0], True, True),
([0, 0], False, False),
],
ids=[
'inside box - inclusive',
'inside box - exclusive',
'on corner - inclusive',
'on corner - exclusive',
],
)
def test_contains(self, value, inclusive, expected):
"""Test that [x, y] in BoundingBox is working."""
bbox = BoundingBox([0, 10], [0, 10], inclusive)
assert (value in bbox) == expected
@pytest.mark.input_validation
@pytest.mark.parametrize(
'value',
[
[True, False],
'12',
12,
False,
],
ids=['list of Booleans', 'string of 2 numbers', 'two-digit integer', 'False'],
)
def test_contains_input_validation(self, value):
"""Test that input validation for `[x, y] in BoundingBox` is working."""
bbox = BoundingBox([0, 10], [0, 10])
with pytest.raises(ValueError, match='must be an iterable of 2 numeric values'):
_ = value in bbox
@pytest.mark.input_validation
@pytest.mark.parametrize('other', [1, True, Interval([0, 1])])
def test_eq_input_validation(self, other):
"""Test that input validation for the __eq__() method is working."""
bbox = BoundingBox([0, 10], [0, 10])
with pytest.raises(TypeError, match='only defined between BoundingBox objects'):
_ = bbox == other
def test_eq(self):
"""Test that the __eq__() method is working."""
limits = ([0, 10], [0, 10])
bbox1 = BoundingBox(*limits)
bbox2 = BoundingBox(*limits)
assert bbox1 == bbox2
bbox1.adjust_bounds(x=1)
assert bbox1 != bbox2
def test_repr(self):
"""Test that the __repr__() method is working."""
assert re.match(
'<BoundingBox>\n. x=<Interval .+>\n y=<Interval.+>',
repr(BoundingBox([0, 10], [0, 10])),
)
@pytest.mark.input_validation
@pytest.mark.parametrize(
('x', 'y'),
[
(True, True),
(None, True),
('s', None),
(None, 's'),
('s', 's'),
],
)
def test_adjust_bounds_input_validation(self, x, y):
"""Test that input validation on the adjust_bounds() method is working."""
bbox = BoundingBox([0, 10], [0, 10])
with pytest.raises(TypeError, match='value must be a numeric value'):
bbox.adjust_bounds(x, y)
@pytest.mark.parametrize(('x', 'y'), [(10, 10), (0, 10), (10, 0)])
def test_adjust_bounds(self, x, y):
"""Test that the adjust_bounds() method is working."""
start = [10, 90]
bbox = BoundingBox(start, start)
initial_range_x, initial_range_y = bbox.range
bbox.adjust_bounds(x, y)
new_range_x, new_range_y = bbox.range
assert new_range_x == initial_range_x + x
assert new_range_y == initial_range_y + y
@pytest.mark.parametrize(
('x', 'y'),
[
([10, 90], [500, 600]),
([500, 600], [10, 90]),
([10, 90], [10, 90]),
],
)
def test_align_aspect_ratio(self, x, y):
"""Test that the align_aspect_ratio() method is working."""
bbox = BoundingBox(x, y)
bbox.align_aspect_ratio()
assert pytest.approx(bbox.aspect_ratio) == 1
def test_clone(self):
"""Test that the clone() method is working."""
bbox1 = BoundingBox([0, 10], [5, 10])
bbox2 = bbox1.clone()
assert bbox1 == bbox2
bbox2.adjust_bounds(x=2, y=2)
assert bbox1 != bbox2
@pytest.mark.parametrize(
('x', 'y', 'expected'),
[
([10, 90], [500, 600], 0.8),
([500, 600], [10, 90], 1.25),
([0, 10], [5, 10], 2),
([10, 90], [10, 90], 1),
],
)
def test_aspect_ratio(self, x, y, expected):
"""Test that the aspect_ratio property is working."""
bbox = BoundingBox(x, y)
assert bbox.aspect_ratio == expected
def test_range(self):
"""Test that the range property is working."""
bbox = BoundingBox([0, 10], [5, 10])
assert bbox.range == (10, 5)
def test_center(self):
"""Test that the center property is working."""
bbox = BoundingBox([0, 10], [5, 10])
assert bbox.center == (5, 7.5)