Skip to content

Commit

Permalink
SIANXKE-114: Fixed comparing string types
Browse files Browse the repository at this point in the history
  • Loading branch information
beachmachine committed Sep 28, 2021
1 parent f233bd0 commit 6b0c9a8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.1] - 2021-09-28
### Fixed
- Fixed compare for `str` types.

## [1.0.0] - 2021-09-27
### Added
- Method to deep compare data structures containing `dict`, `list` and `tuple` types.
- Method to partially deep compare data structures containing `dict`, `list` and `tuple` types.

[Unreleased]: https://github.com/anexia-it/python-deepcompare/compare/1.0.0...HEAD
[Unreleased]: https://github.com/anexia-it/python-deepcompare/compare/1.0.1...HEAD
[1.0.1]: https://github.com/anexia-it/python-deepcompare/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/anexia-it/python-deepcompare/releases/tag/1.0.0
4 changes: 3 additions & 1 deletion deepcompare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

_LIST_TYPES = (abc.Sequence, )
_DICT_TYPES = (abc.Mapping, )
_STR_TYPES = (str, bytes, )


def compare(haystack: Any, subset: Any, strict: bool = False) -> bool:
Expand Down Expand Up @@ -63,7 +64,8 @@ def _compare(haystack: Any, subset: Any, partial: bool, strict: bool) -> bool:

# if we compare compare two list types, we check each value of the haystack object to be equal to the
# subset object. if we are working in partial mode, we ignore if the subset list is shorter than the haystack list.
elif isinstance(haystack, _LIST_TYPES) and isinstance(subset, _LIST_TYPES):
elif isinstance(haystack, _LIST_TYPES) and not isinstance(haystack, _STR_TYPES) \
and isinstance(subset, _LIST_TYPES) and not isinstance(subset, _STR_TYPES):
return _compare_sequence(haystack, subset, partial, strict)

# for any other type, we just compare the two values.
Expand Down
25 changes: 25 additions & 0 deletions tests/test_compare_flat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import deepcompare


def test_compare_str_with_str():
# test full equality
assert deepcompare.compare('abc', 'abc')

# test non-equality
assert not deepcompare.compare('abc', 'a')
assert not deepcompare.compare('abc', 'ab')
assert not deepcompare.compare('abc', 'abcd')


def test_compare_str_in_list_with_str_in_list():
# test full equality
assert deepcompare.compare(['a', 'b', 'c', ], ['a', 'b', 'c', ])

# test partial equality
assert not deepcompare.compare(['a', 'b', 'c', ], ['a', ])
assert not deepcompare.compare(['a', 'b', 'c', ], ['a', 'b', ])
assert not deepcompare.compare(['a', 'b', 'c', ], ['a', 'c', ])

# test non-equality
assert not deepcompare.compare(['a', 'b', 'c', ], ['c', 'b', 'a', ])
assert not deepcompare.compare(['a', 'b', 'c', ], ['a', 'c', 'b', ])
assert not deepcompare.compare(['a', 'b', 'c', ], ['a', 'b', 'c', 'd', ])


def test_compare_list_with_list():
# test full equality
assert deepcompare.compare([1, 2, 3, ], [1, 2, 3, ])
Expand Down
25 changes: 25 additions & 0 deletions tests/test_partial_compare_flat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import deepcompare


def test_compare_str_with_str():
# test full equality
assert deepcompare.partial_compare('abc', 'abc')

# test non-equality
assert not deepcompare.partial_compare('abc', 'a')
assert not deepcompare.partial_compare('abc', 'ab')
assert not deepcompare.partial_compare('abc', 'abcd')


def test_compare_str_in_list_with_str_in_list():
# test full equality
assert deepcompare.partial_compare(['a', 'b', 'c', ], ['a', 'b', 'c', ])

# test partial equality
assert deepcompare.partial_compare(['a', 'b', 'c', ], ['a', ])
assert deepcompare.partial_compare(['a', 'b', 'c', ], ['a', 'b', ])
assert deepcompare.partial_compare(['a', 'b', 'c', ], ['a', 'c', ])

# test non-equality
assert not deepcompare.partial_compare(['a', 'b', 'c', ], ['c', 'b', 'a', ])
assert not deepcompare.partial_compare(['a', 'b', 'c', ], ['a', 'c', 'b', ])
assert not deepcompare.partial_compare(['a', 'b', 'c', ], ['a', 'b', 'c', 'd', ])


def test_compare_list_with_list():
# test full equality
assert deepcompare.partial_compare([1, 2, 3, ], [1, 2, 3, ])
Expand Down

0 comments on commit 6b0c9a8

Please sign in to comment.