Skip to content

Commit

Permalink
Merge pull request #2692 from studentinsights/patch/fix-timezone-for-…
Browse files Browse the repository at this point in the history
…star-importer-update-to-et

STAR: Fix test time since parsing is dependent on time zones, update v1 parsing to ET
  • Loading branch information
kevinrobinson committed Nov 4, 2019
2 parents bca7659 + ab6b59d commit 11a2393
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 58 deletions.
18 changes: 4 additions & 14 deletions app/config_objects/per_district.rb
Expand Up @@ -96,21 +96,11 @@ def parsed_normalized_star_row(row)
end
end

# This is pretty old, but especially with the fixed `CDT` string here
# could probably be simplified or improved.
# Parse in EDT/EST, depending on parse date (probably should be depending on
# the date itself. In earlier iterations this parsing was fixed to CDT.
def parse_star_date_taken_with_v1_format(datetime_string)
day = DateTime.strptime(datetime_string, "%m/%d/%Y")
time = Time.strptime("#{datetime_string} CDT", "%m/%d/%Y %H:%M:%S %Z")

# Merge together data from DateTime and Time because:
# * The Ruby `Time` class handles timezones properly (DateTime has issues)
# * The Ruby `DateTime` class stores dates properly
# * The only way I found to successfully parse and store this data was to
# use both classes and merge the results together
DateTime.new(
day.year, day.month, day.day,
time.hour, time.min, time.sec, time.formatted_offset
)
eastern_offset = Time.now.in_time_zone('Eastern Time (US & Canada)').formatted_offset
DateTime.strptime("#{datetime_string} #{eastern_offset}", '%m/%d/%Y %H:%M:%S %Z')
end

# Remove microseconds manually, since I can't figure out how to get Ruby
Expand Down
48 changes: 26 additions & 22 deletions spec/importers/star/star_math_importer_spec.rb
Expand Up @@ -37,31 +37,35 @@ def mock_for_fixture!(district_key, local_fixture_filename)
let!(:student) { FactoryBot.create(:student, local_id: '10', school: pals.west) }

it 'works for v2 in somerville' do
importer, log = create_mocked_importer(PerDistrict::SOMERVILLE, "#{Rails.root}/spec/importers/star/star_math_v2.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarMathResult.all.size).to eq(1)
expect(StarMathResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 13, 18, 27), # stored in UTC
"percentile_rank"=>70,
"total_time"=>600,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
Timecop.freeze(pals.time_now) do
importer, log = create_mocked_importer(PerDistrict::SOMERVILLE, "#{Rails.root}/spec/importers/star/star_math_v2.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarMathResult.all.size).to eq(1)
expect(StarMathResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 13, 18, 27), # parsed as EDT/EST, stored in UTC
"percentile_rank"=>70,
"total_time"=>600,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
end
end

it 'works for v1 in new_bedford' do
importer, log = create_mocked_importer(PerDistrict::NEW_BEDFORD, "#{Rails.root}/spec/importers/star/star_math_v1.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarMathResult.all.size).to eq(1)
expect(StarMathResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 14, 18, 27), # stored in UTC
"percentile_rank"=>70,
"total_time"=>600,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
Timecop.freeze(pals.time_now) do
importer, log = create_mocked_importer(PerDistrict::NEW_BEDFORD, "#{Rails.root}/spec/importers/star/star_math_v1.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarMathResult.all.size).to eq(1)
expect(StarMathResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 13, 18, 27), # parsed as EDT/EST, stored in UTC
"percentile_rank"=>70,
"total_time"=>600,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
end
end

it 'handles bad data (v2 as example)' do
Expand Down
48 changes: 26 additions & 22 deletions spec/importers/star/star_reading_importer_spec.rb
Expand Up @@ -37,31 +37,35 @@ def mock_for_fixture!(district_key, local_fixture_filename)
let!(:student) { FactoryBot.create(:student, local_id: '10', school: pals.west) }

it 'works for v2 format in somerville' do
importer, log = create_mocked_importer(PerDistrict::SOMERVILLE, "#{Rails.root}/spec/importers/star/star_reading_v2.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarReadingResult.all.size).to eq(1)
expect(StarReadingResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 13, 18, 27), # parsed as EDT/EST, stored in UTC
"percentile_rank"=>90,
"total_time"=>710,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
Timecop.freeze(pals.time_now) do
importer, log = create_mocked_importer(PerDistrict::SOMERVILLE, "#{Rails.root}/spec/importers/star/star_reading_v2.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarReadingResult.all.size).to eq(1)
expect(StarReadingResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 13, 18, 27), # parsed as EDT/EST, stored in UTC
"percentile_rank"=>90,
"total_time"=>710,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
end
end

it 'works for v1 format in new bedford' do
importer, log = create_mocked_importer(PerDistrict::NEW_BEDFORD, "#{Rails.root}/spec/importers/star/star_reading_v1.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarReadingResult.all.size).to eq(1)
expect(StarReadingResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 14, 18, 27), # parsed as CDT(?), stored in UTC
"percentile_rank"=>90,
"total_time"=>710,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
Timecop.freeze(pals.time_now) do
importer, log = create_mocked_importer(PerDistrict::NEW_BEDFORD, "#{Rails.root}/spec/importers/star/star_reading_v1.csv")
importer.import
expect(log.output).to include(':processed_rows_count=>1')
expect(StarReadingResult.all.size).to eq(1)
expect(StarReadingResult.first.as_json(except: [:id, :created_at, :updated_at])).to eq({
"date_taken"=>DateTime.new(2015, 1, 21, 13, 18, 27), # parsed as EDT/EST, stored in UTC
"percentile_rank"=>90,
"total_time"=>710,
"grade_equivalent"=>"1.00",
"student_id"=>student.id
})
end
end

it 'skips and logs bad data (v2 as example)' do
Expand Down

0 comments on commit 11a2393

Please sign in to comment.