allow random addon sort to be used with promoted filter (#15469)

This commit is contained in:
Andrew Williamson 2020-09-11 10:57:22 +01:00 коммит произвёл GitHub
Родитель 02c352cbc2
Коммит 5648e2f8ed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 44 добавлений и 9 удалений

Просмотреть файл

@ -54,7 +54,7 @@ This endpoint allows you to search through public add-ons.
downloads Number of weekly downloads, descending.
hotness Hotness (average number of users progression), descending.
random Random ordering. Only available when no search query is
passed and when filtering to only return recommended add-ons.
passed and when filtering to only return promoted add-ons.
rating Bayesian rating, descending.
recommended Recommended add-ons above non-recommend add-ons. Only
available combined with another sort - ignored on its own.
@ -66,14 +66,11 @@ This endpoint allows you to search through public add-ons.
users Average number of daily users, descending.
============== ==========================================================
The new default behavior is to sort by relevance if a search query (``q``)
The default behavior is to sort by relevance if a search query (``q``)
is present; otherwise place recommended add-ons first, then non recommended
add-ons, then sorted by average daily users, descending. (``sort=recommended,users``).
This is the default on AMO dev server.
The default on AMO production currently is to sort by relevance if a search
query (``q``) is present; otherwise sort by number of weekly downloads, descending.
You can combine multiple parameters by separating them with a comma.
For instance, to sort search results by downloads and then by creation
date, use ``sort=downloads,created``. The only exception is the ``random``

Просмотреть файл

@ -915,7 +915,8 @@ class SortingFilter(BaseFilterBackend):
raise serializers.ValidationError(
'The "random" "sort" parameter can not be combined.')
# Second, for perf reasons it's only available when the 'featured'
# Second, for perf reasons it's only available when the 'featured',
# 'promoted'
# or 'recommended' param is present (to limit the number of
# documents we'll have to apply the random score to) and a search
# query is absent (to prevent clashing with the score functions
@ -924,6 +925,7 @@ class SortingFilter(BaseFilterBackend):
is_random_sort_available = (
(AddonFeaturedQueryParam.query_param in request.GET or
AddonPromotedQueryParam.query_param in request.GET or
AddonRecommendedQueryParam.query_param in request.GET) and
not search_query_param
)
@ -940,7 +942,8 @@ class SortingFilter(BaseFilterBackend):
else:
raise serializers.ValidationError(
'The "sort" parameter "random" can only be specified '
'when the "featured" or "recommended" parameter is '
'when the "featured", "promoted", or "recommended" '
'parameter is '
'also present, and the "q" parameter absent.')
# Sorting by relevance only makes sense with a query string

Просмотреть файл

@ -492,7 +492,8 @@ class TestSortingFilter(FilterTestsBase):
def test_sort_random_restrictions(self):
expected = ('The "sort" parameter "random" can only be specified when '
'the "featured" or "recommended" parameter is also '
'the "featured", "promoted", or "recommended" parameter '
'is also '
'present, and the "q" parameter absent.')
with self.assertRaises(serializers.ValidationError) as context:
@ -504,6 +505,11 @@ class TestSortingFilter(FilterTestsBase):
data={'q': 'something', 'featured': 'true', 'sort': 'random'})
assert context.exception.detail == [expected]
with self.assertRaises(serializers.ValidationError) as context:
self._filter(
data={'q': 'something', 'promoted': 'line', 'sort': 'random'})
assert context.exception.detail == [expected]
with self.assertRaises(serializers.ValidationError) as context:
self._filter(
data={'q': 'something', 'recommended': 'true',
@ -523,13 +529,24 @@ class TestSortingFilter(FilterTestsBase):
@freeze_time('2020-02-27')
def test_sort_random(self):
qs = self._filter(data={'promoted': 'recommended', 'sort': 'random'})
# Note: this test does not call AddonPromotedQueryParam so it won't
# apply the recommended filtering. That's tested below in
# TestCombinedFilter.test_filter_promoted_sort_random
assert qs['sort'] == ['_score']
assert qs['query']['function_score']['functions'] == [
{'random_score': {'seed': 737482}}
]
@freeze_time('2020-02-28')
def test_sort_random_recommended(self):
qs = self._filter(data={'recommended': 'true', 'sort': 'random'})
# Note: this test does not call AddonRecommendedQueryParam so it won't
# apply the recommended filtering. That's tested below in
# TestCombinedFilter.test_filter_recommended_sort_random
assert qs['sort'] == ['_score']
assert qs['query']['function_score']['functions'] == [
{'random_score': {'seed': 737482}}
{'random_score': {'seed': 737483}}
]
def test_sort_recommended_only(self):
@ -1079,6 +1096,24 @@ class TestCombinedFilter(FilterTestsBase):
{'random_score': {'seed': 737481}}
]
@freeze_time('2020-02-26')
def test_filter_promoted_sort_random(self):
qs = self._filter(data={'promoted': 'verified', 'sort': 'random'})
bool_ = qs['query']['bool']
assert 'must_not' not in bool_
filter_ = bool_['filter']
assert {'terms': {'status': amo.REVIEWED_STATUSES}} in filter_
assert {'exists': {'field': 'current_version'}} in filter_
assert {'term': {'is_disabled': False}} in filter_
assert qs['sort'] == ['_score']
assert bool_['must'][0]['function_score']['functions'] == [
{'random_score': {'seed': 737481}}
]
@freeze_time('2020-02-26')
def test_filter_recommended_sort_random(self):
qs = self._filter(data={'recommended': 'true', 'sort': 'random'})