diff --git a/app/controllers/v0/institution_programs_controller.rb b/app/controllers/v0/institution_programs_controller.rb index 4bcf08913..a2b48e348 100644 --- a/app/controllers/v0/institution_programs_controller.rb +++ b/app/controllers/v0/institution_programs_controller.rb @@ -29,7 +29,7 @@ def index render json: search_results .search_order(@query) - .page(page), meta: @meta + .then { |results| pagination_for(results) }, meta: @meta end private @@ -40,7 +40,9 @@ def normalized_query_params query[:name].try(:strip!) query[:name].try(:downcase!) query[:preferred_provider].try(:downcase!) + query[:disable_pagination].try(:downcase!) query[:provider].try(:upcase!) + query[:facility_code].try(:upcase!) %i[state country type].each do |k| query[k].try(:upcase!) end @@ -85,6 +87,7 @@ def filter_results(relation) [ %i[program_type type], %i[institutions.institution provider], + %i[institutions.facility_code facility_code], %w[institutions.physical_country country], %w[institutions.physical_state state], %w[institutions.preferred_provider preferred_provider] @@ -116,5 +119,9 @@ def add_active_search_facets(raw_facets) add_country_search_facet(raw_facets) raw_facets end + + def pagination_for(results) + @query[:disable_pagination] == 'true' ? results : results.page(page) + end end end diff --git a/spec/controllers/v0/institution_programs_controller_spec.rb b/spec/controllers/v0/institution_programs_controller_spec.rb index 671d4aea8..a711650d4 100644 --- a/spec/controllers/v0/institution_programs_controller_spec.rb +++ b/spec/controllers/v0/institution_programs_controller_spec.rb @@ -186,6 +186,24 @@ expect(response).to match_response_schema('institution_programs') end + it 'filters by uppercase facility code' do + create(:institution, version_id: Version.last.id, facility_code: 'ABCD1234') + create(:institution_program, institution_id: Institution.last.id) + get(:index, params: { facility_code: Institution.last.facility_code }) + expect(JSON.parse(response.body)['data'].count).to eq(1) + expect(response.media_type).to eq('application/json') + expect(response).to match_response_schema('institution_programs') + end + + it 'filters by lowercase facility code' do + create(:institution, version_id: Version.last.id, facility_code: 'ABCD1234') + create(:institution_program, institution_id: Institution.last.id) + get(:index, params: { facility_code: Institution.last.facility_code.downcase }) + expect(JSON.parse(response.body)['data'].count).to eq(1) + expect(response.media_type).to eq('application/json') + expect(response).to match_response_schema('institution_programs') + end + it 'filter by lowercase state returns results' do get(:index, params: { state: 'ny' }) expect(JSON.parse(response.body)['data'].count).to eq(3) @@ -283,5 +301,41 @@ get(:index, params: { name: 'testville, ak' }) expect(JSON.parse(response.body)['data'].count).to eq(1) end + + context 'when configuring pagination' do + context 'when enabled' do + it 'enables pagination by default' do + get(:index) + expect(JSON.parse(response.body)['links']).not_to be_nil + end + + it 'enables pagination with invalid query parameter' do + get(:index, params: { disable_pagination: 'invalid' }) + expect(JSON.parse(response.body)['links']).not_to be_nil + end + + it 'enables pagination with a lowercase query parameter' do + get(:index, params: { disable_pagination: 'false' }) + expect(JSON.parse(response.body)['links']).not_to be_nil + end + + it 'enables pagination with an uppercase query parameter' do + get(:index, params: { disable_pagination: 'False' }) + expect(JSON.parse(response.body)['links']).not_to be_nil + end + end + + context 'when disabled' do + it 'disables pagination with a lowercase query parameter' do + get(:index, params: { disable_pagination: 'true' }) + expect(JSON.parse(response.body)['links']).to be_nil + end + + it 'disabled pagination with an uppercase query parameter' do + get(:index, params: { disable_pagination: 'True' }) + expect(JSON.parse(response.body)['links']).to be_nil + end + end + end end end