mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 23:06:42 +00:00
added time_travel plugin for validation specs to fix time from Time.now
This commit is contained in:
parent
bf6aeea841
commit
494f3bdef4
@ -2,26 +2,31 @@ $: << File.dirname(__FILE__) + '/../lib' << File.dirname(__FILE__)
|
|||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'spec'
|
require 'spec'
|
||||||
|
require 'spec/interop/test'
|
||||||
|
|
||||||
if File.exists?(File.dirname(__FILE__) + '/../../../../vendor/rails')
|
vendored_rails = File.dirname(__FILE__) + '/../../../../vendor/rails'
|
||||||
$:.unshift File.dirname(__FILE__) + '/../../../../vendor/rails'
|
|
||||||
require 'activesupport/lib/active_support'
|
if vendored = File.exists?(vendored_rails)
|
||||||
require 'activerecord/lib/active_record'
|
Dir.glob(vendored_rails + "/**/lib").each { |dir| $:.unshift dir }
|
||||||
require 'activerecord/lib/active_record/version'
|
else
|
||||||
require 'railties/lib/rails/version'
|
|
||||||
vendored = true
|
|
||||||
else
|
|
||||||
gem 'rails', "=#{ENV['VERSION']}" if ENV['VERSION']
|
gem 'rails', "=#{ENV['VERSION']}" if ENV['VERSION']
|
||||||
require 'rails/version'
|
|
||||||
require 'active_record'
|
|
||||||
require 'active_record/version'
|
|
||||||
vendored = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{Rails::VERSION::STRING} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
|
require 'rails/version'
|
||||||
|
require 'active_record'
|
||||||
|
require 'active_record/version'
|
||||||
|
require 'action_controller'
|
||||||
|
require 'action_controller/test_process'
|
||||||
|
require 'action_view'
|
||||||
|
|
||||||
|
require 'rspec-rails'
|
||||||
|
require 'time_travel/time_travel'
|
||||||
|
require 'validates_timeliness'
|
||||||
|
|
||||||
RAILS_VER = Rails::VERSION::STRING
|
RAILS_VER = Rails::VERSION::STRING
|
||||||
|
|
||||||
|
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{RAILS_VER} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
|
||||||
|
|
||||||
ActiveRecord::Base.default_timezone = :utc
|
ActiveRecord::Base.default_timezone = :utc
|
||||||
|
|
||||||
if RAILS_VER >= '2.1'
|
if RAILS_VER >= '2.1'
|
||||||
@ -29,8 +34,6 @@ if RAILS_VER >= '2.1'
|
|||||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'validates_timeliness'
|
|
||||||
|
|
||||||
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
||||||
|
|
||||||
require 'resources/schema'
|
require 'resources/schema'
|
||||||
|
|||||||
1
spec/time_travel
Submodule
1
spec/time_travel
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 7d4e697b1cee90bb1f34ac302881ce0641e654ec
|
||||||
@ -1,6 +1,14 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
require File.dirname(__FILE__) + '/spec_helper'
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validations do
|
describe ValidatesTimeliness::Validations do
|
||||||
|
before :all do
|
||||||
|
Time.now = Time.utc(2008, 1, 1, 12, 0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
after :all do
|
||||||
|
Time.now = nil
|
||||||
|
end
|
||||||
|
|
||||||
describe "with no restrictions" do
|
describe "with no restrictions" do
|
||||||
before :all do
|
before :all do
|
||||||
class BasicValidation < Person
|
class BasicValidation < Person
|
||||||
@ -56,7 +64,7 @@ describe ValidatesTimeliness::Validations do
|
|||||||
before :all do
|
before :all do
|
||||||
class TimeBeforeAfter < Person
|
class TimeBeforeAfter < Person
|
||||||
validates_timeliness_of :birth_date_and_time, :before => Time.now, :after => 1.day.ago
|
validates_timeliness_of :birth_date_and_time, :before => Time.now, :after => 1.day.ago
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
before :each do
|
before :each do
|
||||||
@ -74,6 +82,18 @@ describe ValidatesTimeliness::Validations do
|
|||||||
@person.should_not be_valid
|
@person.should_not be_valid
|
||||||
@person.errors.on(:birth_date_and_time).should match(/must be after/)
|
@person.errors.on(:birth_date_and_time).should match(/must be after/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should have error when on boundary of :before restriction" do
|
||||||
|
@person.birth_date_and_time = Time.now
|
||||||
|
@person.should_not be_valid
|
||||||
|
@person.errors.on(:birth_date_and_time).should match(/must be before/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when on boundary of :after restriction" do
|
||||||
|
@person.birth_date_and_time = 1.day.ago
|
||||||
|
@person.should_not be_valid
|
||||||
|
@person.errors.on(:birth_date_and_time).should match(/must be after/)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with on_or_before and on_or_after restrictions" do
|
describe "with on_or_before and on_or_after restrictions" do
|
||||||
@ -83,18 +103,18 @@ describe ValidatesTimeliness::Validations do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
before :each do
|
before do
|
||||||
@person = TimeOnOrBeforeAndAfter.new
|
@person = TimeOnOrBeforeAndAfter.new
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have error when past :on_or_before restriction" do
|
it "should have error when past :on_or_before restriction" do
|
||||||
@person.birth_date_and_time = 1.minute.from_now
|
@person.birth_date_and_time = Time.now.at_midnight + 1
|
||||||
@person.should_not be_valid
|
@person.should_not be_valid
|
||||||
@person.errors.on(:birth_date_and_time).should match(/must be on or before/)
|
@person.errors.on(:birth_date_and_time).should match(/must be on or before/)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have error when before :on_or_after restriction" do
|
it "should have error when before :on_or_after restriction" do
|
||||||
@person.birth_date_and_time = 2.days.ago
|
@person.birth_date_and_time = 1.days.ago - 1
|
||||||
@person.should_not be_valid
|
@person.should_not be_valid
|
||||||
@person.errors.on(:birth_date_and_time).should match(/must be on or after/)
|
@person.errors.on(:birth_date_and_time).should match(/must be on or after/)
|
||||||
end
|
end
|
||||||
@ -107,7 +127,7 @@ describe ValidatesTimeliness::Validations do
|
|||||||
it "should be valid when value equal to :on_or_after restriction" do
|
it "should be valid when value equal to :on_or_after restriction" do
|
||||||
@person.birth_date_and_time = 1.day.ago
|
@person.birth_date_and_time = 1.day.ago
|
||||||
@person.should be_valid
|
@person.should be_valid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user