From 1ff9d8785f74cdaeb9f080102e728025c9918d6b Mon Sep 17 00:00:00 2001 From: kevinrobinson Date: Mon, 4 Nov 2019 15:39:05 -0500 Subject: [PATCH] STAR: Fix test time since parsing is dependent on time zones, update v1 parsing to ET --- app/config_objects/per_district.rb | 18 ++----- .../importers/star/star_math_importer_spec.rb | 48 ++++++++++--------- .../star/star_reading_importer_spec.rb | 48 ++++++++++--------- 3 files changed, 56 insertions(+), 58 deletions(-) diff --git a/app/config_objects/per_district.rb b/app/config_objects/per_district.rb index 734162e34f..0c51c29243 100644 --- a/app/config_objects/per_district.rb +++ b/app/config_objects/per_district.rb @@ -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 diff --git a/spec/importers/star/star_math_importer_spec.rb b/spec/importers/star/star_math_importer_spec.rb index f0bc0ab30f..7861428051 100644 --- a/spec/importers/star/star_math_importer_spec.rb +++ b/spec/importers/star/star_math_importer_spec.rb @@ -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 diff --git a/spec/importers/star/star_reading_importer_spec.rb b/spec/importers/star/star_reading_importer_spec.rb index 8ecf4dfbde..6309093a65 100644 --- a/spec/importers/star/star_reading_importer_spec.rb +++ b/spec/importers/star/star_reading_importer_spec.rb @@ -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