Remove old schools during the seed process

pull/1/head
Liam Morley 4 years ago
parent f81874d082
commit 75c3a1fccc

@ -8,10 +8,12 @@ class Seeder
end end
def seed_districts_and_schools csv_file def seed_districts_and_schools csv_file
dese_ids = []
CSV.parse(File.read(csv_file), headers: true) do |row| CSV.parse(File.read(csv_file), headers: true) do |row|
district_name = row['District'].strip district_name = row['District'].strip
district_code = row['District Code'].try(:strip) district_code = row['District Code'].try(:strip)
dese_id = row['DESE School ID'].strip dese_id = row['DESE School ID'].strip
dese_ids << dese_id
school_name = row['School Name'].strip school_name = row['School Name'].strip
school_code = row['School Code'].try(:strip) school_code = row['School Code'].try(:strip)
@ -23,6 +25,8 @@ class Seeder
school.qualtrics_code = school_code school.qualtrics_code = school_code
school.save! school.save!
end end
School.where.not(dese_id: dese_ids).destroy_all
end end
def seed_sqm_framework csv_file def seed_sqm_framework csv_file

@ -31,44 +31,27 @@ describe Seeder do
School.delete_all School.delete_all
end end
it 'seeds new districts' do it 'seeds new districts and schools' do
expect { expect {
seeder.seed_districts_and_schools sample_districts_and_schools_csv seeder.seed_districts_and_schools sample_districts_and_schools_csv
}.to change { District.count }.by(2) }.to change { District.count }.by(2)
end .and change { School.count }.by(2)
it 'seeds new schools' do
expect {
seeder.seed_districts_and_schools sample_districts_and_schools_csv
}.to change { School.count }.by(2)
end end
context 'when partial data already exists' do context 'when partial data already exists' do
let!(:existing_district) { create(:district, name: 'Boston') } let!(:existing_district) { create(:district, name: 'Boston') }
let!(:removed_school) { create(:school, name: 'John Oldes Academy', dese_id: 12345, district: existing_district) }
let!(:existing_school) { create(:school, name: 'Sam Adams Elementary School', dese_id: 350302, slug: 'some-slug-for-sam-adams', district: existing_district) } 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' do it 'only creates new districts and schools' do
expect { expect {
seeder.seed_districts_and_schools sample_districts_and_schools_csv seeder.seed_districts_and_schools sample_districts_and_schools_csv
}.to change { District.count }.by(1) }.to change { District.count }.by(1)
.and change { School.count }.by(0) # +1 for new school, -1 for old school
new_district = District.find_by_name 'Attleboro' new_district = District.find_by_name 'Attleboro'
expect(new_district.qualtrics_code).to eq 1 expect(new_district.qualtrics_code).to eq 1
expect(new_district.slug).to eq 'attleboro' expect(new_district.slug).to eq 'attleboro'
end
it 'updates existing districts with the correct data' do
seeder.seed_districts_and_schools sample_districts_and_schools_csv
existing_district.reload
expect(existing_district.qualtrics_code).to eq 2
expect(existing_district.slug).to eq 'boston'
end
it 'only creates new schools' do
expect {
seeder.seed_districts_and_schools sample_districts_and_schools_csv
}.to change { School.count }.by(1)
new_school = School.find_by_name 'Attleboro High School' new_school = School.find_by_name 'Attleboro High School'
expect(new_school.dese_id).to eq 160505 expect(new_school.dese_id).to eq 160505
@ -76,14 +59,24 @@ describe Seeder do
expect(new_school.slug).to eq 'attleboro-high-school' expect(new_school.slug).to eq 'attleboro-high-school'
end end
it 'updates existing schools with the qualtrics code' do it 'updates existing districts and schools with the correct data' do
seeder.seed_districts_and_schools sample_districts_and_schools_csv seeder.seed_districts_and_schools sample_districts_and_schools_csv
existing_district.reload
expect(existing_district.qualtrics_code).to eq 2
expect(existing_district.slug).to eq 'boston'
existing_school.reload existing_school.reload
expect(existing_school.qualtrics_code).to eq 1 expect(existing_school.qualtrics_code).to eq 1
expect(existing_school.name).to eq 'Samuel Adams Elementary School' expect(existing_school.name).to eq 'Samuel Adams Elementary School'
expect(existing_school.slug).to eq 'some-slug-for-sam-adams' expect(existing_school.slug).to eq 'some-slug-for-sam-adams'
end end
it 'removes any schools 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
end
end end
end end
@ -99,7 +92,7 @@ describe Seeder do
it 'creates new objects as necessary' do it 'creates new objects as necessary' do
expect { expect {
seeder.seed_sqm_framework sample_sqm_framework_csv seeder.seed_sqm_framework sample_sqm_framework_csv
}.to change { Category.count }.by(4) }.to change { Category.count }.by(4)
.and change { Subcategory.count }.by(15) .and change { Subcategory.count }.by(15)
.and change { Measure.count }.by(31) .and change { Measure.count }.by(31)
.and change { SurveyItem.count }.by(136) .and change { SurveyItem.count }.by(136)
@ -112,7 +105,6 @@ describe Seeder do
end end
it 'updates category data' do it 'updates category data' do
seeder.seed_sqm_framework sample_sqm_framework_csv
teachers_leadership = Category.find_by_name 'Teachers & Leadership' teachers_leadership = Category.find_by_name 'Teachers & Leadership'
expect(teachers_leadership.slug).to eq 'teachers-and-leadership' expect(teachers_leadership.slug).to eq 'teachers-and-leadership'
@ -167,5 +159,4 @@ describe Seeder do
def sample_sqm_framework_csv def sample_sqm_framework_csv
Rails.root.join('spec', 'fixtures', 'sample_sqm_framework.csv') Rails.root.join('spec', 'fixtures', 'sample_sqm_framework.csv')
end end
end end

Loading…
Cancel
Save