Skip to content

Commit 56a9235

Browse files
committed
Rename ignore to non_strict
1 parent fbecfe4 commit 56a9235

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

abe/unittest.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,48 +44,50 @@ def assert_item_matches(self, real, sample):
4444
sample = normalize(sample)
4545
self.assertEqual(real, sample)
4646

47-
def assert_data_equal(self, real, sample, ignore=None):
47+
def assert_data_equal(self, real, sample, non_strict=None):
4848
"""
4949
Two elements are recursively equal
5050
51-
:param ignore:
52-
Names of fields to ignore
51+
:param non_strict:
52+
Names of fields to match non-strictly. In current implementation,
53+
only check for field presence.
5354
"""
54-
ignore = ignore or []
55+
non_strict = non_strict or []
5556
try:
5657
if isinstance(real, list):
5758
self.assertIsInstance(sample, list)
58-
self.assert_data_list_equal(real, sample, ignore)
59+
self.assert_data_list_equal(real, sample, non_strict)
5960
elif isinstance(real, dict):
6061
self.assertIsInstance(sample, dict)
61-
self.assert_data_dict_equal(real, sample, ignore)
62+
self.assert_data_dict_equal(real, sample, non_strict)
6263
else:
6364
self.assert_item_matches(real, sample)
6465
except AssertionError as exc:
6566
message = str(exc) + '\n{}\n{}\n\n'.format(real, sample)
6667
raise type(exc)(message)
6768

68-
def assert_data_dict_equal(self, real, sample, ignore=None):
69+
def assert_data_dict_equal(self, real, sample, non_strict=None):
6970
"""
7071
Two dicts are recursively equal without taking order into account
7172
"""
72-
ignore = ignore or []
73+
non_strict = non_strict or []
7374
self.assertEqual(
7475
len(real), len(sample),
7576
msg='Number of elements mismatch: {} != {}\n'.format(
7677
real.keys(), sample.keys())
7778
)
7879
for key in real:
79-
if key not in ignore:
80+
if key not in non_strict:
8081
self.assertIn(key, sample)
81-
inner_ignore = subkeys(ignore, key)
82-
self.assert_data_equal(real[key], sample[key], inner_ignore)
82+
inner_non_strict = subkeys(non_strict, key)
83+
self.assert_data_equal(
84+
real[key], sample[key], inner_non_strict)
8385

84-
def assert_data_list_equal(self, real, sample, ignore=None):
86+
def assert_data_list_equal(self, real, sample, non_strict=None):
8587
"""
8688
Two lists are recursively equal, including ordering.
8789
"""
88-
ignore = ignore or []
90+
non_strict = non_strict or []
8991
self.assertEqual(
9092
len(real), len(sample),
9193
msg='Number of elements mismatch: {} {}'.format(
@@ -95,7 +97,7 @@ def assert_data_list_equal(self, real, sample, ignore=None):
9597
exceptions = []
9698
for real_item, sample_item in zip(real, sample):
9799
try:
98-
self.assert_data_equal(real_item, sample_item, ignore)
100+
self.assert_data_equal(real_item, sample_item, non_strict)
99101
except AssertionError as exc:
100102
exceptions.append(exc)
101103

@@ -123,35 +125,44 @@ def assert_headers_contain(self, response_data, spec_data):
123125
"header {0}".format(expected_header)
124126
)
125127

126-
def assert_matches_request(self, sample_request, wsgi_request):
128+
def assert_matches_request(self, sample_request, wsgi_request,
129+
non_strict=None):
127130
"""
128131
Check that the sample request and wsgi request match.
129132
"""
130-
self.assertEqual(wsgi_request.META['PATH_INFO'], sample_request['url'])
131-
self.assertEqual(wsgi_request.META['REQUEST_METHOD'],
132-
sample_request['method'])
133+
non_strict = non_strict or []
133134

134-
if 'headers' in sample_request:
135+
if 'url' not in non_strict:
136+
self.assertEqual(wsgi_request.META['PATH_INFO'],
137+
sample_request['url'])
138+
if 'method' not in non_strict:
139+
self.assertEqual(wsgi_request.META['REQUEST_METHOD'],
140+
sample_request['method'])
141+
142+
if 'headers' in sample_request and 'headers' not in non_strict:
135143
self.assert_headers_contain(
136144
wsgi_request.META, sample_request['headers']
137145
)
138146

139-
if 'body' in sample_request:
147+
if 'body' in sample_request and 'body' not in non_strict:
140148
self.assert_data_equal(wsgi_request.POST, sample_request['body'])
141149

142150
def assert_matches_response(self, sample_response, wsgi_response,
143-
ignore=None):
151+
non_strict=None):
144152
"""
145153
Check that the sample response and wsgi response match.
146154
"""
147-
ignore = ignore or []
155+
non_strict = non_strict or []
148156
self.assertEqual(wsgi_response.status_code, sample_response.status)
149157
if 'body' in sample_response:
150158
response_parsed = wsgi_response.data
151159
self.assert_data_equal(
152-
response_parsed, sample_response.body, ignore)
160+
response_parsed, sample_response.body, non_strict)
153161

154-
def assert_matches_sample(self, path, label, response, ignore=None):
162+
def assert_matches_sample(
163+
self, path, label, response, non_strict_response=None,
164+
non_strict_request=None
165+
):
155166
"""
156167
Check a URL and response against a sample.
157168
@@ -162,15 +173,19 @@ def assert_matches_sample(self, path, label, response, ignore=None):
162173
:param response:
163174
The actual API response we want to match with the sample.
164175
It is assumed to be a Django Rest Framework response object
165-
:param ignore:
176+
:param non_strict:
166177
List of fields that will not be checked for strict matching.
167178
You can use this to include server-generated fields whose exact
168179
value you don't care about in your test, like ids, dates, etc.
169180
"""
170-
ignore = ignore or []
181+
non_strict_response = non_strict_response or []
182+
non_strict_request = non_strict_request or []
171183
sample = self.load_sample(path)
172184
sample_request = sample.examples[label].request
173185
sample_response = sample.examples[label].response
174186

175-
self.assert_matches_response(sample_response, response, ignore=ignore)
176-
self.assert_matches_request(sample_request, response.wsgi_request)
187+
self.assert_matches_response(
188+
sample_response, response, non_strict=non_strict_response)
189+
self.assert_matches_request(
190+
sample_request, response.wsgi_request,
191+
non_strict=non_strict_request)

abe/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ def normalize(data):
3232
return data
3333

3434

35-
def subkeys(ignore, key):
36-
new_keys = filter(lambda s: s.startswith(key + '.'), ignore)
35+
def subkeys(original, key):
36+
"""
37+
Takes a list of dot-hierarchical values and keeps only matching subkeys.
38+
39+
Example:
40+
41+
>>> subkeys(['one.two', 'one.three', 'four'], 'one')
42+
['two', 'three']
43+
"""
44+
new_keys = filter(lambda s: s.startswith(key + '.'), original)
3745
new_keys = list(map(lambda s: s[len(key) + 1:], new_keys))
3846
return new_keys

tests/test_assertions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ def test_non_strict_response_matches(self):
223223
}
224224

225225
self.assert_matches_response(
226-
self.sample_response, response, ignore=['id', 'url', 'author.url']
226+
self.sample_response, response,
227+
non_strict=['id', 'url', 'author.url']
227228
)
228229

229230
def test_non_strict_list_value_matches(self):
@@ -255,7 +256,7 @@ def test_non_strict_list_value_matches(self):
255256
}
256257

257258
self.assert_matches_response(
258-
sample, response, ignore=['contributors.id']
259+
sample, response, non_strict=['contributors.id']
259260
)
260261

261262

0 commit comments

Comments
 (0)