Show More
@@ -0,0 +1,93 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | ||||
|
3 | # Copyright (C) 2010-2019 RhodeCode GmbH | |||
|
4 | # | |||
|
5 | # This program is free software: you can redistribute it and/or modify | |||
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |||
|
7 | # (only), as published by the Free Software Foundation. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
14 | # You should have received a copy of the GNU Affero General Public License | |||
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
|
16 | # | |||
|
17 | # This program is dual-licensed. If you wish to learn more about the | |||
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |||
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
|
20 | ||||
|
21 | import pytest | |||
|
22 | from rhodecode.tests import HG_REPO | |||
|
23 | from rhodecode.api.tests.utils import ( | |||
|
24 | build_data, api_call, assert_error, assert_ok) | |||
|
25 | ||||
|
26 | ||||
|
27 | @pytest.mark.usefixtures("testuser_api", "app") | |||
|
28 | class TestApiSearch(object): | |||
|
29 | ||||
|
30 | @pytest.mark.parametrize("query, expected_hits, expected_paths", [ | |||
|
31 | ('todo', 23, [ | |||
|
32 | 'vcs/backends/hg/inmemory.py', | |||
|
33 | 'vcs/tests/test_git.py']), | |||
|
34 | ('extension:rst installation', 6, [ | |||
|
35 | 'docs/index.rst', | |||
|
36 | 'docs/installation.rst']), | |||
|
37 | ('def repo', 87, [ | |||
|
38 | 'vcs/tests/test_git.py', | |||
|
39 | 'vcs/tests/test_changesets.py']), | |||
|
40 | ('repository:%s def test' % HG_REPO, 18, [ | |||
|
41 | 'vcs/tests/test_git.py', | |||
|
42 | 'vcs/tests/test_changesets.py']), | |||
|
43 | ('"def main"', 9, [ | |||
|
44 | 'vcs/__init__.py', | |||
|
45 | 'vcs/tests/__init__.py', | |||
|
46 | 'vcs/utils/progressbar.py']), | |||
|
47 | ('owner:test_admin', 358, [ | |||
|
48 | 'vcs/tests/base.py', | |||
|
49 | 'MANIFEST.in', | |||
|
50 | 'vcs/utils/termcolors.py', | |||
|
51 | 'docs/theme/ADC/static/documentation.png']), | |||
|
52 | ('owner:test_admin def main', 72, [ | |||
|
53 | 'vcs/__init__.py', | |||
|
54 | 'vcs/tests/test_utils_filesize.py', | |||
|
55 | 'vcs/tests/test_cli.py']), | |||
|
56 | ('owner:michaΕ test', 0, []), | |||
|
57 | ]) | |||
|
58 | def test_search_content_results(self, query, expected_hits, expected_paths): | |||
|
59 | id_, params = build_data( | |||
|
60 | self.apikey_regular, 'search', | |||
|
61 | search_query=query, | |||
|
62 | search_type='content') | |||
|
63 | ||||
|
64 | response = api_call(self.app, params) | |||
|
65 | json_response = response.json | |||
|
66 | ||||
|
67 | assert json_response['result']['item_count'] == expected_hits | |||
|
68 | paths = [x['f_path'] for x in json_response['result']['results']] | |||
|
69 | ||||
|
70 | for expected_path in expected_paths: | |||
|
71 | assert expected_path in paths | |||
|
72 | ||||
|
73 | @pytest.mark.parametrize("query, expected_hits, expected_paths", [ | |||
|
74 | ('readme.rst', 3, []), | |||
|
75 | ('test*', 75, []), | |||
|
76 | ('*model*', 1, []), | |||
|
77 | ('extension:rst', 48, []), | |||
|
78 | ('extension:rst api', 24, []), | |||
|
79 | ]) | |||
|
80 | def test_search_file_paths(self, query, expected_hits, expected_paths): | |||
|
81 | id_, params = build_data( | |||
|
82 | self.apikey_regular, 'search', | |||
|
83 | search_query=query, | |||
|
84 | search_type='path') | |||
|
85 | ||||
|
86 | response = api_call(self.app, params) | |||
|
87 | json_response = response.json | |||
|
88 | ||||
|
89 | assert json_response['result']['item_count'] == expected_hits | |||
|
90 | paths = [x['f_path'] for x in json_response['result']['results']] | |||
|
91 | ||||
|
92 | for expected_path in expected_paths: | |||
|
93 | assert expected_path in paths |
@@ -1,109 +1,112 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 |
# Copyright (C) 2011-201 |
|
3 | # Copyright (C) 2011-2019 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | import logging |
|
22 | import logging | |
23 |
|
23 | |||
24 | from rhodecode.api import jsonrpc_method |
|
24 | from rhodecode.api import jsonrpc_method | |
25 | from rhodecode.api.exc import JSONRPCValidationError |
|
25 | from rhodecode.api.exc import JSONRPCValidationError | |
26 | from rhodecode.api.utils import Optional |
|
26 | from rhodecode.api.utils import Optional | |
27 | from rhodecode.lib.index import searcher_from_config |
|
27 | from rhodecode.lib.index import searcher_from_config | |
28 | from rhodecode.model import validation_schema |
|
28 | from rhodecode.model import validation_schema | |
29 | from rhodecode.model.validation_schema.schemas import search_schema |
|
29 | from rhodecode.model.validation_schema.schemas import search_schema | |
30 |
|
30 | |||
31 | log = logging.getLogger(__name__) |
|
31 | log = logging.getLogger(__name__) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | @jsonrpc_method() |
|
34 | @jsonrpc_method() | |
35 | def search(request, apiuser, search_query, search_type, page_limit=Optional(10), page=Optional(1), search_sort=Optional('newfirst'), repo_name=Optional(None), repo_group_name=Optional(None)): |
|
35 | def search(request, apiuser, search_query, search_type, page_limit=Optional(10), | |
|
36 | page=Optional(1), search_sort=Optional('newfirst'), | |||
|
37 | repo_name=Optional(None), repo_group_name=Optional(None)): | |||
36 | """ |
|
38 | """ | |
37 | Search |
|
39 | Fetch Full Text Search results using API. | |
38 |
|
40 | |||
39 | :param apiuser: This is filled automatically from the |authtoken|. |
|
41 | :param apiuser: This is filled automatically from the |authtoken|. | |
40 | :type apiuser: AuthUser |
|
42 | :type apiuser: AuthUser | |
41 | :param search_query: Search query. |
|
43 | :param search_query: Search query. | |
42 | :type search_query: str |
|
44 | :type search_query: str | |
43 | :param search_type: Search type. The following are valid options: |
|
45 | :param search_type: Search type. The following are valid options: | |
44 | * commit |
|
46 | * commit | |
45 | * content |
|
47 | * content | |
46 | * path |
|
48 | * path | |
47 | :type search_type: str |
|
49 | :type search_type: str | |
48 | :param page_limit: Page item limit, from 1 to 500. Default 10 items. |
|
50 | :param page_limit: Page item limit, from 1 to 500. Default 10 items. | |
49 | :type page_limit: Optional(int) |
|
51 | :type page_limit: Optional(int) | |
50 | :param page: Page number. Default first page. |
|
52 | :param page: Page number. Default first page. | |
51 | :type page: Optional(int) |
|
53 | :type page: Optional(int) | |
52 | :param search_sort: Search sort order. Default newfirst. The following are valid options: |
|
54 | :param search_sort: Search sort order. Default newfirst. The following are valid options: | |
53 | * newfirst |
|
55 | * newfirst | |
54 | * oldfirst |
|
56 | * oldfirst | |
55 | :type search_sort: Optional(str) |
|
57 | :type search_sort: Optional(str) | |
56 | :param repo_name: Filter by one repo. Default is all. |
|
58 | :param repo_name: Filter by one repo. Default is all. | |
57 | :type repo_name: Optional(str) |
|
59 | :type repo_name: Optional(str) | |
58 | :param repo_group_name: Filter by one repo group. Default is all. |
|
60 | :param repo_group_name: Filter by one repo group. Default is all. | |
59 | :type repo_group_name: Optional(str) |
|
61 | :type repo_group_name: Optional(str) | |
60 | """ |
|
62 | """ | |
61 |
|
63 | |||
62 | searcher = searcher_from_config(request.registry.settings) |
|
|||
63 | data = {'execution_time': ''} |
|
64 | data = {'execution_time': ''} | |
64 | repo_name = Optional.extract(repo_name) |
|
65 | repo_name = Optional.extract(repo_name) | |
65 | repo_group_name = Optional.extract(repo_group_name) |
|
66 | repo_group_name = Optional.extract(repo_group_name) | |
66 |
|
67 | |||
67 | schema = search_schema.SearchParamsSchema() |
|
68 | schema = search_schema.SearchParamsSchema() | |
68 |
|
69 | |||
69 | search_params = {} |
|
|||
70 | try: |
|
70 | try: | |
71 | search_params = schema.deserialize( |
|
71 | search_params = schema.deserialize( | |
72 | dict(search_query=search_query, |
|
72 | dict(search_query=search_query, | |
73 | search_type=search_type, |
|
73 | search_type=search_type, | |
74 | search_sort=Optional.extract(search_sort), |
|
74 | search_sort=Optional.extract(search_sort), | |
75 | page_limit=Optional.extract(page_limit), |
|
75 | page_limit=Optional.extract(page_limit), | |
76 | requested_page=Optional.extract(page)) |
|
76 | requested_page=Optional.extract(page)) | |
77 | ) |
|
77 | ) | |
78 | except validation_schema.Invalid as err: |
|
78 | except validation_schema.Invalid as err: | |
79 | raise JSONRPCValidationError(colander_exc=err) |
|
79 | raise JSONRPCValidationError(colander_exc=err) | |
80 |
|
80 | |||
81 | search_query = search_params.get('search_query') |
|
81 | search_query = search_params.get('search_query') | |
82 | search_type = search_params.get('search_type') |
|
82 | search_type = search_params.get('search_type') | |
83 | search_sort = search_params.get('search_sort') |
|
83 | search_sort = search_params.get('search_sort') | |
84 |
|
84 | |||
85 | if search_params.get('search_query'): |
|
85 | if search_params.get('search_query'): | |
86 | page_limit = search_params['page_limit'] |
|
86 | page_limit = search_params['page_limit'] | |
87 | requested_page = search_params['requested_page'] |
|
87 | requested_page = search_params['requested_page'] | |
88 |
|
88 | |||
|
89 | searcher = searcher_from_config(request.registry.settings) | |||
|
90 | ||||
89 | try: |
|
91 | try: | |
90 | search_result = searcher.search( |
|
92 | search_result = searcher.search( | |
91 | search_query, search_type, apiuser, repo_name, repo_group_name, |
|
93 | search_query, search_type, apiuser, repo_name, repo_group_name, | |
92 | requested_page=requested_page, page_limit=page_limit, sort=search_sort) |
|
94 | requested_page=requested_page, page_limit=page_limit, sort=search_sort) | |
93 |
|
95 | |||
94 | data.update(dict( |
|
96 | data.update(dict( | |
95 | results=list(search_result['results']), page=requested_page, |
|
97 | results=list(search_result['results']), page=requested_page, | |
96 | item_count=search_result['count'], |
|
98 | item_count=search_result['count'], | |
97 | items_per_page=page_limit)) |
|
99 | items_per_page=page_limit)) | |
98 | finally: |
|
100 | finally: | |
99 | searcher.cleanup() |
|
101 | searcher.cleanup() | |
100 |
|
102 | |||
101 | if not search_result['error']: |
|
103 | if not search_result['error']: | |
102 | data['execution_time'] = '%s results (%.3f seconds)' % ( |
|
104 | data['execution_time'] = '%s results (%.3f seconds)' % ( | |
103 | search_result['count'], |
|
105 | search_result['count'], | |
104 | search_result['runtime']) |
|
106 | search_result['runtime']) | |
105 | else: |
|
107 | else: | |
106 | node = schema['search_query'] |
|
108 | node = schema['search_query'] | |
107 | raise JSONRPCValidationError(colander_exc=validation_schema.Invalid(node, search_result['error'])) |
|
109 | raise JSONRPCValidationError( | |
|
110 | colander_exc=validation_schema.Invalid(node, search_result['error'])) | |||
108 |
|
111 | |||
109 | return data |
|
112 | return data |
General Comments 0
You need to be logged in to leave comments.
Login now