1
+ import pytest
2
+ from django .core .urlresolvers import reverse
3
+
4
+ from readthedocs .search .tests .utils import get_search_query_from_project_file
5
+
6
+
7
+ @pytest .mark .django_db
8
+ @pytest .mark .search
9
+ class TestPageSearch (object ):
10
+ url = reverse ('doc_search' )
11
+
12
+ @pytest .mark .parametrize ('data_type' , ['content' , 'headers' , 'title' ])
13
+ @pytest .mark .parametrize ('page_num' , [0 , 1 ])
14
+ def test_search_works (self , api_client , project , data_type , page_num ):
15
+ query = get_search_query_from_project_file (project_slug = project .slug , page_num = page_num ,
16
+ data_type = data_type )
17
+
18
+ version = project .versions .all ()[0 ]
19
+ url = reverse ('doc_search' )
20
+ resp = api_client .get (url , {'project' : project .slug , 'version' : version .slug , 'query' : query })
21
+ data = resp .data
22
+ assert len (data ['results' ]) == 1
23
+
24
+ # @pytest.mark.parametrize('case', ['upper', 'lower', 'title'])
25
+ # def test_file_search_case_insensitive(self, client, project, case):
26
+ # """Check File search is case insensitive
27
+ #
28
+ # It tests with uppercase, lowercase and camelcase
29
+ # """
30
+ # query_text = get_search_query_from_project_file(project_slug=project.slug)
31
+ #
32
+ # cased_query = getattr(query_text, case)
33
+ # query = cased_query()
34
+ #
35
+ # result, _ = self._get_search_result(url=self.url, client=client,
36
+ # search_params={'q': query, 'type': 'file'})
37
+ #
38
+ # assert len(result) == 1
39
+ # # Check the actual text is in the result, not the cased one
40
+ # assert query_text in result.text()
41
+ #
42
+ # def test_file_search_exact_match(self, client, project):
43
+ # """Check quoted query match exact phrase
44
+ #
45
+ # Making a query with quoted text like ``"foo bar"`` should match
46
+ # exactly ``foo bar`` phrase.
47
+ # """
48
+ #
49
+ # # `Github` word is present both in `kuma` and `pipeline` files
50
+ # # But the phrase Github can is available only in kuma docs.
51
+ # # So search with this phrase to check
52
+ # query = r'"GitHub can"'
53
+ #
54
+ # result, _ = self._get_search_result(url=self.url, client=client,
55
+ # search_params={'q': query, 'type': 'file'})
56
+ #
57
+ # assert len(result) == 1
58
+ #
59
+ # def test_page_search_not_return_removed_page(self, client, project):
60
+ # """Check removed page are not in the search index"""
61
+ # query = get_search_query_from_project_file(project_slug=project.slug)
62
+ # # Make a query to check it returns result
63
+ # result, _ = self._get_search_result(url=self.url, client=client,
64
+ # search_params={'q': query, 'type': 'file'})
65
+ # assert len(result) == 1
66
+ #
67
+ # # Delete all the HTML files of the project
68
+ # HTMLFile.objects.filter(project=project).delete()
69
+ # # Run the query again and this time there should not be any result
70
+ # result, _ = self._get_search_result(url=self.url, client=client,
71
+ # search_params={'q': query, 'type': 'file'})
72
+ # assert len(result) == 0
73
+ #
74
+ # def test_file_search_show_projects(self, client, all_projects):
75
+ # """Test that search result page shows list of projects while searching for files"""
76
+ #
77
+ # # `Github` word is present both in `kuma` and `pipeline` files
78
+ # # so search with this phrase
79
+ # result, page = self._get_search_result(url=self.url, client=client,
80
+ # search_params={'q': 'GitHub', 'type': 'file'})
81
+ #
82
+ # # There should be 2 search result
83
+ # assert len(result) == 2
84
+ #
85
+ # # there should be 2 projects in the left side column
86
+ # content = page.find('.navigable .project-list')
87
+ # assert len(content) == 2
88
+ # text = content.text()
89
+ #
90
+ # # kuma and pipeline should be there
91
+ # assert 'kuma' and 'pipeline' in text
92
+ #
93
+ # def test_file_search_filter_by_project(self, client):
94
+ # """Test that search result are filtered according to project"""
95
+ #
96
+ # # `Github` word is present both in `kuma` and `pipeline` files
97
+ # # so search with this phrase but filter through `kuma` project
98
+ # search_params = {'q': 'GitHub', 'type': 'file', 'project': 'kuma'}
99
+ # result, page = self._get_search_result(url=self.url, client=client,
100
+ # search_params=search_params)
101
+ #
102
+ # # There should be 1 search result as we have filtered
103
+ # assert len(result) == 1
104
+ # content = page.find('.navigable .project-list')
105
+ #
106
+ # # kuma should should be there only
107
+ # assert 'kuma' in result.text()
108
+ # assert 'pipeline' not in result.text()
109
+ #
110
+ # # But there should be 2 projects in the left side column
111
+ # # as the query is present in both projects
112
+ # content = page.find('.navigable .project-list')
113
+ # if len(content) != 2:
114
+ # pytest.xfail("failing because currently all projects are not showing in project list")
115
+ # else:
116
+ # assert 'kuma' and 'pipeline' in content.text()
117
+ #
118
+ # @pytest.mark.xfail(reason="Versions are not showing correctly! Fixme while rewrite!")
119
+ # def test_file_search_show_versions(self, client, all_projects, es_index, settings):
120
+ # # override the settings to index all versions
121
+ # settings.INDEX_ONLY_LATEST = False
122
+ #
123
+ # project = all_projects[0]
124
+ # # Create some versions of the project
125
+ # versions = [G(Version, project=project) for _ in range(3)]
126
+ #
127
+ # query = get_search_query_from_project_file(project_slug=project.slug)
128
+ #
129
+ # result, page = self._get_search_result(url=self.url, client=client,
130
+ # search_params={'q': query, 'type': 'file'})
131
+ #
132
+ # # There should be only one result because by default
133
+ # # only latest version result should be there
134
+ # assert len(result) == 1
135
+ #
136
+ # content = page.find('.navigable .version-list')
137
+ # # There should be total 4 versions
138
+ # # one is latest, and other 3 that we created above
139
+ # assert len(content) == 4
140
+ #
141
+ # project_versions = [v.slug for v in versions] + [LATEST]
142
+ # content_versions = []
143
+ # for element in content:
144
+ # text = element.text_content()
145
+ # # strip and split to keep the version slug only
146
+ # slug = text.strip().split('\n')[0]
147
+ # content_versions.append(slug)
148
+ #
149
+ # assert sorted(project_versions) == sorted(content_versions)
150
+ #
151
+ # def test_file_search_subprojects(self, client, all_projects, es_index):
152
+ # """File search should return results from subprojects also"""
153
+ # project = all_projects[0]
154
+ # subproject = all_projects[1]
155
+ # # Add another project as subproject of the project
156
+ # project.add_subproject(subproject)
157
+ #
158
+ # # Now search with subproject content but explicitly filter by the parent project
159
+ # query = get_search_query_from_project_file(project_slug=subproject.slug)
160
+ # search_params = {'q': query, 'type': 'file', 'project': project.slug}
161
+ # result, page = self._get_search_result(url=self.url, client=client,
162
+ # search_params=search_params)
163
+ #
164
+ # assert len(result) == 1
0 commit comments