summaryrefslogtreecommitdiff
path: root/spec/migrations/update_historical_data_recorded_at_spec.rb
blob: bccc711f3399a24e564bdb16fa23f22ea045b5c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

require 'spec_helper'

require Rails.root.join('db', 'migrate', '20201022094846_update_historical_data_recorded_at.rb')

RSpec.describe UpdateHistoricalDataRecordedAt do
  let(:historical_data_table) { table(:historical_data) }

  it 'reversibly populates recorded_at from created_at or date' do
    row1 = historical_data_table.create!(
      date: Date.current - 1.day,
      created_at: Time.current - 1.day
    )

    row2 = historical_data_table.create!(date: Date.current - 2.days)
    row2.update!(created_at: nil)

    reversible_migration do |migration|
      migration.before -> {
        expect(row1.reload.recorded_at).to eq(nil)
        expect(row2.reload.recorded_at).to eq(nil)
      }

      migration.after -> {
        expect(row1.reload.recorded_at).to eq(row1.created_at)
        expect(row2.reload.recorded_at).to eq(row2.date.in_time_zone(Time.zone).change(hour: 12))
      }
    end
  end
end