|
1 |
| -import random |
2 |
| -import string |
3 |
| - |
4 | 1 | import pytest
|
5 |
| -from django.core.management import call_command |
6 | 2 | from django.core.urlresolvers import reverse
|
7 | 3 | from django_dynamic_fixture import G
|
8 |
| -from django_elasticsearch_dsl import Index |
9 | 4 | from pyquery import PyQuery as pq
|
10 |
| -from pytest_mock import mock |
11 | 5 |
|
12 | 6 | from readthedocs.builds.constants import LATEST
|
13 | 7 | from readthedocs.builds.models import Version
|
14 |
| -from readthedocs.projects.models import Project |
| 8 | +from readthedocs.projects.models import Project, HTMLFile |
15 | 9 | from readthedocs.search.tests.utils import get_search_query_from_project_file
|
16 | 10 |
|
17 | 11 |
|
@@ -67,26 +61,60 @@ def test_search_project_filter_language(self, client, project):
|
67 | 61 |
|
68 | 62 | @pytest.mark.django_db
|
69 | 63 | @pytest.mark.search
|
70 |
| -class TestElasticSearch(object): |
| 64 | +class TestPageSearch(object): |
71 | 65 | url = reverse('search')
|
72 | 66 |
|
73 | 67 | def _get_search_result(self, url, client, search_params):
|
74 | 68 | resp = client.get(url, search_params)
|
75 | 69 | assert resp.status_code == 200
|
76 | 70 |
|
77 | 71 | page = pq(resp.content)
|
78 |
| - result = page.find('.module-list-wrapper .module-item-title') |
| 72 | + result = page.find('.module-list-wrapper .search-result-item') |
79 | 73 | return result, page
|
80 | 74 |
|
81 | 75 | @pytest.mark.parametrize('data_type', ['content', 'headers', 'title'])
|
82 | 76 | @pytest.mark.parametrize('page_num', [0, 1])
|
83 |
| - def test_search_by_file_content(self, client, project, data_type, page_num): |
| 77 | + def test_file_search(self, client, project, data_type, page_num): |
84 | 78 | query = get_search_query_from_project_file(project_slug=project.slug, page_num=page_num,
|
85 | 79 | data_type=data_type)
|
86 | 80 |
|
87 | 81 | result, _ = self._get_search_result(url=self.url, client=client,
|
88 | 82 | search_params={'q': query, 'type': 'file'})
|
89 |
| - assert len(result) == 1, ("failed"+ query) |
| 83 | + assert len(result) == 1 |
| 84 | + assert query in result.text() |
| 85 | + |
| 86 | + @pytest.mark.parametrize('case', ['upper', 'lower', 'title']) |
| 87 | + def test_file_search_case_insensitive(self, client, project, case): |
| 88 | + """Check File search is case insensitive |
| 89 | +
|
| 90 | + It tests with uppercase, lowercase and camelcase |
| 91 | + """ |
| 92 | + query_text = get_search_query_from_project_file(project_slug=project.slug) |
| 93 | + |
| 94 | + cased_query = getattr(query_text, case) |
| 95 | + query = cased_query() |
| 96 | + |
| 97 | + result, _ = self._get_search_result(url=self.url, client=client, |
| 98 | + search_params={'q': query, 'type': 'file'}) |
| 99 | + |
| 100 | + assert len(result) == 1 |
| 101 | + # Check the actual text is in the result, not the cased one |
| 102 | + assert query_text in result.text() |
| 103 | + |
| 104 | + def test_page_search_not_return_removed_page(self, client, project): |
| 105 | + """Check removed page are not in the search index""" |
| 106 | + query = get_search_query_from_project_file(project_slug=project.slug) |
| 107 | + # Make a query to check it returns result |
| 108 | + result, _ = self._get_search_result(url=self.url, client=client, |
| 109 | + search_params={'q': query, 'type': 'file'}) |
| 110 | + assert len(result) == 1 |
| 111 | + |
| 112 | + # Delete all the HTML files of the project |
| 113 | + HTMLFile.objects.filter(project=project).delete() |
| 114 | + # Run the query again and this time there should not be any result |
| 115 | + result, _ = self._get_search_result(url=self.url, client=client, |
| 116 | + search_params={'q': query, 'type': 'file'}) |
| 117 | + assert len(result) == 0 |
90 | 118 |
|
91 | 119 | def test_file_search_show_projects(self, client, all_projects):
|
92 | 120 | """Test that search result page shows list of projects while searching for files"""
|
|
0 commit comments