Skip to content

Commit

Permalink
test: refactor tests for where filter in include #259
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 3, 2021
1 parent 38d17a5 commit d6c62ea
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 63 deletions.
2 changes: 2 additions & 0 deletions datagateway_api/src/search_api/query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def get_include_filter(filter_input):
)

for scope_query_filter in scope_query_filters:
# TODO - fix for AND boolean operator test needs to go in here
# Search through scope_query_filter to find where filters via nested
if isinstance(scope_query_filter, SearchAPIWhereFilter):
scope_query_filter.field = (
f"{included_entity}.{scope_query_filter.field}"
Expand Down
234 changes: 171 additions & 63 deletions test/search_api/test_search_api_query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,8 @@ def test_valid_include_filter(

@pytest.mark.parametrize(
"test_request_filter, test_entity_name, expected_length"
", expected_included_entities, expected_where_filter_data",
", expected_included_entities, expected_where_filter_data"
", expected_nested_wheres",
[
pytest.param(
{
Expand All @@ -1276,8 +1277,9 @@ def test_valid_include_filter(
},
"datasets",
2,
[["parameters"], []],
[[], ["parameters.name", "eq", "My parameter", "and"]],
[["parameters"]],
[SearchAPIWhereFilter("parameters.name", "My parameter", "eq")],
"",
id="Property value with no operator",
),
pytest.param(
Expand All @@ -1293,8 +1295,9 @@ def test_valid_include_filter(
},
"datasets",
2,
[["parameters"], []],
[[], ["parameters.name", "ne", "My parameter", "and"]],
[["parameters"]],
[SearchAPIWhereFilter("parameters.name", "My parameter", "ne")],
"",
id="Property value with operator",
),
pytest.param(
Expand All @@ -1310,8 +1313,13 @@ def test_valid_include_filter(
},
"datasets",
2,
[["files"], []],
[[], ["files.name", "eq", "file1", "or"]],
[["files"]],
[],
[
NestedWhereFilters(
[], [SearchAPIWhereFilter("name", "file1", "eq")], "or",
),
],
id="Text operator on defined field mapping to single field",
),
pytest.param(
Expand All @@ -1327,8 +1335,9 @@ def test_valid_include_filter(
},
"datasets",
1,
[["parameters"], []],
[[], []],
[["parameters"]],
[],
[],
id="Text operator on non-defined field",
),
pytest.param(
Expand All @@ -1343,12 +1352,15 @@ def test_valid_include_filter(
},
},
"datasets",
3,
[["documents"], []],
2,
[["documents"]],
[],
[
[],
["documents.title", "eq", "document1", "or"],
["documents.summary", "eq", "document1", "or"],
NestedWhereFilters(
[SearchAPIWhereFilter("documents.title", "document1", "eq")],
[SearchAPIWhereFilter("documents.summary", "document1", "eq")],
"or",
),
],
id="Text operator on defined field mapping to multiple field",
),
Expand All @@ -1371,12 +1383,19 @@ def test_valid_include_filter(
},
},
"datasets",
3,
[["documents"], [], []],
2,
[["documents"]],
[],
[
[],
["documents.summary", "eq", "My Test Summary", "and"],
["documents.title", "eq", "Test title", "and"],
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.summary", "My Test Summary", "eq"
)
],
[SearchAPIWhereFilter("documents.title", "Test title", "eq")],
"and",
),
],
id="AND boolean operator",
),
Expand All @@ -1388,7 +1407,7 @@ def test_valid_include_filter(
"relation": "documents",
"scope": {
"where": {
"and": [
"or": [
{"summary": "My Test Summary"},
{"title": "Test title"},
],
Expand All @@ -1399,12 +1418,19 @@ def test_valid_include_filter(
},
},
"datasets",
3,
[["documents"], [], []],
2,
[["documents"]],
[],
[
[],
["documents.summary", "eq", "My Test Summary", "and"],
["documents.title", "eq", "Test title", "and"],
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.summary", "My Test Summary", "eq"
)
],
[SearchAPIWhereFilter("documents.title", "Test title", "eq")],
"or",
),
],
id="OR boolean operator",
),
Expand Down Expand Up @@ -1447,16 +1473,56 @@ def test_valid_include_filter(
},
},
"datasets",
7,
[["documents"], [], [], [], [], [], []],
2,
[["documents"]],
[],
[
[],
["documents.summary", "eq", "My Test Summary", "and"],
["documents.title", "like", "Test title", "and"],
["documents.pid", "eq", "Test pid", "and"],
["documents.type", "eq", "Test type", "and"],
["documents.doi", "eq", "Test doi", "or"],
["documents.license", "like", "Test license", "or"],
NestedWhereFilters(
[
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.summary", "My Test Summary", "eq"
)
],
[
SearchAPIWhereFilter(
"documents.title", "Test title", "like"
)
],
"and",
),
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.pid", "Test pid", "eq"
)
],
[
SearchAPIWhereFilter(
"documents.type", "Test type", "eq"
)
],
"and",
),
],
[
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.doi", "Test doi", "eq"
)
],
[
SearchAPIWhereFilter(
"documents.license", "Test license", "like"
)
],
"or",
),
],
"and",
),
],
id="Nested AND boolean operator",
),
Expand Down Expand Up @@ -1499,16 +1565,56 @@ def test_valid_include_filter(
},
},
"datasets",
7,
[["documents"], [], [], [], [], [], []],
2,
[["documents"]],
[],
[
[],
["documents.summary", "eq", "My Test Summary", "and"],
["documents.title", "like", "Test title", "and"],
["documents.pid", "eq", "Test pid", "and"],
["documents.type", "eq", "Test type", "and"],
["documents.doi", "eq", "Test doi", "or"],
["documents.license", "like", "Test license", "or"],
NestedWhereFilters(
[
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.summary", "My Test Summary", "eq"
)
],
[
SearchAPIWhereFilter(
"documents.title", "Test title", "like"
)
],
"and",
),
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.pid", "Test pid", "eq"
)
],
[
SearchAPIWhereFilter(
"documents.type", "Test type", "eq"
)
],
"and",
),
],
[
NestedWhereFilters(
[
SearchAPIWhereFilter(
"documents.doi", "Test doi", "eq"
)
],
[
SearchAPIWhereFilter(
"documents.license", "Test license", "like"
)
],
"or",
),
],
"or",
),
],
id="Nested OR boolean operator",
),
Expand All @@ -1529,13 +1635,12 @@ def test_valid_include_filter(
},
"datasets",
4,
[["parameters"], [], ["documents"], []],
[["parameters"], ["documents"]],
[
[],
["parameters.name", "eq", "My parameter", "and"],
[],
["documents.title", "eq", "Document title", "and"],
SearchAPIWhereFilter("parameters.name", "My parameter", "eq"),
SearchAPIWhereFilter("documents.title", "Document title", "eq"),
],
[],
id="Multiple related models",
),
pytest.param(
Expand All @@ -1561,13 +1666,14 @@ def test_valid_include_filter(
},
"documents",
4,
[["datasets"], [], ["datasets.instrument"], []],
[["datasets"], ["datasets.instrument"]],
[
[],
["datasets.title", "eq", "Dataset 1", "and"],
[],
["datasets.instrument.name", "eq", "Instrument 1", "and"],
SearchAPIWhereFilter("datasets.title", "Dataset 1", "eq"),
SearchAPIWhereFilter(
"datasets.instrument.name", "Instrument 1", "eq"
),
],
[],
id="Nested related models",
),
],
Expand All @@ -1579,24 +1685,26 @@ def test_valid_include_filter_with_where_filter_in_scope(
expected_length,
expected_included_entities,
expected_where_filter_data,
expected_nested_wheres,
):
# TODO
filters = SearchAPIQueryFilterFactory.get_query_filter(
test_request_filter, test_entity_name,
)

assert len(filters) == expected_length

for test_filter, included_entities, where_filter_data in zip(
filters, expected_included_entities, expected_where_filter_data,
):
for test_filter in filters:
if isinstance(test_filter, SearchAPIIncludeFilter):
assert test_filter.included_filters == included_entities
for expected_include in expected_included_entities:
assert test_filter.included_filters == expected_include
expected_included_entities.remove(expected_include)
if isinstance(test_filter, NestedWhereFilters):
for expected_nested in expected_nested_wheres:
assert repr(test_filter) == repr(expected_nested)
expected_nested_wheres.remove(expected_nested)
if isinstance(test_filter, SearchAPIWhereFilter):
assert test_filter.field == where_filter_data[0]
assert test_filter.operation == where_filter_data[1]
assert test_filter.value == where_filter_data[2]
assert test_filter.boolean_operator == where_filter_data[3]
for expected_where in expected_where_filter_data:
assert repr(test_filter) == repr(expected_where)
expected_where_filter_data.remove(expected_where)

@pytest.mark.parametrize(
"test_request_filter, test_entity_name, expected_length"
Expand Down

0 comments on commit d6c62ea

Please sign in to comment.