-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
240 additions
and
0 deletions.
There are no files selected for viewing
240 changes: 240 additions & 0 deletions
240
api/test/unit/controllers/test_search_controller_search_query.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
from django.core.cache import cache | ||
|
||
import pytest | ||
|
||
from api.controllers import search_controller | ||
|
||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_create_search_query_empty(media_type_config): | ||
serializer = media_type_config.search_request_serializer(data={}) | ||
serializer.is_valid() | ||
search_query = search_controller.create_search_query(serializer) | ||
actual_query_clauses = search_query.to_dict()["bool"] | ||
|
||
assert actual_query_clauses == { | ||
"must_not": [{"term": {"mature": True}}], | ||
"must": [{"match_all": {}}], | ||
"should": [ | ||
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}} | ||
], | ||
} | ||
|
||
|
||
def test_create_search_query_empty_no_ranking(media_type_config, settings): | ||
settings.USE_RANK_FEATURES = False | ||
serializer = media_type_config.search_request_serializer(data={}) | ||
serializer.is_valid() | ||
search_query = search_controller.create_search_query(serializer) | ||
actual_query_clauses = search_query.to_dict()["bool"] | ||
|
||
assert actual_query_clauses == { | ||
"must_not": [{"term": {"mature": True}}], | ||
"must": [{"match_all": {}}], | ||
} | ||
|
||
|
||
def test_create_search_query_q_search_no_filters(media_type_config): | ||
serializer = media_type_config.search_request_serializer(data={"q": "cat"}) | ||
serializer.is_valid() | ||
search_query = search_controller.create_search_query(serializer) | ||
actual_query_clauses = search_query.to_dict()["bool"] | ||
|
||
assert actual_query_clauses == { | ||
"must_not": [{"term": {"mature": True}}], | ||
"must": [ | ||
{ | ||
"simple_query_string": { | ||
"default_operator": "AND", | ||
"fields": ["title", "description", "tags.name"], | ||
"query": "cat", | ||
} | ||
} | ||
], | ||
"should": [ | ||
{ | ||
"simple_query_string": { | ||
"boost": 10000, | ||
"fields": ["title"], | ||
"query": "cat", | ||
} | ||
}, | ||
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}}, | ||
], | ||
} | ||
|
||
|
||
def test_create_search_query_q_search_with_quotes_adds_exact_suffix(media_type_config): | ||
serializer = media_type_config.search_request_serializer( | ||
data={"q": '"The cutest cat"'} | ||
) | ||
serializer.is_valid() | ||
search_query = search_controller.create_search_query(serializer) | ||
actual_query_clauses = search_query.to_dict()["bool"] | ||
|
||
assert actual_query_clauses == { | ||
"must_not": [{"term": {"mature": True}}], | ||
"must": [ | ||
{ | ||
"simple_query_string": { | ||
"default_operator": "AND", | ||
"fields": ["title", "description", "tags.name"], | ||
"query": '"The cutest cat"', | ||
"quote_field_suffix": ".exact", | ||
} | ||
} | ||
], | ||
"should": [ | ||
{ | ||
"simple_query_string": { | ||
"boost": 10000, | ||
"fields": ["title"], | ||
"query": "The cutest cat", | ||
} | ||
}, | ||
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}}, | ||
], | ||
} | ||
|
||
|
||
def test_create_search_query_q_search_with_filters(image_media_type_config): | ||
serializer = image_media_type_config.search_request_serializer( | ||
data={ | ||
"q": "cat", | ||
"license": "by-nc", | ||
"aspect_ratio": "wide", | ||
# this is a deprecated param, and it doesn't work because it doesn't exist in the serializer | ||
"categories": "digitized_artwork", | ||
"category": "illustration", | ||
"excluded_source": "flickr", | ||
"unstable__authority": True, | ||
"unstable__authority_boost": "2.5", | ||
"unstable__include_sensitive_results": True, | ||
} | ||
) | ||
serializer.is_valid() | ||
search_query = search_controller.create_search_query(serializer) | ||
actual_query_clauses = search_query.to_dict()["bool"] | ||
|
||
assert actual_query_clauses == { | ||
"filter": [ | ||
{"terms": {"category": ["illustration"]}}, | ||
{"terms": {"license": ["by-nc"]}}, | ||
{"terms": {"aspect_ratio": ["wide"]}}, | ||
], | ||
"must_not": [{"terms": {"source": ["flickr"]}}], | ||
"must": [ | ||
{ | ||
"simple_query_string": { | ||
"default_operator": "AND", | ||
"fields": ["title", "description", "tags.name"], | ||
"query": "cat", | ||
} | ||
} | ||
], | ||
"should": [ | ||
{ | ||
"simple_query_string": { | ||
"boost": 10000, | ||
"fields": ["title"], | ||
"query": "cat", | ||
} | ||
}, | ||
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}}, | ||
{"rank_feature": {"boost": 25000, "field": "authority_boost"}}, | ||
], | ||
} | ||
|
||
|
||
def test_create_search_query_non_q_query(image_media_type_config): | ||
serializer = image_media_type_config.search_request_serializer( | ||
data={ | ||
"creator": "Artist From Openverse", | ||
"title": "kitten🐱", | ||
"tags": "cute", | ||
} | ||
) | ||
serializer.is_valid() | ||
search_query = search_controller.create_search_query(serializer) | ||
actual_query_clauses = search_query.to_dict()["bool"] | ||
|
||
assert actual_query_clauses == { | ||
"must_not": [{"term": {"mature": True}}], | ||
"must": [ | ||
{ | ||
"simple_query_string": { | ||
"fields": ["creator"], | ||
"query": "Artist From Openverse", | ||
} | ||
}, | ||
{"simple_query_string": {"fields": ["title"], "query": "kitten🐱"}}, | ||
{"simple_query_string": {"fields": ["tags.name"], "query": "cute"}}, | ||
], | ||
"should": [ | ||
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}}, | ||
], | ||
} | ||
|
||
|
||
def test_create_search_query_q_search_license_license_type_creates_2_terms_filters( | ||
image_media_type_config, | ||
): | ||
serializer = image_media_type_config.search_request_serializer( | ||
data={ | ||
"license": "by-nc", | ||
"license_type": "commercial", | ||
} | ||
) | ||
serializer.is_valid() | ||
search_query = search_controller.create_search_query(serializer) | ||
actual_query_clauses = search_query.to_dict()["bool"] | ||
|
||
first_license_terms_filter = actual_query_clauses["filter"][0] | ||
second_license_terms_filter_licenses = sorted( | ||
actual_query_clauses["filter"][1]["terms"]["license"] | ||
) | ||
# Extracting these to make comparisons not dependent on list order. | ||
assert first_license_terms_filter == {"terms": {"license": ["by-nc"]}} | ||
assert second_license_terms_filter_licenses == [ | ||
"by", | ||
"by-nd", | ||
"by-sa", | ||
"cc0", | ||
"pdm", | ||
"sampling+", | ||
] | ||
actual_query_clauses.pop("filter", None) | ||
|
||
assert actual_query_clauses == { | ||
"must_not": [{"term": {"mature": True}}], | ||
"must": [{"match_all": {}}], | ||
"should": [ | ||
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}}, | ||
], | ||
} | ||
|
||
|
||
def test_create_search_query_empty_with_dynamically_excluded_providers( | ||
image_media_type_config, | ||
): | ||
excluded = {"provider_identifier": "flickr"} | ||
cache.set(key="filtered_providers", timeout=1, value=[excluded]) | ||
|
||
serializer = image_media_type_config.search_request_serializer(data={}) | ||
serializer.is_valid() | ||
|
||
search_query = search_controller.create_search_query(serializer) | ||
|
||
actual_query_clauses = search_query.to_dict()["bool"] | ||
assert actual_query_clauses == { | ||
"must_not": [ | ||
{"term": {"mature": True}}, | ||
{"terms": {"provider": [excluded["provider_identifier"]]}}, | ||
], | ||
"must": [{"match_all": {}}], | ||
"should": [ | ||
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}} | ||
], | ||
} |