require'json'require'gds_api/test_helpers/json_client_helper'moduleGdsApimoduleTestHelpersmoduleSearchSEARCH_ENDPOINT=Plek.current.find('search')defstub_any_search_post(index: nil)ifindexstub_request(:post,%r{#{SEARCH_ENDPOINT}/#{index}/documents}).to_return(status: [202,"Accepted"])elsestub_request(:post,%r{#{SEARCH_ENDPOINT}/documents}).to_return(status: [202,"Accepted"])endenddefassert_search_posted_item(attributes,index: nil,**options)ifindexurl=SEARCH_ENDPOINT+"/#{index}/documents"elseurl=SEARCH_ENDPOINT+"/documents"endassert_requested(:post,url,**options)do|req|data=JSON.parse(req.body)attributes.to_a.all?do|key,value|data[key.to_s]==valueendendenddefstub_any_searchstub_request(:get,%r{#{SEARCH_ENDPOINT}/search.json})enddefstub_any_search_to_return_no_resultsstub_any_search.to_return(body: {results: []}.to_json)enddefassert_search(options)assert_requested:get,"#{SEARCH_ENDPOINT}/search.json",**optionsenddefstub_any_search_delete(index: nil)ifindexstub_request(:delete,%r{#{SEARCH_ENDPOINT}/#{index}/documents/.*})else# use search-api's default indexstub_request(:delete,%r{#{SEARCH_ENDPOINT}/documents/.*})endenddefstub_any_search_delete_contentstub_request(:delete,%r{#{SEARCH_ENDPOINT}/content.*})enddefassert_search_deleted_item(id,index: nil,**options)ifid=~%r{^/}raiseArgumentError,'Search id must not start with a slash'endifindexassert_requested(:delete,%r{#{SEARCH_ENDPOINT}/#{index}/documents/#{id}},**options)elseassert_requested(:delete,%r{#{SEARCH_ENDPOINT}/documents/#{id}},**options)endenddefassert_search_deleted_content(base_path,**options)assert_requested(:delete,%r{#{SEARCH_ENDPOINT}/content.*#{base_path}},**options)enddefstub_search_has_services_and_info_data_for_organisationstub_request_for(search_results_found)run_example_queryenddefstub_search_has_no_services_and_info_data_for_organisationstub_request_for(no_search_results_found)run_example_queryenddefstub_search_has_specialist_sector_organisations(_sub_sector)stub_request_for(sub_sector_organisations_results)run_example_queryenddefstub_search_has_no_policies_for_any_typestub_request(:get,%r{/search.json}).to_return(body: no_search_results_found)enddefstub_search_has_policies_for_every_type(options={})ifoptions[:count]stub_request(:get,%r{/search.json.*count=#{options[:count]}.*}).to_return(body: first_n_results(new_policies_results,n: options[:count]))elsestub_request(:get,%r{/search.json}).to_return(body: new_policies_results)endendprivatedefstub_request_for(result_set)stub_request(:get,/example.com\/search/).to_return(body: result_set)enddefrun_example_queryclient.search(example_query)enddefsearch_results_foundFile.read(File.expand_path("../../../test/fixtures/services_and_info_fixture.json",__dir__))enddefno_search_results_foundFile.read(File.expand_path("../../../test/fixtures/no_services_and_info_data_found_fixture.json",__dir__))enddefsub_sector_organisations_resultsFile.read(File.expand_path("../../../test/fixtures/sub_sector_organisations.json",__dir__))enddefnew_policies_resultsFile.read(File.expand_path("../../../test/fixtures/new_policies_for_dwp.json",__dir__))enddefold_policies_resultsFile.read(File.expand_path("../../../test/fixtures/old_policies_for_dwp.json",__dir__))enddeffirst_n_results(results,options)n=options[:n]results=JSON.parse(results)results["results"]=results["results"][0...n]results.to_jsonenddefclientGdsApi::Search.new("http://example.com")enddefexample_query{filter_organisations: ["an-organisation-slug"],facet_specialist_sectors: "1000,examples:4,example_scope:global,order:value.title"}endendendend