Skip to content

Commit 19c6f71

Browse files
committed
Merge pull request #6 from alexcouper/alex/order_matters
Add tests for ordering.
2 parents 0af2829 + 842cc60 commit 19c6f71

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

abe/unittest.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_sample_request(self, path, label):
3636

3737
def assert_data_equal(self, data1, data2):
3838
"""
39-
Two elements are recursively equal without taking order into account
39+
Two elements are recursively equal
4040
"""
4141
try:
4242
if isinstance(data1, list):
@@ -69,31 +69,27 @@ def assert_data_dict_equal(self, data1, data2):
6969

7070
def assert_data_list_equal(self, data1, data2):
7171
"""
72-
Two lists are recursively equal without taking order into account
72+
Two lists are recursively equal, including ordering.
7373
"""
7474
self.assertEqual(
7575
len(data1), len(data2),
7676
msg='Number of elements mismatch: {} {}'.format(
7777
data1, data2)
7878
)
7979

80-
data1_elements = copy(data1)
81-
for element2 in data2:
82-
fails, exceptions, found = [], [], False
83-
while data1_elements and not found:
84-
element = data1_elements.pop()
85-
try:
86-
self.assert_data_equal(element, element2)
87-
found = True
88-
except AssertionError as exc:
89-
exceptions.append(exc)
90-
fails.append(element)
91-
if not data1_elements:
92-
message = '\n*\n'.join(
93-
map(str, exceptions)
94-
)
95-
raise type(exceptions[0])(message)
96-
data1_elements.extend(fails)
80+
exceptions = []
81+
for element, element2 in zip(data1, data2):
82+
try:
83+
self.assert_data_equal(element, element2)
84+
except AssertionError as exc:
85+
exceptions.append(exc)
86+
87+
if exceptions:
88+
message = '\n*\n'.join(
89+
map(str, exceptions)
90+
)
91+
raise type(exceptions[0])(message)
92+
9793

9894
def assert_matches_sample(self, path, label, url, response):
9995
"""

tests/test_assertions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from unittest import TestCase
2+
3+
from abe.unittest import AbeTestMixin
4+
5+
6+
class TestDataListEqual(TestCase, AbeTestMixin):
7+
8+
9+
def test_simple_list_equality(self):
10+
self.assert_data_list_equal([1, 2], [1, 2])
11+
12+
def test_simple_list_equality_for_ordering(self):
13+
self.assertRaises(
14+
AssertionError,
15+
self.assert_data_list_equal, [3, 1, 2], [2, 3, 1]
16+
)
17+
18+
def test_list_equality_nested_items(self):
19+
self.assert_data_list_equal([[3, 1], 1, 2], [[3, 1], 1, 2])
20+
self.assert_data_list_equal(
21+
[[3, 1], {'foo': 'bar'}, 2],
22+
[[3, 1], {'foo': 'bar'}, 2]
23+
)
24+
self.assert_data_list_equal(
25+
[[3, 1], {'foo': [1, 2, 3]}, 2],
26+
[[3, 1], {'foo': [1, 2, 3]}, 2]
27+
)
28+
29+
def test_list_equality_ordering_nested_items(self):
30+
nested_examples = [
31+
([[3, 1], 1, 2], [[1, 3], 1, 2]),
32+
([[3, 1], {'foo': 'bar'}, 2], [[3, 1], {'foo': 'dee'}, 2]),
33+
([[3, 1], {'foo': [1, 2, 3]}, 2], [[3, 1], {'foo': [1, 3, 2]}, 2])
34+
]
35+
for data1, data2 in nested_examples:
36+
self.assertRaises(
37+
AssertionError,
38+
self.assert_data_list_equal, data1, data2
39+
)

0 commit comments

Comments
 (0)