parent
e222edc7e7
commit
f5724407f2
@ -0,0 +1,68 @@
|
||||
require 'csv'
|
||||
|
||||
class SurveyResponsesDataLoader
|
||||
def self.load_data(filepath:)
|
||||
csv_file = File.read(filepath)
|
||||
|
||||
parsed_csv_file = CSV.parse(csv_file, headers: true)
|
||||
survey_items = parsed_csv_file.headers
|
||||
.filter { |header| !header.nil? }
|
||||
.filter { |header| header.start_with? 't-' }
|
||||
.map { |survey_item_id| SurveyItem.find_by_survey_item_id survey_item_id }
|
||||
|
||||
parsed_csv_file.each do |row|
|
||||
process_row row: row, survey_items: survey_items
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.process_row(row:, survey_items:)
|
||||
response_date = Date.parse(row['Recorded Date'])
|
||||
academic_year = academic_year date: response_date
|
||||
|
||||
response_id = row['Response ID']
|
||||
|
||||
school_code = row['school_code']
|
||||
return if school_code.nil?
|
||||
|
||||
school = school(row: row)
|
||||
return if school.nil?
|
||||
|
||||
return unless SurveyItemResponse.find_by_response_id(response_id).nil?
|
||||
|
||||
survey_items.each do |survey_item|
|
||||
likert_score = row[survey_item.survey_item_id]
|
||||
next if likert_score.nil?
|
||||
SurveyItemResponse.create(
|
||||
response_id: response_id,
|
||||
academic_year: academic_year,
|
||||
school: school,
|
||||
survey_item: survey_item,
|
||||
likert_score: likert_score
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def self.school(row:)
|
||||
district_code = row['district_code']
|
||||
school_code = row['school_code']
|
||||
return nil if school_code.nil?
|
||||
|
||||
School
|
||||
.where({district: District.find_by_qualtrics_code(district_code), qualtrics_code: school_code})
|
||||
.first
|
||||
end
|
||||
|
||||
def self.academic_year(date:)
|
||||
if date.month > 7
|
||||
ay_range_start = date.year
|
||||
ay_range_end = date.year + 1
|
||||
else
|
||||
ay_range_start = date.year - 1
|
||||
ay_range_end = date.year
|
||||
end
|
||||
AcademicYear.find_by_range("#{ay_range_start}-#{ay_range_end.to_s[2, 3]}")
|
||||
end
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe SurveyResponseAggregator, type: :model do
|
||||
describe SurveyResponseAggregator do
|
||||
let(:category) { SqmCategory.create }
|
||||
let(:subcategory) { Subcategory.create sqm_category: category }
|
||||
|
||||
@ -0,0 +1,66 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe SurveyResponsesDataLoader do
|
||||
let(:path_to_csv) { Rails.root.join('spec', 'fixtures', 'test_2020-21_teacher_survey_responses.csv') }
|
||||
|
||||
let(:ay_2020_21) { AcademicYear.find_by_range '2020-21' }
|
||||
|
||||
let(:attleboro_high_school) { School.find_by_slug 'attleboro-high-school' }
|
||||
|
||||
let(:t_pcom_q3) { SurveyItem.find_by_survey_item_id 't-pcom-q3' }
|
||||
let(:t_pcom_q2) { SurveyItem.find_by_survey_item_id 't-pcom-q2' }
|
||||
|
||||
describe 'self.load_data' do
|
||||
before :each do
|
||||
SurveyResponsesDataLoader.load_data filepath: path_to_csv
|
||||
end
|
||||
|
||||
it 'loads the csv data into the database' do
|
||||
expect(SurveyItemResponse.count).to be > 0
|
||||
end
|
||||
|
||||
it 'assigns the academic year to the survey item responses' do
|
||||
expect(SurveyItemResponse.first.academic_year).to eq ay_2020_21
|
||||
end
|
||||
|
||||
it 'assigns the school to the survey item responses' do
|
||||
expect(SurveyItemResponse.first.school).to eq attleboro_high_school
|
||||
end
|
||||
|
||||
it 'loads all the survey item responses for a given survey response' do
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_1').count).to eq 5
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_2').count).to eq 0
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_3').count).to eq 69
|
||||
end
|
||||
|
||||
it 'loads all the survey item responses for a given survey item' do
|
||||
expect(SurveyItemResponse.where(survey_item: t_pcom_q2).count).to eq 3
|
||||
expect(SurveyItemResponse.where(survey_item: t_pcom_q3).count).to eq 4
|
||||
end
|
||||
|
||||
it 'captures the likert scores for the survey item responses' do
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_1').where(survey_item: t_pcom_q2)).to be_empty
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_1').where(survey_item: t_pcom_q3).first.likert_score).to eq 3
|
||||
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_2').where(survey_item: t_pcom_q2)).to be_empty
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_2').where(survey_item: t_pcom_q3)).to be_empty
|
||||
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_3').where(survey_item: t_pcom_q2).first.likert_score).to eq 5
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_3').where(survey_item: t_pcom_q3).first.likert_score).to eq 5
|
||||
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_4').where(survey_item: t_pcom_q2).first.likert_score).to eq 4
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_4').where(survey_item: t_pcom_q3).first.likert_score).to eq 4
|
||||
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_5').where(survey_item: t_pcom_q2).first.likert_score).to eq 2
|
||||
expect(SurveyItemResponse.where(response_id: 'survey_response_5').where(survey_item: t_pcom_q3).first.likert_score).to eq 4
|
||||
end
|
||||
|
||||
it 'is idempotent, i.e. loading the data a second time does not duplicate survey item responses' do
|
||||
number_of_survey_item_responses = SurveyItemResponse.count
|
||||
|
||||
SurveyResponsesDataLoader.load_data filepath: path_to_csv
|
||||
|
||||
expect(SurveyItemResponse.count).to eq number_of_survey_item_responses
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue