Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Oct 27, 2023
1 parent 9cfb08b commit 464a79b
Showing 1 changed file with 240 additions and 0 deletions.
240 changes: 240 additions & 0 deletions api/test/unit/controllers/test_search_controller_search_query.py
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"}}
],
}

0 comments on commit 464a79b

Please sign in to comment.