|
12 | 12 | from hdx.data.dataset import Dataset
|
13 | 13 | from hdx.data.hdxobject import HDXError
|
14 | 14 | from hdx.utilities.dictionary import merge_two_dictionaries
|
| 15 | +from hdx.utilities.loader import load_yaml |
15 | 16 |
|
16 | 17 |
|
17 | 18 | class MockResponse:
|
@@ -83,6 +84,7 @@ def json(self):
|
83 | 84 | 'solr_additions': '{"countries": ["Algeria", "Zimbabwe"]}',
|
84 | 85 | 'dataset_date': '06/04/2016'}
|
85 | 86 |
|
| 87 | +searchdict = load_yaml(join('fixtures', 'search_results.yml')) |
86 | 88 |
|
87 | 89 | def mockshow(url, datadict):
|
88 | 90 | if 'show' not in url and 'related_list' not in url:
|
@@ -113,6 +115,28 @@ def mockshow(url, datadict):
|
113 | 115 | '{"success": false, "error": {"message": "Not found", "__type": "Not Found Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=dataset_show"}')
|
114 | 116 |
|
115 | 117 |
|
| 118 | +def mocksearch(url, datadict): |
| 119 | + if 'search' not in url and 'related_list' not in url: |
| 120 | + return MockResponse(404, |
| 121 | + '{"success": false, "error": {"message": "TEST ERROR: Not search", "__type": "TEST ERROR: Not Search Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=package_search"}') |
| 122 | + if 'related_list' in url: |
| 123 | + result = json.dumps(TestDataset.gallery_data) |
| 124 | + return MockResponse(200, |
| 125 | + '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=related_list"}' % result) |
| 126 | + result = json.dumps(searchdict) |
| 127 | + if datadict['q'] == 'ACLED': |
| 128 | + return MockResponse(200, |
| 129 | + '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=package_search"}' % result) |
| 130 | + if datadict['q'] == '"': |
| 131 | + return MockResponse(404, |
| 132 | + '{"success": false, "error": {"message": "Validation Error", "__type": "Validation Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=package_search"}') |
| 133 | + if datadict['q'] == 'ajyhgr': |
| 134 | + return MockResponse(200, |
| 135 | + '{"success": true, "result": {"count": 0, "results": []}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=package_search"}') |
| 136 | + return MockResponse(404, |
| 137 | + '{"success": false, "error": {"message": "Not found", "__type": "Not Found Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=package_search"}') |
| 138 | + |
| 139 | + |
116 | 140 | class TestDataset():
|
117 | 141 | dataset_data = {
|
118 | 142 | 'name': 'MyDataset1',
|
@@ -290,6 +314,15 @@ def mockreturn(url, data, headers, files, allow_redirects, auth):
|
290 | 314 |
|
291 | 315 | monkeypatch.setattr(requests, 'post', mockreturn)
|
292 | 316 |
|
| 317 | + @pytest.fixture(scope='function') |
| 318 | + def search(self, monkeypatch): |
| 319 | + def mockreturn(url, data, headers, files, allow_redirects, auth): |
| 320 | + datadict = json.loads(data.decode('utf-8')) |
| 321 | + return mocksearch(url, datadict) |
| 322 | + |
| 323 | + monkeypatch.setattr(requests, 'post', mockreturn) |
| 324 | + |
| 325 | + |
293 | 326 | @pytest.fixture(scope='class')
|
294 | 327 | def configuration(self):
|
295 | 328 | hdx_key_file = join('fixtures', '.hdxkey')
|
@@ -460,3 +493,11 @@ def test_add_update_delete_gallery(self, configuration, post_delete):
|
460 | 493 | dataset.delete_galleryitem('NOTEXIST')
|
461 | 494 | dataset.delete_galleryitem('d59a01d8-e52b-4337-bcda-fceb1d059bef')
|
462 | 495 | assert len(dataset.gallery) == 0
|
| 496 | + |
| 497 | + def test_search_in_hdx(self, configuration, search): |
| 498 | + datasets = Dataset.search_in_hdx(configuration, 'ACLED') |
| 499 | + assert len(datasets) == 10 |
| 500 | + datasets = Dataset.search_in_hdx(configuration, 'ajyhgr') |
| 501 | + assert len(datasets) == 0 |
| 502 | + with pytest.raises(HDXError): |
| 503 | + Dataset.search_in_hdx(configuration, '"') |
0 commit comments