Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure the correct behavior of non-required field #28

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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