Skip to content

Commit

Permalink
ensure the correct behavior of non-requried field
Browse files Browse the repository at this point in the history
  • Loading branch information
timtan committed Jun 25, 2018
1 parent b16a1fa commit 4944876
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_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
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 4944876

Please sign in to comment.