Skip to content

Commit

Permalink
R2-2847 Fixing rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
aespinoza-quoin committed Apr 29, 2024
1 parent 77c7520 commit 2eb816c
Show file tree
Hide file tree
Showing 54 changed files with 150 additions and 637 deletions.
7 changes: 3 additions & 4 deletions app/models/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ def self.api_path
end

date(:date_closure)
# TODO: these must be deleted once task dashboard is migrated
date(:assessment_due_dates, multiple: true) { Tasks::AssessmentTask.from_case(self).map(&:due_date) }
date(:case_plan_due_dates, multiple: true) { Tasks::CasePlanTask.from_case(self).map(&:due_date) }
date(:followup_due_dates, multiple: true) { Tasks::FollowUpTask.from_case(self).map(&:due_date) }
%w[consent_for_tracing].each do |f|
boolean(f) { data[f] == true || data[f] == 'true' }
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/overrides/sunspot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
Sunspot::Rails::Server.class_eval do
# Log file for Solr. File is in the rails log/ directory.
def log_file
return unless Rails.configuration.solr_enabled

configuration.log_file
end
end
Expand All @@ -18,6 +20,8 @@ def log_file
# rsolr = Sunspot.session.session.rsolr_connection
Sunspot::Session.class_eval do
def rsolr_connection
return unless Rails.configuration.solr_enabled

connection
end
end
8 changes: 7 additions & 1 deletion app/services/data_removal_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def remove_all_records
ActiveRecord::Base.connection.execute('DELETE FROM form_sections_roles')
end

Sunspot.remove_all(RECORD_MODELS)
remove_from_solr
end

def remove_config(args = {})
Expand Down Expand Up @@ -104,5 +104,11 @@ def model_classes(models)

models.map { |model| Kernel.const_get(model) }
end

def remove_from_solr
return unless Rails.configuration.solr_enabled

Sunspot.remove_all(RECORD_MODELS)
end
end
end
2 changes: 2 additions & 0 deletions app/services/health_check_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def database_accessible?
end

def solr_accessible?
return true unless Rails.configuration.solr_enabled

Sunspot.session.session.rsolr_connection.head('admin/ping').response[:status] == 200
rescue RSolr::Error::ConnectionRefused
false
Expand Down
8 changes: 7 additions & 1 deletion app/services/model_deletion_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def delete_records!(query)
query.delete_all
end

Sunspot.remove_by_id(model_class, record_ids)
remove_from_solr(record_ids)
end

def delete_all!
Expand Down Expand Up @@ -162,5 +162,11 @@ def model_reflections
def model_join_reflections
model_class.reflect_on_all_associations(:has_and_belongs_to_many).reject(&:through_reflection?)
end

def remove_from_solr(record_ids)
return unless Rails.configuration.solr_enabled

Sunspot.remove_by_id(model_class, record_ids)
end
end
# rubocop:enable Metrics/ClassLength
2 changes: 0 additions & 2 deletions spec/models/attachements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@record_updated_on = child.last_updated_at
child.reload
attachment.attach!
Sunspot.commit
end

it 'attaches a base64 encoded file' do
Expand Down Expand Up @@ -53,7 +52,6 @@
attachment.attach!
child.reload
attachment.detach!
Sunspot.commit
end

it 'detaches the file and removes the attachment record' do
Expand Down
3 changes: 2 additions & 1 deletion spec/models/bulk_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
require 'sunspot'
require 'roo'

describe BulkExport, search: true do
# TODO: Remove skip_when_solr_disabled once exporters are migrated
describe BulkExport, { search: true, skip_when_solr_disabled: true } do
before :each do
clean_data(BulkExport, Location, UserGroup, User, Agency, Role, Field,
FormSection, Child, PrimeroModule, PrimeroProgram, SystemSettings,
Expand Down
46 changes: 30 additions & 16 deletions spec/models/child_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Copyright (c) 2014 - 2023 UNICEF. All rights reserved.

require 'rails_helper'
require 'sunspot'
require 'sunspot' if Rails.configuration.solr_enabled

describe Child do
describe 'quicksearch', search: true do
describe 'quicksearch' do
before do
clean_data(Child, Family)
end
Expand All @@ -16,9 +16,9 @@
end

it 'can find a child by survivor code' do
child = Child.create!(data: { name: 'Lonnie', survivor_code_no: 'ABC123XYZ' })
child.index!
search_result = SearchService.search(Child, query: 'ABC123XYZ').results
Child.create!(data: { name: 'Lonnie', survivor_code_no: 'ABC123XYZ' })

search_result = PhoneticSearchService.search(Child, query: 'ABC123XYZ').records
expect(search_result).to have(1).child
expect(search_result.first.survivor_code_no).to eq('ABC123XYZ')
end
Expand All @@ -27,15 +27,15 @@
family = Family.create!(
family_number: '4225',
family_members: [
{ unique_id: '123', relation_name: 'Family Name',relation_age: 5, relation_sex: "male" }
{ unique_id: '123', relation_name: 'Family Name', relation_age: 5, relation_sex: 'male' }
]
)
child = Child.create!(data: { name: 'Lonnie', survivor_code_no: 'ABC123XYZ' })
child.family = family
child.save!
child.index!

search_result = SearchService.search(Child, query: '4225').results
filter = SearchFilters::TextValue.new(field_name: 'family_number', value: '4225')
search_result = PhoneticSearchService.search(Child, filters: [filter]).records
expect(search_result).to have(1).child
expect(search_result.first.family_number).to eq('4225')
end
Expand Down Expand Up @@ -1014,25 +1014,39 @@
describe 'urgent_protection_concern', search: true do
it 'finds cases where the value is stored as string true' do
child = Child.create!(data: { name: 'Lonnie', urgent_protection_concern: 'true' })
child.index!
search_result = SearchService.search(
search_result = PhoneticSearchService.search(
Child,
filters: [SearchFilters::Value.new(field_name: 'urgent_protection_concern', value: true)]
).results
filters: [SearchFilters::BooleanValue.new(field_name: 'urgent_protection_concern', value: true)]
).records
expect(search_result).to have(1).child
expect(search_result.first.id).to eq(child.id)
end

it 'finds cases where the value is stored as string false' do
child = Child.create!(data: { name: 'Lonnie', urgent_protection_concern: 'false' })
child.index!
search_result = SearchService.search(
search_result = PhoneticSearchService.search(
Child,
filters: [SearchFilters::Value.new(field_name: 'urgent_protection_concern', value: false)]
).results
filters: [SearchFilters::BooleanValue.new(field_name: 'urgent_protection_concern', value: false)]
).records
expect(search_result).to have(1).child
expect(search_result.first.id).to eq(child.id)
end

context 'when fields store values string or boolean' do
let!(:record1) { Child.create!(data: { name: 'Foo', urgent_protection_concern: 'true' }) }
let!(:record2) { Child.create!(data: { name: 'Bar', urgent_protection_concern: true }) }
let!(:record3) { Child.create!(data: { name: 'Random', age: 2 }) }

it 'finds cases where the value is stored as string or boolean' do
search_result = PhoneticSearchService.search(
Child,
filters: [SearchFilters::BooleanValue.new(field_name: 'urgent_protection_concern', value: true)]
).records

expect(search_result).to have(2).child
expect(search_result.map(&:id)).to match_array([record1.id, record2.id])
end
end
end

describe 'calculate_has_case_plan' do
Expand Down
2 changes: 0 additions & 2 deletions spec/models/concerns/monitoring_reporting_mechanism_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@
incident3
incident4
incident5
Incident.reindex
Sunspot.commit
end

it 'can find incidents where individual victims are linked to a violation type' do
Expand Down
81 changes: 0 additions & 81 deletions spec/models/concerns/searchable_spec.rb

This file was deleted.

8 changes: 2 additions & 6 deletions spec/models/duplicate_bulk_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
require 'rails_helper'
require 'sunspot'

describe DuplicateBulkExport, search: true do
# TODO: Remove skip_when_solr_disabled once exporters are migrated
describe DuplicateBulkExport, { search: true, skip_when_solr_disabled: true } do
before :each do
clean_data(BulkExport, User, Agency, Role, Location, UserGroup, Field,
FormSection, Child, PrimeroModule, PrimeroProgram, SystemSettings,
Expand Down Expand Up @@ -69,7 +70,6 @@
create(:child, age: 5, name: 'Test Child 6')
@child1 = create(:child, national_id_no: 'test1', age: 5, name: 'Test Child 1')
@child2 = create(:child, national_id_no: 'test1', age: 6, name: 'Test Child 2')
Sunspot.commit
end

it 'export cases with duplicate ids' do
Expand All @@ -82,7 +82,6 @@
create(:child, national_id_no: 'test5', age: 3, name: 'Test Child 4')
create(:child, national_id_no: 'test5', age: 3, name: 'Test Child 4')
create(:child, national_id_no: 'test3', age: 3, name: 'Test Child 4')
Sunspot.commit
expect(export_csv.count).to eq(7)
ids_orderes = %w[test1 test3 test5]
export_csv[1..].each_with_index do |row, index|
Expand All @@ -96,7 +95,6 @@
before do
@child1 = create(:child, national_id_no: 'test-1', age: 5, name: 'Test Child 1')
@child2 = create(:child, national_id_no: 'test-1', age: 6, name: 'Test Child 2')
Sunspot.commit
end

it 'export cases with duplicate ids' do
Expand All @@ -114,7 +112,6 @@
create(:child, national_id_no: 'test3', age: 3, name: 'Test Child 4')
create(:child, age: 4, name: 'Test Child 5')
create(:child, age: 5, name: 'Test Child 6')
Sunspot.commit
end

it 'exports no cases' do
Expand All @@ -131,7 +128,6 @@
create(:child, age: 3, name: 'Test Child 4')
create(:child, age: 4, name: 'Test Child 5')
create(:child, age: 5, name: 'Test Child 6')
Sunspot.commit
end

it 'exports no cases' do
Expand Down
10 changes: 2 additions & 8 deletions spec/models/family_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@
end

describe 'quicksearch', search: true do
it 'has a searchable case id, survivor number' do
expected = %w[short_id family_id family_name family_number]
expect(Family.quicksearch_fields).to match_array(expected)
end

it 'can find a Family by Family Number' do
family = Family.create!(data: { family_name: 'Family One', family_number: 'ABC123XYZ' })
family.index!
search_result = SearchService.search(Family, query: 'ABC123XYZ').results
Family.create!(data: { family_name: 'Family One', family_number: 'ABC123XYZ' })
search_result = PhoneticSearchService.search(Family, query: 'ABC123XYZ').records
expect(search_result).to have(1).family
expect(search_result.first.family_number).to eq('ABC123XYZ')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/kpi/assessment_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require 'rails_helper'

describe Kpi::AssessmentStatus, search: true do
describe Kpi::AssessmentStatus, { search: true, skip_when_solr_disabled: true } do
include FormAndFieldHelper
include SunspotHelper

Expand Down
2 changes: 1 addition & 1 deletion spec/models/kpi/average_followup_meetings_per_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require 'rails_helper'

describe Kpi::AverageFollowupMeetingsPerCase, search: true do
describe Kpi::AverageFollowupMeetingsPerCase, { search: true, skip_when_solr_disabled: true } do
include FormAndFieldHelper
include SunspotHelper

Expand Down
2 changes: 1 addition & 1 deletion spec/models/kpi/average_referrals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require 'rails_helper'

describe Kpi::AverageReferrals, search: true do
describe Kpi::AverageReferrals, { search: true, skip_when_solr_disabled: true } do
include FormAndFieldHelper
include SunspotHelper

Expand Down
2 changes: 1 addition & 1 deletion spec/models/kpi/case_closure_rate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require 'rails_helper'

describe Kpi::CaseClosureRate, search: true do
describe Kpi::CaseClosureRate, { search: true, skip_when_solr_disabled: true } do
include FormAndFieldHelper
include SunspotHelper

Expand Down
Loading

0 comments on commit 2eb816c

Please sign in to comment.