Skip to content

Commit

Permalink
Merge pull request #28 from iCHEF/feature/date-onnly-queryfilter
Browse files Browse the repository at this point in the history
ensure the correct behavior of non-required field
  • Loading branch information
tim authored Jun 25, 2018
2 parents 2d60499 + 910ecc7 commit 1686263
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions queryfilter/datetimefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions queryfilter/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
)


Expand Down
35 changes: 35 additions & 0 deletions queryfilter/tests/test_datetimefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_non_required_value_should_filter_nothing(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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1686263

Please sign in to comment.