diff --git a/CHANGELOG.md b/CHANGELOG.md index bbff050..c02873d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/deepcompare/__init__.py b/deepcompare/__init__.py index 631a3e2..5e9d086 100644 --- a/deepcompare/__init__.py +++ b/deepcompare/__init__.py @@ -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: @@ -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. diff --git a/tests/test_compare_flat.py b/tests/test_compare_flat.py index bbc9a75..3363c66 100644 --- a/tests/test_compare_flat.py +++ b/tests/test_compare_flat.py @@ -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, ]) diff --git a/tests/test_partial_compare_flat.py b/tests/test_partial_compare_flat.py index 43fd2b1..5cb71ee 100644 --- a/tests/test_partial_compare_flat.py +++ b/tests/test_partial_compare_flat.py @@ -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, ])