From aae3784697f40fe5b93afd23052c46d72d7d0813 Mon Sep 17 00:00:00 2001 From: achan Date: Mon, 30 Dec 2024 15:04:02 -0500 Subject: [PATCH] Add rspec --- app/controllers/v1/institutions_controller.rb | 8 ++--- .../v1/institutions_controller_spec.rb | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/controllers/v1/institutions_controller.rb b/app/controllers/v1/institutions_controller.rb index d85c7885f..c5587c167 100644 --- a/app/controllers/v1/institutions_controller.rb +++ b/app/controllers/v1/institutions_controller.rb @@ -72,15 +72,15 @@ def location meta: @meta end - # GET /v1/institutions?description=nursing&latitude=42.3601&longitude=-71.0589 + # GET /v1/gi/institutions/search?description=nursing&latitude=42.3601&longitude=-71.0589 def program @query ||= normalized_query_params # Start with filtering by institution programs based on description institution_programs = InstitutionProgram.joins(:institution) - .where('institution_programs.description ILIKE ?', "%#{@query[:description]}%") - .where(institutions: { version: @version }) - + .where('institution_programs.description ILIKE ?', "%#{@query[:description]}%") + .where(institutions: { version: @version }) + # Now filter by location location_results = Institution.approved_institutions(@version) .location_search(@query) diff --git a/spec/controllers/v1/institutions_controller_spec.rb b/spec/controllers/v1/institutions_controller_spec.rb index 6e7ff6f5c..8d401afb0 100644 --- a/spec/controllers/v1/institutions_controller_spec.rb +++ b/spec/controllers/v1/institutions_controller_spec.rb @@ -535,4 +535,33 @@ def check_boolean_facets(facets) expect(JSON.parse(response.body)['data'].count).to eq(0) end end + + context 'with program search results' do + before do + create(:version, :production) + create(:institution, :production_version, latitude: 30.1659, longitude: -93.2146) # Example institution + create(:institution_program, institution: Institution.last, description: 'Software Engineering') # Program associated with the institution + end + + it 'search returns results matching program description and location' do + get(:program, params: { description: 'Software Engineering', latitude: 30.1659, longitude: -93.2146 }) + expect(JSON.parse(response.body)['data'].count).to eq(1) # Expecting one result + expect(response.media_type).to eq('application/json') + expect(response).to match_response_schema('institution_search_results') + end + + it 'returns no results for non-matching program description' do + get(:program, params: { description: 'Non-existing Program', latitude: 30.1659, longitude: -93.2146 }) + expect(JSON.parse(response.body)['data'].count).to eq(0) # Expecting no results + expect(response.media_type).to eq('application/json') + expect(response).to match_response_schema('institution_search_results') + end + + it 'returns no results for non-matching location' do + get(:program, params: { description: 'Software Engineering', latitude: 0.0, longitude: 0.0 }) + expect(JSON.parse(response.body)['data'].count).to eq(0) # Expecting no results + expect(response.media_type).to eq('application/json') + expect(response).to match_response_schema('institution_search_results') + end + end end