From 243ab5c351c16ad443c1518a5fa712c4929e858d Mon Sep 17 00:00:00 2001 From: BJ Hargrave Date: Thu, 27 Jun 2024 13:27:59 -0400 Subject: [PATCH] extracting: Support filter=None and sort=None Allow filter and sort kwargs to have values None which are equivalent to not specifying the kwargs. sort=None already worked in this manner. A small code change was needed to support filter=None. Test methods are added. Signed-off-by: BJ Hargrave --- assertpy/extracting.py | 2 +- tests/test_extracting.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/assertpy/extracting.py b/assertpy/extracting.py index 98d4ad8..257fe30 100644 --- a/assertpy/extracting.py +++ b/assertpy/extracting.py @@ -209,7 +209,7 @@ def _filter(x): return True elif callable(kwargs['filter']): return kwargs['filter'](x) - return False + return kwargs['filter'] is None return True def _sort(x): diff --git a/tests/test_extracting.py b/tests/test_extracting.py index b443064..f4aa1ba 100644 --- a/tests/test_extracting.py +++ b/tests/test_extracting.py @@ -150,6 +150,10 @@ def test_extracting_filter(): assert_that(users).extracting('user', filter=lambda x: x['age'] < 10).is_empty() +def test_extracting_filter_none(): + assert_that(users).extracting('user', filter=None).is_equal_to(['Fred', 'Bob', 'Johnny']) + + def test_extracting_filter_bad_type(): assert_that(users).extracting('user', filter=123).is_equal_to([]) @@ -231,6 +235,10 @@ def test_extracting_sort(): assert_that(users).extracting('user', sort=lambda x: -x['age']).is_equal_to(['Bob', 'Fred', 'Johnny']) +def test_extracting_sort_none(): + assert_that(users).extracting('user', sort=None).is_equal_to(['Fred', 'Bob', 'Johnny']) + + def test_extracting_sort_ignore_bad_type(): assert_that(users).extracting('user', sort=123).is_equal_to(['Fred', 'Bob', 'Johnny'])