|
1 | 1 | """Tests for search tasks."""
|
2 | 2 |
|
| 3 | +import mock |
3 | 4 | import pytest
|
4 | 5 |
|
5 | 6 | from django.urls import reverse
|
@@ -43,6 +44,48 @@ def test_search_query_recorded_when_results_not_zero(self, api_client):
|
43 | 44 | SearchQuery.objects.all().count() == 1
|
44 | 45 | ), 'there should be 1 obj since a search is made which returns one result.'
|
45 | 46 |
|
| 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 | + |
46 | 89 | def test_search_query_not_recorded_when_results_are_zero(self, api_client):
|
47 | 90 | """Test that search queries are not recorded when they have zero results."""
|
48 | 91 |
|
|
0 commit comments