Skip to content

Commit 4e30b4d

Browse files
committed
adding more tests and fixup
1 parent 41a67a3 commit 4e30b4d

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

readthedocs/restapi/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from readthedocs.restapi.views import (
1212
core_views, footer_views, search_views, task_views, integrations
1313
)
14-
from readthedocs.search.api import PageSearchAPIView
1514

1615
from .views.model_views import (BuildViewSet, BuildCommandViewSet,
1716
ProjectViewSet, NotificationViewSet,
@@ -56,7 +55,6 @@
5655
url(r'search/section/$',
5756
search_views.section_search,
5857
name='api_section_search'),
59-
url(r'^docsearch/$', PageSearchAPIView.as_view(), name='doc_search'),
6058
]
6159

6260
task_urls = [

readthedocs/search/api.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ def get_queryset(self):
2828
return queryset
2929

3030
def validate_query_params(self):
31-
required_query_params = set(['query', 'project', 'version'])
31+
required_query_params = {'query', 'project', 'version'} # python `set` literal is `{}`
3232
request_params = set(self.request.query_params.keys())
3333
missing_params = required_query_params - request_params
3434
if missing_params:
35-
errors = []
35+
errors = {}
3636
for param in missing_params:
37-
e = {param: "This query param is required"}
38-
errors.append(e)
37+
errors[param] = ["This query param is required"]
3938

4039
raise ValidationError(errors)

readthedocs/search/faceted_search.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ class FileSearch(RTDFacetedSearch):
3333
}
3434

3535
def query(self, search, query):
36-
"""Add query part to ``search``"""
36+
"""Add query part to ``search``
37+
38+
Overriding because we pass ES Query object instead of
39+
string search.
40+
"""
3741
if query:
3842
search = search.query(query)
3943

readthedocs/search/tests/test_api.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,44 @@ def test_doc_search_filter_by_version(self, api_client, project):
6969
assert len(data) == 1
7070
assert data[0]['project'] == project.slug
7171

72+
def test_doc_search_pagination(self, api_client, project):
73+
"""Test Doc search result can be paginated"""
74+
latest_version = project.versions.all()[0]
75+
html_file = HTMLFile.objects.filter(version=latest_version)[0]
76+
title = html_file.processed_json['title']
77+
query = title.split()[0]
78+
79+
# Create 15 more same html file
80+
for _ in range(15):
81+
# Make primary key to None, so django will create new object
82+
html_file.pk = None
83+
html_file.save()
84+
85+
search_params = {'query': query, 'project': project.slug, 'version': latest_version.slug}
86+
resp = api_client.get(self.url, search_params)
87+
assert resp.status_code == 200
88+
89+
# Check the count is 16 (1 existing and 15 new created)
90+
assert resp.data['count'] == 16
91+
# Check there are next url
92+
assert resp.data['next'] is not None
93+
# There should be only 10 data as the pagination is 10 by default
94+
assert len(resp.data['results']) == 10
95+
96+
# Add `page_size` parameter and check the data is paginated accordingly
97+
search_params['page_size'] = 5
98+
resp = api_client.get(self.url, search_params)
99+
assert resp.status_code == 200
100+
101+
assert len(resp.data['results']) == 5
102+
103+
def test_doc_search_without_parameters(self, api_client, project):
104+
"""Hitting Document Search endpoint without query parameters should return error"""
105+
resp = api_client.get(self.url)
106+
assert resp.status_code == 400
107+
# Check error message is there
108+
assert sorted(['query', 'project', 'version']) == sorted(resp.data.keys())
109+
72110
def test_doc_search_subprojects(self, api_client, all_projects):
73111
"""Test Document search return results from subprojects also"""
74112
project = all_projects[0]

readthedocs/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
do_not_track,
2323
)
2424
from readthedocs.search import views as search_views
25+
from readthedocs.search.api import PageSearchAPIView
2526

2627
v1_api = Api(api_name='v1')
2728
v1_api.register(UserResource())
@@ -65,6 +66,8 @@
6566
api_urls = [
6667
url(r'^api/', include(v1_api.urls)),
6768
url(r'^api/v2/', include('readthedocs.restapi.urls')),
69+
# Keep the `doc_search` at root level, so the test does not fail for other API
70+
url(r'^api/v2/docsearch/$', PageSearchAPIView.as_view(), name='doc_search'),
6871
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
6972
]
7073

0 commit comments

Comments
 (0)