require 'rails_helper'

RSpec.describe 'ActiveRecord::Relation#find_each_ordered' do
  it "preserves order" do
    oi1 = SourceFile.create!(current_at_day: '2017-12-25')
    oi2 = SourceFile.create!(current_at_day: '2017-02-04')
    oi3 = SourceFile.create!(current_at_day: '2017-09-25')

    scope = SourceFile.order(current_at_day: :asc)

    find_each_result = []
    scope.find_each{|r| find_each_result.push r.current_at_day.iso8601}
    find_each_result.should eq ['2017-12-25', '2017-02-04', '2017-09-25']

    find_each_ordered_result = []
    scope.find_each_ordered{|r| find_each_ordered_result.push r.current_at_day.iso8601}
    find_each_ordered_result.should eq ['2017-02-04', '2017-09-25', '2017-12-25']
  end

  it "works on a raw model class" do
    oi1 = SourceFile.create!(current_at_day: '2017-12-25')
    oi2 = SourceFile.create!(current_at_day: '2017-02-04')
    oi3 = SourceFile.create!(current_at_day: '2017-09-25')

    scope = SourceFile

    find_each_result = []
    scope.find_each{|r| find_each_result.push r.current_at_day.iso8601}
    find_each_result.should match_array ['2017-12-25', '2017-02-04', '2017-09-25']

    find_each_ordered_result = []
    scope.find_each_ordered{|r| find_each_ordered_result.push r.current_at_day.iso8601}
    find_each_ordered_result.should match_array ['2017-02-04', '2017-09-25', '2017-12-25']
  end
end
