Tweak AR specs

This commit is contained in:
Adam Meehan 2012-08-21 10:53:54 +10:00
parent 8168bcbd3a
commit 091e7ecfa0

View File

@ -41,7 +41,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
@ -49,8 +49,10 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
}
@ -58,6 +60,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
@ -65,6 +68,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
@ -74,6 +78,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
@ -81,8 +86,9 @@ describe ValidatesTimeliness, 'ActiveRecord' 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
@ -90,7 +96,17 @@ describe ValidatesTimeliness, 'ActiveRecord' 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
@ -101,34 +117,74 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
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
context "for a date column" do
it 'should store a date value after parsing string' do
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
r = EmployeeWithParser.new
r.birth_date = '2010-01-01'
end
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'
@ -140,7 +196,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
@ -166,7 +222,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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
@ -184,7 +240,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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