def use_common_search(query, env): """ Make variable call using the common search code based on the network code. """ config = {'env': env} scom.set_logging_to(scom.logging.DEBUG) records = scom.search_by_page("variables", query=query, config=config) process_records("Common Search Example", records)
def search(query = None, filters = None, limit = None, config: dict = None): """ Search and return all records """ page_state = scom.create_page_state(limit=limit) found_items = scom.search_by_page("collections", query=query, filters=filters, page_state=page_state, config=config) return found_items
def _collection_samples(collection_query, limit, config): """ First step in the granule samples function is to run a collection query. Do not require the loading of the search.py file, call the already loaded common file. """ just_cid = lambda obj : obj.get('meta', {}).get('concept-id') found_collections = scom.search_by_page("collections", query=collection_query, filters=just_cid, page_state=scom.create_page_state(limit=limit), config=config) return found_collections[:limit]
def search(query, filters = None, limit = None, config: dict = None): """ Search and return all records Parameters: query (dictionary): required, CMR search parameters filters (list): column filter lambdas limit (int): number from 1 to 100000 config (dictionary): configuration settings Returns: JSON results from CMR """ page_state = scom.create_page_state(limit=limit) found_items = scom.search_by_page("granules", query=query, filters=filters, page_state=page_state, config=config) return found_items
def test_search_by_page(self, clr_scroll_mock, urlopen_mock): """ Test the inner function which performs the first half of a search """ recorded_file = tutil.resolve_full_path( '../data/cmr/search/ten_results_from_ghrc.json') urlopen_mock.return_value = valid_cmr_response(recorded_file, 200) query = {'keyword': 'water'} response = scom.search_by_page('collections', query) self.assertEqual(10, len(response), 'assumed page_state') # page state uses scroll page_state = scom.create_page_state(limit=4000) urlopen_mock.return_value = valid_cmr_response( recorded_file, 200, [('CMR-Scroll-Id', 'si-01')]) page_state['CMR-Scroll-Id'] = 'abcd' response = scom.search_by_page('collections', query, page_state=page_state) self.assertEqual(20, len(response), 'assumed page_state') # error processing 1 urlopen_mock.return_value = tutil.MockResponse("I'm a tea pot", 418) response = scom.search_by_page('collections', query, config={'debug': True}) expected = { 'errors': ['unknown response: I\'m a tea pot'], 'code': 0, 'reason': 'unknown response: I\'m a tea pot' } self.assertEqual(expected, response, "exeption") # error processing 2 urlopen_mock.return_value = valid_cmr_response('{"errors":["Error"]}', 500) urlopen_mock.side_effect = urlerr.HTTPError(Mock(status=500), "500", "Server Error", None, None) response = scom.search_by_page('collections', query) expected = { 'code': '500', 'reason': 'Server Error', 'errors': ['Server Error'] } self.assertEqual(expected, response, "exeption") # bad clear response is logged recorded_file = tutil.resolve_full_path( '../data/cmr/search/ten_results_from_ghrc.json') clr_scroll_mock.return_value = {'errors': ['bad scroll id']} urlopen_mock.return_value = valid_cmr_response(recorded_file, 200) urlopen_mock.side_effect = None response = scom.search_by_page('collections', query, page_state=page_state) self.assertEqual(10, len(response), "bad scroll id") # takes to long recorded_file = tutil.resolve_full_path( '../data/cmr/search/ten_results_from_ghrc.json') page_state['took'] = 300001 page_state['page_size'] = 1 urlopen_mock.return_value = valid_cmr_response(recorded_file, 200) response = scom.search_by_page('collections', query, page_state=page_state) self.assertEqual(10, len(response), "bad scroll id")