Skip to content

Commit

Permalink
Tweak AR specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adzap committed Aug 21, 2012
1 parent 8168bcb commit 091e7ec
Showing 1 changed file with 76 additions and 20 deletions.
96 changes: 76 additions & 20 deletions spec/validates_timeliness/orm/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,34 @@
end

it 'should determine type for attribute' do
Employee.timeliness_attribute_type(:birth_date).should == :date
Employee.timeliness_attribute_type(:birth_date).should eq :date
end

context 'attribute timezone awareness' do
let(:klass) {
Class.new(ActiveRecord::Base) do
self.table_name = 'employees'
attr_accessor :some_date
attr_accessor :some_time
attr_accessor :some_datetime
validates_date :some_date
validates_time :some_time
validates_datetime :some_datetime
end
}

context 'for column attribute' do
it 'should be detected from column type' do
klass.timeliness_attribute_timezone_aware?(:birth_date).should be_false
klass.timeliness_attribute_timezone_aware?(:birth_time).should be_false
klass.timeliness_attribute_timezone_aware?(:birth_datetime).should be_true
end
end

context 'for non-column attribute' do
it 'should be detected from the validation type' do
klass.timeliness_attribute_timezone_aware?(:some_date).should be_false
klass.timeliness_attribute_timezone_aware?(:some_time).should be_false
klass.timeliness_attribute_timezone_aware?(:some_datetime).should be_true
end
end
Expand All @@ -74,23 +78,35 @@
class EmployeeWithCache < ActiveRecord::Base
self.table_name = 'employees'
validates_date :birth_date, :allow_blank => true
validates_time :birth_time, :allow_blank => true
validates_datetime :birth_datetime, :allow_blank => true
end

context 'value cache' do
context 'for datetime column' do
it 'should store raw value' do
r = EmployeeWithCache.new
r.birth_datetime = date_string = '2010-01-01'
r._timeliness_raw_value_for('birth_datetime').should == date_string
r.birth_datetime = datetime_string = '2010-01-01 12:30'

r._timeliness_raw_value_for('birth_datetime').should eq datetime_string
end
end

context 'for date column' do
it 'should store raw value' do
r = EmployeeWithCache.new
r.birth_date = date_string = '2010-01-01'
r._timeliness_raw_value_for('birth_date').should == date_string

r._timeliness_raw_value_for('birth_date').should eq date_string
end
end

context 'for time column' do
it 'should store raw value' do
r = EmployeeWithCache.new
r.birth_time = time_string = '12:12'

r._timeliness_raw_value_for('birth_time').should eq time_string
end
end
end
Expand All @@ -101,34 +117,74 @@ class EmployeeWithCache < ActiveRecord::Base
class EmployeeWithParser < ActiveRecord::Base
self.table_name = 'employees'
validates_date :birth_date, :allow_blank => true
validates_time :birth_time, :allow_blank => true
validates_datetime :birth_datetime, :allow_blank => true
end

it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
r = EmployeeWithParser.new
r.birth_date = '2010-01-01'
end
context "for a date column" do
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)

it 'should parse a invalid string value as nil' do
Timeliness::Parser.should_receive(:parse)
r = EmployeeWithParser.new
r.birth_date = 'not a date'
end
r = EmployeeWithParser.new
r.birth_date = '2010-01-01'
end

context "for a date column" do
it 'should store a date value after parsing string' do
it 'should parse a invalid string value as nil' do
Timeliness::Parser.should_receive(:parse)
r = EmployeeWithParser.new
r.birth_date = 'not valid'
end

it 'should store a Date value after parsing string' do
r = EmployeeWithParser.new
r.birth_date = '2010-01-01'

r.birth_date.should be_kind_of(Date)
r.birth_date.should == Date.new(2010, 1, 1)
r.birth_date.should eq Date.new(2010, 1, 1)
end
end

context "for a time column" do
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)

r = EmployeeWithParser.new
r.birth_time = '12:30'
end

it 'should parse a invalid string value as nil' do
Timeliness::Parser.should_receive(:parse)

r = EmployeeWithParser.new
r.birth_time = 'not valid'
end

it 'should store a Time value after parsing string' do
r = EmployeeWithParser.new
r.birth_time = '12:30'

r.birth_time.should be_kind_of(Time)
r.birth_time.should eq Time.utc(2000, 1, 1, 12, 30)
end
end

context "for a datetime column" do
with_config(:default_timezone, 'Australia/Melbourne')

it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)

r = EmployeeWithParser.new
r.birth_datetime = '2010-01-01 12:00'
end

it 'should parse a invalid string value as nil' do
Timeliness::Parser.should_receive(:parse)

r = EmployeeWithParser.new
r.birth_datetime = 'not valid'
end

it 'should parse string into Time value' do
r = EmployeeWithParser.new
r.birth_datetime = '2010-01-01 12:00'
Expand All @@ -140,7 +196,7 @@ class EmployeeWithParser < ActiveRecord::Base
r = EmployeeWithParser.new
r.birth_datetime = '2010-06-01 12:00'

r.birth_datetime.utc_offset.should == Time.zone.utc_offset
r.birth_datetime.utc_offset.should eq Time.zone.utc_offset
end
end
end
Expand All @@ -166,7 +222,7 @@ class EmployeeWithParser < ActiveRecord::Base
r = Employee.new
r.birth_datetime = date_string = '2010-01-01'

r.birth_datetime_before_type_cast.should == date_string
r.birth_datetime_before_type_cast.should eq date_string
end

it 'should return attribute if no attribute assignment has been made' do
Expand All @@ -184,7 +240,7 @@ class EmployeeWithParser < ActiveRecord::Base
r = Employee.new
r.birth_datetime = date_string = '2010-01-31'

r.birth_datetime_before_type_cast.should == date_string
r.birth_datetime_before_type_cast.should eq date_string
end
end

Expand Down

0 comments on commit 091e7ec

Please sign in to comment.