Sha256: 87a0f3cdfec678de0b2d23b01ef0b538ae3940e06aad046cf12127e860e30c08

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require 'date_ext'
require File.dirname(__FILE__) + '/test_helper.rb'

class ActsAsHistoricalWeekdayTest < ActiveSupport::TestCase
  load_schema 

  
  class Record < ActiveRecord::Base
    acts_as_historical
    def snapshot_date
      date = super
      date and date.to_weekday
    end
  end

  context 'named_scopes with 5 weekdays' do
    setup {
      Record.delete_all
      @fri = Weekday.new(2009,11,27)
      @mon = Weekday.new(2009,11,30)
      @tue = Weekday.new(2009,12,1)
      @wed = Weekday.new(2009,12,2)
      @thu = Weekday.new(2009,12,3)
    
      @r_fri = Record.create! :snapshot_date => @fri
      @r_mon = Record.create! :snapshot_date => @mon
      @r_tue = Record.create! :snapshot_date => @tue
      @r_wed = Record.create! :snapshot_date => @wed
      @r_thu = Record.create! :snapshot_date => @thu
    }

    context "snapshot_date" do
      should "return weekday" do
        assert_equal @mon, Record.find(@r_mon).snapshot_date
      end
    end

    context "snapshots over weekend" do
      should "return friday as previous of monday" do
        assert_equal @r_fri, @r_mon.previous
      end

      should "return monday as next of friday" do
        assert_equal @r_mon, @r_fri.next
      end

      should "include friday, monday, tuesday for nearest(monday, 1)" do
        results = Record.nearest(@mon,1)
        assert_equal 3, results.length
        assert results.include?(@r_mon)
        assert results.include?(@r_tue)
        assert results.include?(@r_fri)
      end
    end
    
    context "tolerance over week" do
      should "work" do
        Record.tolerance_to_range(@mon, 1).include?(@fri)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_historical-0.1.5 test/weekday_test.rb
acts_as_historical-0.1.4 test/weekday_test.rb