From 4944876e8e895dde1a764126203d75e23fa6a67e Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 25 Jun 2018 11:21:21 +0800 Subject: [PATCH 1/2] ensure the correct behavior of non-requried field --- queryfilter/datetimefilter.py | 6 ++++ queryfilter/schemas.py | 4 +-- queryfilter/tests/test_datetimefilter.py | 35 ++++++++++++++++++++++++ setup.py | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/queryfilter/datetimefilter.py b/queryfilter/datetimefilter.py index 85d9569..a113266 100644 --- a/queryfilter/datetimefilter.py +++ b/queryfilter/datetimefilter.py @@ -42,6 +42,9 @@ def in_range(datum): else: to_compare = parse(datetime_string) + if not self.start and not self.end: + return False + if self.start and (to_compare < self.start): return False @@ -55,6 +58,9 @@ def in_range(datum): def _do_django_query(self, queryset): query_dict = dict() + if not self.start and not self.end: + return queryset.none() + if self.start: query_dict["{}__gte".format(self.field_name)] = self.start diff --git a/queryfilter/schemas.py b/queryfilter/schemas.py index 19a0e8d..7e84cf9 100644 --- a/queryfilter/schemas.py +++ b/queryfilter/schemas.py @@ -22,7 +22,7 @@ class TextFilterQueryType(graphene.InputObjectType): condition = graphene.Field( TextFilterTypes, default_value=TextFilterTypes.EQUALS.value ) - value = graphene.String(description="text to query", required=True) + value = graphene.String(description="text to query") class SelectStringFilterQueryType(graphene.InputObjectType): @@ -33,7 +33,7 @@ class SelectStringFilterQueryType(graphene.InputObjectType): ) values = graphene.List( - graphene.String, description="text list to query", required=True + graphene.String, description="text list to query" ) diff --git a/queryfilter/tests/test_datetimefilter.py b/queryfilter/tests/test_datetimefilter.py index 08d8100..a9ef1ed 100644 --- a/queryfilter/tests/test_datetimefilter.py +++ b/queryfilter/tests/test_datetimefilter.py @@ -166,3 +166,38 @@ def test_end_date_has_no_time(self): queryset = DatetimeFilterTestingModel.objects.all() result = date_filter.on_django_query(queryset) assert len(result) == 1 + + +@pytest.mark.django_db +class TestFilterWithoutValue(object): + + def test_end_date_has_no_time(self): + + datetime_datum = dateutil.parser.parse("2018-12-31T02:00:00+00:00") + + DatetimeFilterTestingModel.objects.create(datetime=datetime_datum) + test_data = [{ + FIELD_NAME: datetime_datum + }] + + date_filter = DatetimeRangeFilter(FIELD_NAME, { + "start": '', + "end": '', + }) + + result = date_filter.on_dicts(test_data) + assert len(result) == 0 + + queryset = DatetimeFilterTestingModel.objects.all() + result = date_filter.on_django_query(queryset) + assert len(result) == 0 + + date_filter = DatetimeRangeFilter(FIELD_NAME, { + }) + + result = date_filter.on_dicts(test_data) + assert len(result) == 0 + + queryset = DatetimeFilterTestingModel.objects.all() + result = date_filter.on_django_query(queryset) + assert len(result) == 0 diff --git a/setup.py b/setup.py index 22e72f2..9e9ee3c 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ setup( name="queryfilter", - version="0.4.3", + version="0.4.4", description=("Allow same query interface to be shared between Django ORM," "SQLAlchemy, and GraphQL backend."), long_description=open(os.path.join(source_root, "README.rst")).read(), From 910ecc709167a087e4b7ac0ae8d47d47793b5bd5 Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 25 Jun 2018 11:38:17 +0800 Subject: [PATCH 2/2] datetime better test name --- queryfilter/tests/test_datetimefilter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/queryfilter/tests/test_datetimefilter.py b/queryfilter/tests/test_datetimefilter.py index a9ef1ed..f375d89 100644 --- a/queryfilter/tests/test_datetimefilter.py +++ b/queryfilter/tests/test_datetimefilter.py @@ -171,7 +171,7 @@ def test_end_date_has_no_time(self): @pytest.mark.django_db class TestFilterWithoutValue(object): - def test_end_date_has_no_time(self): + def test_non_required_value_should_filter_nothing(self): datetime_datum = dateutil.parser.parse("2018-12-31T02:00:00+00:00")