From 915a4c1aaf525e1a4f2b0e210ebf5c1c8e13ab38 Mon Sep 17 00:00:00 2001 From: Liam Morley Date: Wed, 1 Dec 2021 12:37:25 -0500 Subject: [PATCH] Remove associated survey item responses when we remove schools --- app/models/school.rb | 2 ++ spec/factories.rb | 2 +- spec/lib/seeder_spec.rb | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/school.rb b/app/models/school.rb index 74efc564..10af82b4 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -1,6 +1,8 @@ class School < ApplicationRecord belongs_to :district + has_many :survey_item_responses, dependent: :delete_all + validates :name, presence: true scope :alphabetic, -> { order(name: :asc) } diff --git a/spec/factories.rb b/spec/factories.rb index aada3a63..491040a8 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -71,7 +71,7 @@ FactoryBot.define do response_id { rand.to_s } academic_year school - survey_item + survey_item factory: :teacher_survey_item end factory :admin_data_item do diff --git a/spec/lib/seeder_spec.rb b/spec/lib/seeder_spec.rb index 04d285a4..5aa63782 100644 --- a/spec/lib/seeder_spec.rb +++ b/spec/lib/seeder_spec.rb @@ -41,6 +41,7 @@ describe Seeder do context 'when partial data already exists' do let!(:existing_district) { create(:district, name: 'Boston') } let!(:removed_school) { create(:school, name: 'John Oldes Academy', dese_id: 12345, district: existing_district) } + let!(:removed_survey_item_response) { create(:survey_item_response, school: removed_school) } let!(:existing_school) { create(:school, name: 'Sam Adams Elementary School', dese_id: 350302, slug: 'some-slug-for-sam-adams', district: existing_district) } it 'only creates new districts and schools' do @@ -72,10 +73,11 @@ describe Seeder do expect(existing_school.slug).to eq 'some-slug-for-sam-adams' end - it 'removes any schools not contained within the CSV' do + it 'removes any schools and associated child objects not contained within the CSV' do seeder.seed_districts_and_schools sample_districts_and_schools_csv expect(School.where(id: removed_school)).not_to exist + expect(SurveyItemResponse.where(id: removed_survey_item_response)).not_to exist end end end