Module: GdsApi::TestHelpers::Search

Defined in:
lib/gds_api/test_helpers/search.rb

Constant Summary collapse

SEARCH_ENDPOINT =
Plek.current.find("search")

Instance Method Summary collapse

Instance Method Details

#assert_search(options) ⇒ Object


42
43
44
# File 'lib/gds_api/test_helpers/search.rb', line 42

def assert_search(options)
  assert_requested :get, "#{SEARCH_ENDPOINT}/search.json", **options
end

#assert_search_deleted_content(base_path, **options) ⇒ Object


79
80
81
82
83
84
85
# File 'lib/gds_api/test_helpers/search.rb', line 79

def assert_search_deleted_content(base_path, **options)
  assert_requested(
    :delete,
    %r{#{SEARCH_ENDPOINT}/content.*#{base_path}},
    **options,
  )
end

#assert_search_deleted_item(id, index: nil, **options) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gds_api/test_helpers/search.rb', line 59

def assert_search_deleted_item(id, index: nil, **options)
  if id =~ %r{^/}
    raise ArgumentError, "Search id must not start with a slash"
  end

  if index
    assert_requested(
      :delete,
      %r{#{SEARCH_ENDPOINT}/#{index}/documents/#{id}},
      **options,
    )
  else
    assert_requested(
      :delete,
      %r{#{SEARCH_ENDPOINT}/documents/#{id}},
      **options,
    )
  end
end

#assert_search_posted_item(attributes, index: nil, **options) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gds_api/test_helpers/search.rb', line 19

def assert_search_posted_item(attributes, index: nil, **options)
  url = if index
          SEARCH_ENDPOINT + "/#{index}/documents"
        else
          "#{SEARCH_ENDPOINT}/documents"
        end

  assert_requested(:post, url, **options) do |req|
    data = JSON.parse(req.body)
    attributes.to_a.all? do |key, value|
      data[key.to_s] == value
    end
  end
end

#stub_any_searchObject


34
35
36
# File 'lib/gds_api/test_helpers/search.rb', line 34

def stub_any_search
  stub_request(:get, %r{#{SEARCH_ENDPOINT}/search.json})
end

#stub_any_search_delete(index: nil) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/gds_api/test_helpers/search.rb', line 46

def stub_any_search_delete(index: nil)
  if index
    stub_request(:delete, %r{#{SEARCH_ENDPOINT}/#{index}/documents/.*})
  else
    # use search-api's default index
    stub_request(:delete, %r{#{SEARCH_ENDPOINT}/documents/.*})
  end
end

#stub_any_search_delete_contentObject


55
56
57
# File 'lib/gds_api/test_helpers/search.rb', line 55

def stub_any_search_delete_content
  stub_request(:delete, %r{#{SEARCH_ENDPOINT}/content.*})
end

#stub_any_search_post(index: nil) ⇒ Object


9
10
11
12
13
14
15
16
17
# File 'lib/gds_api/test_helpers/search.rb', line 9

def stub_any_search_post(index: nil)
  if index
    stub_request(:post, %r{#{SEARCH_ENDPOINT}/#{index}/documents})
      .to_return(status: [202, "Accepted"])
  else
    stub_request(:post, %r{#{SEARCH_ENDPOINT}/documents})
      .to_return(status: [202, "Accepted"])
  end
end

#stub_any_search_to_return_no_resultsObject


38
39
40
# File 'lib/gds_api/test_helpers/search.rb', line 38

def stub_any_search_to_return_no_results
  stub_any_search.to_return(body: { results: [] }.to_json)
end

#stub_search_has_no_policies_for_any_typeObject


102
103
104
105
# File 'lib/gds_api/test_helpers/search.rb', line 102

def stub_search_has_no_policies_for_any_type
  stub_request(:get, %r{/search.json})
    .to_return(body: no_search_results_found)
end

#stub_search_has_no_services_and_info_data_for_organisationObject


92
93
94
95
# File 'lib/gds_api/test_helpers/search.rb', line 92

def stub_search_has_no_services_and_info_data_for_organisation
  stub_request_for(no_search_results_found)
  run_example_query
end

#stub_search_has_policies_for_every_type(options = {}) ⇒ Object


107
108
109
110
111
112
113
114
115
# File 'lib/gds_api/test_helpers/search.rb', line 107

def stub_search_has_policies_for_every_type(options = {})
  if options[:count]
    stub_request(:get, %r{/search.json.*count=#{options[:count]}.*})
      .to_return(body: first_n_results(new_policies_results, n: options[:count]))
  else
    stub_request(:get, %r{/search.json})
      .to_return(body: new_policies_results)
  end
end

#stub_search_has_services_and_info_data_for_organisationObject


87
88
89
90
# File 'lib/gds_api/test_helpers/search.rb', line 87

def stub_search_has_services_and_info_data_for_organisation
  stub_request_for(search_results_found)
  run_example_query
end

#stub_search_has_specialist_sector_organisations(_sub_sector) ⇒ Object


97
98
99
100
# File 'lib/gds_api/test_helpers/search.rb', line 97

def stub_search_has_specialist_sector_organisations(_sub_sector)
  stub_request_for(sub_sector_organisations_results)
  run_example_query
end