def test_search_autosave(session): """Assert that a valid search query selection update works as expected.""" query = SearchClient.find_by_id(200000000) assert query.search_response update_data = json.loads(query.search_response) if update_data[0]['matchType'] == 'EXACT': update_data[0]['matchType'] = 'SIMILAR' else: update_data[0]['matchType'] = 'EXACT' query.update_search_selection(update_data) json_data = query.json assert json_data['results'][0]['matchType'] == update_data[0]['matchType']
def put(search_id): """Execute a search selection update request replacing the current value with the request body contents.""" try: if search_id is None: return resource_utils.path_param_error_response('search ID') # Quick check: must be staff or provide an account ID. account_id = resource_utils.get_account_id(request) if not is_staff(jwt) and account_id is None: return resource_utils.account_required_response() # Verify request JWT and account ID if not authorized(account_id, jwt): return resource_utils.unauthorized_error_response(account_id) request_json = request.get_json(silent=True) # Validate schema. valid_format, errors = schema_utils.validate( request_json, 'searchSummary', 'ppr') if not valid_format: return resource_utils.validation_error_response( errors, VAL_ERROR) search_client = SearchClient.find_by_id(search_id) if not search_client: return resource_utils.not_found_error_response( 'searchId', search_id) # Save the updated search selection. search_client.update_search_selection(request_json) return jsonify(json.loads( search_client.search_response)), HTTPStatus.ACCEPTED except BusinessException as exception: return resource_utils.business_exception_response(exception) except Exception as default_exception: # noqa: B902; return nicer default error return resource_utils.default_exception_response(default_exception)