Skip to content

Commit 50d4e1a

Browse files
committed
Ensure all sample keys are in real response data.
- Includes additional coverage of strict/non-strict mismatching
1 parent ec6bc59 commit 50d4e1a

File tree

2 files changed

+154
-41
lines changed

2 files changed

+154
-41
lines changed

abe/unittest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def assert_data_dict_equal(self, real, sample, non_strict=None):
7676
msg='Number of elements mismatch: {} != {}\n'.format(
7777
real.keys(), sample.keys())
7878
)
79-
for key in real:
79+
for key in sample:
80+
self.assertIn(key, real)
8081
if key not in non_strict:
81-
self.assertIn(key, sample)
8282
inner_non_strict = subkeys(non_strict, key)
8383
self.assert_data_equal(
8484
real[key], sample[key], inner_non_strict)

tests/test_assertions.py

Lines changed: 152 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -167,32 +167,32 @@ def test_assertion_error_if_post_data_mismatch(self):
167167
)
168168

169169

170+
def _abe_wrap_response(response):
171+
abe_mock = AbeMock({
172+
"method": "POST",
173+
"url": "/resource/",
174+
"examples": {
175+
"0": {"response": response}
176+
}
177+
})
178+
return abe_mock.examples['0'].response
179+
180+
170181
class TestAssertMatchesResponse(TestCase, AbeTestMixin):
171182

172-
def setUp(self):
173-
abe_mock = AbeMock({
174-
"method": "POST",
175-
"url": "/resource/",
176-
"examples": {
177-
"OK": {
178-
"response": {
179-
"status": 201,
180-
"body": {
181-
"id": 12,
182-
"name": "My Resource",
183-
"url": "http://example.com/resource/12",
184-
"author": {
185-
"name": "Slough",
186-
"url": "http://example.com/user/25"
187-
}
188-
}
189-
}
183+
def test_response_matches_strictly(self):
184+
sample = _abe_wrap_response({
185+
"status": 201,
186+
"body": {
187+
"id": 12,
188+
"name": "My Resource",
189+
"url": "http://example.com/resource/12",
190+
"author": {
191+
"name": "Slough",
192+
"url": "http://example.com/user/25"
190193
}
191194
}
192195
})
193-
self.sample_response = abe_mock.examples['OK'].response
194-
195-
def test_response_matches_strictly(self):
196196
response = Mock()
197197
response.status_code = 201
198198
response.data = {
@@ -206,10 +206,41 @@ def test_response_matches_strictly(self):
206206
}
207207

208208
self.assert_matches_response(
209-
self.sample_response, response
209+
sample, response
210+
)
211+
212+
def test_strict_response_mismatch(self):
213+
sample = _abe_wrap_response({
214+
"status": 201,
215+
"body": {
216+
"url": "http://example.com/resource/12"
217+
}
218+
})
219+
response = Mock()
220+
response.status_code = 201
221+
response.data = {
222+
"url": "http://example.com/resource/13"
223+
}
224+
225+
self.assertRaises(
226+
AssertionError,
227+
self.assert_matches_response,
228+
sample, response
210229
)
211230

212231
def test_non_strict_response_matches(self):
232+
sample = _abe_wrap_response({
233+
"status": 201,
234+
"body": {
235+
"id": 12,
236+
"name": "My Resource",
237+
"url": "http://example.com/resource/12",
238+
"author": {
239+
"name": "Slough",
240+
"url": "http://example.com/user/25"
241+
}
242+
}
243+
})
213244
response = Mock()
214245
response.status_code = 201
215246
response.data = {
@@ -223,29 +254,86 @@ def test_non_strict_response_matches(self):
223254
}
224255

225256
self.assert_matches_response(
226-
self.sample_response, response,
257+
sample, response,
227258
non_strict=['id', 'url', 'author.url']
228259
)
229260

230-
def test_non_strict_list_value_matches(self):
231-
abe_mock = AbeMock({
232-
"url": "/resource/",
233-
"method": "GET",
234-
"examples": {
235-
"OK": {
236-
"response": {
237-
"status": 200,
238-
"body": {
239-
"contributors": [
240-
{"name": "Jack", "id": 1},
241-
{"name": "Jill", "id": 2},
242-
]
243-
}
244-
}
261+
def test_non_strict_response_mismatch(self):
262+
sample = _abe_wrap_response({
263+
"status": 201,
264+
"body": {
265+
"author": {
266+
"name": "Some",
267+
"url": "http://example.com/user/1"
245268
}
246269
}
247270
})
248-
sample = abe_mock.examples['OK'].response
271+
response = Mock()
272+
response.status_code = 201
273+
response.data = {
274+
"author": {
275+
"name": "Something",
276+
"url": "http://testserver/user/25"
277+
}
278+
}
279+
280+
self.assertRaises(
281+
AssertionError,
282+
self.assert_matches_response,
283+
sample, response, non_strict=['author.url']
284+
)
285+
286+
def test_strict_response_checks_presence_of_sample_fields(self):
287+
sample = _abe_wrap_response({
288+
"status": 201,
289+
"body": {
290+
"id": 1,
291+
"name": "Some"
292+
}
293+
})
294+
response = Mock()
295+
response.status_code = 201
296+
response.data = {
297+
"name": "Some",
298+
}
299+
300+
self.assertRaises(
301+
AssertionError,
302+
self.assert_matches_response,
303+
sample, response
304+
)
305+
306+
def test_non_strict_response_checks_presence_of_sample_fields(self):
307+
sample = _abe_wrap_response({
308+
"status": 201,
309+
"body": {
310+
"id": 1,
311+
"name": "Some"
312+
}
313+
})
314+
response = Mock()
315+
response.status_code = 201
316+
response.data = {
317+
"name": "Some",
318+
"url": "http://testserver/user/25"
319+
}
320+
321+
self.assertRaises(
322+
AssertionError,
323+
self.assert_matches_response,
324+
sample, response, non_strict=['url']
325+
)
326+
327+
def test_non_strict_list_value_matches(self):
328+
sample = _abe_wrap_response({
329+
"status": 200,
330+
"body": {
331+
"contributors": [
332+
{"name": "Jack", "id": 1},
333+
{"name": "Jill", "id": 2},
334+
]
335+
}
336+
})
249337
response = Mock()
250338
response.status_code = 200
251339
response.data = {
@@ -259,6 +347,31 @@ def test_non_strict_list_value_matches(self):
259347
sample, response, non_strict=['contributors.id']
260348
)
261349

350+
def test_non_strict_list_value_mismatch(self):
351+
sample = _abe_wrap_response({
352+
"status": 200,
353+
"body": {
354+
"contributors": [
355+
{"name": "Jack", "id": 1},
356+
{"name": "Jill", "id": 2},
357+
]
358+
}
359+
})
360+
response = Mock()
361+
response.status_code = 200
362+
response.data = {
363+
"contributors": [
364+
{"name": "Joe", "id": 12},
365+
{"name": "Jill", "id": 23},
366+
]
367+
}
368+
369+
self.assertRaises(
370+
AssertionError,
371+
self.assert_matches_response,
372+
sample, response, non_strict=['contributors.id']
373+
)
374+
262375

263376
class TestFilenameInstantiation(TestCase):
264377

0 commit comments

Comments
 (0)