Skip to content

Commit

Permalink
Merge pull request #35 from ScottFreeCode/v1.4.0/fix-array-verify
Browse files Browse the repository at this point in the history
List (& dict) comparison tests/fixes
  • Loading branch information
shanejansen authored Sep 14, 2021
2 parents 7f4593d + 4c081b5 commit 9d27d34
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 13 deletions.
129 changes: 129 additions & 0 deletions tests/helpers/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,105 @@ def test_matches_MatchingANYWithNestedList_ReturnsTrue(self):
# Then
self.assertTrue(result)

def test_matches_MatchingListOfStrings_ReturnsTrue(self):
# Given
expected = ['foo']
actual = ['foo']

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_MatchingListOfDicts_ReturnsTrue(self):
# Given
expected = [{'foo': 'bar'}]
actual = [{'foo': 'bar'}]

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_MatchingListOfLists_ReturnsTrue(self):
# Given
expected = [['foo']]
actual = [['foo']]

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_MatchingListOfStringsOutOfOrder_ReturnsTrue(self):
# Given
expected = ['foo', 'bar']
actual = ['bar', 'foo']

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_MatchingListOfDictsOutOfOrder_ReturnsTrue(self):
# Given
expected = [{'foo': 'bar'}, {'bazz': 'buzz'}]
actual = [{'bazz': 'buzz'}, {'foo': 'bar'}]

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_MatchingListOfListsOutOfOrder_ReturnsTrue(self):
# Given
expected = [['foo'], ['bar']]
actual = [['bar'], ['foo']]

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_MatchingListWithANY_ReturnsTrue(self):
# Given
expected = ['foo', validation.ANY, 'bar']
actual = ['foo', 'bazz', 'bar']

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_NonMatchingListWithANY_ReturnsFalse(self):
# Given
expected = ['foo', validation.ANY, 'bar']
actual = ['foo', 'bazz', 'buzz']

# When
result = validation.matches(expected, actual)

# Then
self.assertFalse(result)

def test_matches_MatchingListWithMultipleANY_ReturnsTrue(self):
# Given
expected = ['foo', validation.ANY, validation.ANY]
actual = ['foo', 'bazz', 'bar']

# When
result = validation.matches(expected, actual)

# Then
self.assertTrue(result)

def test_matches_NonMatchingStrings_ReturnsFalse(self):
# Given
expected = 'foo'
Expand Down Expand Up @@ -90,6 +189,36 @@ def test_matches_NonMatchingDict_ReturnsFalse(self):
# Then
self.assertFalse(result)

def test_matches_NonMatchingDictValueTypesExpectListActualDict_ReturnsFalse(self):
# Given
expected = {
'foo': ['bar']
}
actual = {
'foo': {'bar': 'bar'}
}

# When
result = validation.matches(expected, actual)

# Then
self.assertFalse(result)

def test_matches_NonMatchingDictValueTypesExpectDictActualList_ReturnsFalse(self):
# Given
expected = {
'foo': {'bar': 'bar'}
}
actual = {
'foo': ['bar']
}

# When
result = validation.matches(expected, actual)

# Then
self.assertFalse(result)

def test_matches_MatchingNestedDict_ReturnsTrue(self):
# Given
expected = {
Expand Down
21 changes: 8 additions & 13 deletions touchstone/helpers/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ def __equals(expected, actual) -> bool:
def __equals_list(expected: list, actual: list) -> bool:
if len(expected) != len(actual):
return False
for item in expected:
if isinstance(item, dict):
for actual_item in actual:
if isinstance(actual_item, dict) and not __equals_dict(item, actual_item):
return False
elif not __equals(item, actual):
for expected_item in expected:
expected_item_in_actual = False
for actual_item in actual:
if matches(expected_item, actual_item, quiet = True):
expected_item_in_actual = True
break
if not expected_item_in_actual:
return False
return True

Expand All @@ -26,13 +27,7 @@ def __equals_dict(expected: dict, actual: dict) -> bool:
for k, v in expected.items():
if k not in actual:
return False
if isinstance(v, dict):
if not __equals_dict(v, actual[k]):
return False
elif isinstance(v, list):
if not __equals_list(v, actual[k]):
return False
elif not __equals(v, actual[k]):
if not matches(v, actual[k], quiet = True):
return False
return True

Expand Down

0 comments on commit 9d27d34

Please sign in to comment.