Skip to content

Commit 294f6bd

Browse files
committed
add test
1 parent 58ecb39 commit 294f6bd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

readthedocs/search/tests/test_search_tasks.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for search tasks."""
22

3+
import mock
34
import pytest
45

56
from django.urls import reverse
@@ -43,6 +44,48 @@ def test_search_query_recorded_when_results_not_zero(self, api_client):
4344
SearchQuery.objects.all().count() == 1
4445
), 'there should be 1 obj since a search is made which returns one result.'
4546

47+
def test_partial_queries_are_not_recorded(self, api_client):
48+
"""Test if partial queries are not recorded."""
49+
50+
assert (
51+
SearchQuery.objects.all().count() == 0
52+
), 'no SearchQuery should be present if there is no search made.'
53+
54+
time = timezone.now()
55+
search_params = { 'q': 'stack', 'project': 'docs', 'version': 'latest' }
56+
57+
with mock.patch('django.utils.timezone.now') as test_time:
58+
test_time.return_value = time
59+
resp = api_client.get(self.url, search_params)
60+
assert resp.status_code, 200
61+
62+
assert (
63+
SearchQuery.objects.all().count() == 1
64+
), 'one SearchQuery should be present'
65+
66+
# update the time and the search query and make another search request
67+
time = time + timezone.timedelta(seconds=2)
68+
search_params['q'] = 'stack over'
69+
with mock.patch('django.utils.timezone.now') as test_time:
70+
test_time.return_value = time
71+
resp = api_client.get(self.url, search_params)
72+
assert resp.status_code, 200
73+
74+
# update the time and the search query and make another search request
75+
time = time + timezone.timedelta(seconds=2)
76+
search_params['q'] = 'stack overflow'
77+
with mock.patch('django.utils.timezone.now') as test_time:
78+
test_time.return_value = time
79+
resp = api_client.get(self.url, search_params)
80+
assert resp.status_code, 200
81+
82+
assert (
83+
SearchQuery.objects.all().count() == 1
84+
), 'one SearchQuery should be present'
85+
assert (
86+
SearchQuery.objects.all().first().query == 'stack overflow'
87+
), 'one SearchQuery should be there because partial queries gets updated'
88+
4689
def test_search_query_not_recorded_when_results_are_zero(self, api_client):
4790
"""Test that search queries are not recorded when they have zero results."""
4891

0 commit comments

Comments
 (0)