Skip to content

Commit d3c64c1

Browse files
authored
Merge pull request #4277 from safwanrahman/insensitive
[Fix #2328 #2013] Refresh search index and test for case insensitive search
2 parents fd75aa3 + e21b79e commit d3c64c1

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

readthedocs/search/documents.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,14 @@ def update(self, thing, refresh=None, action='index', **kwargs):
9696
# TODO: remove this overwrite when the issue has been fixed
9797
# See below link for more information
9898
# https://github.com/sabricot/django-elasticsearch-dsl/issues/111
99-
if isinstance(thing, HTMLFile):
99+
# Moreover, do not need to check if its a delete action
100+
# Because while delete action, the object is already remove from database
101+
if isinstance(thing, HTMLFile) and action != 'delete':
100102
# Its a model instance.
101103
queryset = self.get_queryset()
102104
obj = queryset.filter(pk=thing.pk)
103105
if not obj.exists():
104106
return None
105107

106-
return super(PageDocument, self).update(thing=thing, refresh=None, action='index', **kwargs)
108+
return super(PageDocument, self).update(thing=thing, refresh=refresh,
109+
action=action, **kwargs)

readthedocs/search/tests/test_views.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import random
2-
import string
3-
41
import pytest
5-
from django.core.management import call_command
62
from django.core.urlresolvers import reverse
73
from django_dynamic_fixture import G
8-
from django_elasticsearch_dsl import Index
94
from pyquery import PyQuery as pq
10-
from pytest_mock import mock
115

126
from readthedocs.builds.constants import LATEST
137
from readthedocs.builds.models import Version
14-
from readthedocs.projects.models import Project
8+
from readthedocs.projects.models import Project, HTMLFile
159
from readthedocs.search.tests.utils import get_search_query_from_project_file
1610

1711

@@ -67,26 +61,60 @@ def test_search_project_filter_language(self, client, project):
6761

6862
@pytest.mark.django_db
6963
@pytest.mark.search
70-
class TestElasticSearch(object):
64+
class TestPageSearch(object):
7165
url = reverse('search')
7266

7367
def _get_search_result(self, url, client, search_params):
7468
resp = client.get(url, search_params)
7569
assert resp.status_code == 200
7670

7771
page = pq(resp.content)
78-
result = page.find('.module-list-wrapper .module-item-title')
72+
result = page.find('.module-list-wrapper .search-result-item')
7973
return result, page
8074

8175
@pytest.mark.parametrize('data_type', ['content', 'headers', 'title'])
8276
@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):
8478
query = get_search_query_from_project_file(project_slug=project.slug, page_num=page_num,
8579
data_type=data_type)
8680

8781
result, _ = self._get_search_result(url=self.url, client=client,
8882
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
90118

91119
def test_file_search_show_projects(self, client, all_projects):
92120
"""Test that search result page shows list of projects while searching for files"""

readthedocs/templates/search/elastic_search.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ <h3>{% blocktrans with query=query|default:"" %}Results for {{ query }}{% endblo
123123

124124
<ul>
125125
{% for result in results %}
126-
<li class="module-item">
126+
<li class="module-item search-result-item">
127127
<p class="module-item-title">
128128
{% if result.name %}
129129

130130
{# Project #}
131131
<a href="{{ result.url }}">{{ result.name }}</a>
132+
</p>
132133
{{ result.meta.description }}
133134
{% for fragment in result.meta.highlight.description|slice:":3" %}
134135
<p>
@@ -150,7 +151,6 @@ <h3>{% blocktrans with query=query|default:"" %}Results for {{ query }}{% endblo
150151
{# End File #}
151152

152153
{% endif %}
153-
</p>
154154
</li>
155155
{% empty %}
156156
<li class="module-item"><span class="quiet">{% trans "No results found. Bummer." %}</span></li>

0 commit comments

Comments
 (0)