From 8082b5ce1cb1f14e6afd9623c3f0d0c266462230 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 22 Jul 2008 11:39:17 +1000 Subject: [PATCH] added specs for checking call to parse method to verify read and write attribute methods are defined --- spec/attribute_methods_spec.rb | 41 ++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/spec/attribute_methods_spec.rb b/spec/attribute_methods_spec.rb index 8d42965..7afe2b0 100644 --- a/spec/attribute_methods_spec.rb +++ b/spec/attribute_methods_spec.rb @@ -8,29 +8,39 @@ describe ValidatesTimeliness::AttributeMethods do @person = Person.new end - it "should return string value for attribute_before_type_cast when written as string" do - time_string = "2000-06-01 01:02:03" + it "should call parser on write for time attribute" do + @person.class.should_receive(:parse_date_time).once + @person.birth_date_and_time = "2000-06-01 02:03:04" + end + + it "should call parser on write for date attribute" do + @person.class.should_receive(:parse_date_time).once + @person.birth_date = "2000-06-01" + end + + it "should return raw string value for attribute_before_type_cast when written as string" do + time_string = "2000-06-01 02:03:04" @person.birth_date_and_time = time_string @person.birth_date_and_time_before_type_cast.should == time_string end it "should return Time object for attribute_before_type_cast when written as Time" do - @person.birth_date_and_time = Time.mktime(2000, 06, 01, 1, 2, 3) + @person.birth_date_and_time = Time.mktime(2000, 6, 1, 2, 3, 4) @person.birth_date_and_time_before_type_cast.should be_kind_of(Time) end it "should return Time object using attribute read method when written with string" do - @person.birth_date_and_time = "2000-06-01 01:02:03" + @person.birth_date_and_time = "2000-06-01 02:03:04" @person.birth_date_and_time.should be_kind_of(Time) end it "should return nil when time is invalid" do - @person.birth_date_and_time = "2000-02-30 01:02:03" + @person.birth_date_and_time = "2000-01-32 02:03:04" @person.birth_date_and_time.should be_nil end it "should not save invalid date value to database" do - time_string = "2000-02-30 09:00:00" + time_string = "2000-01-32 02:03:04" @person = Person.new @person.birth_date_and_time = time_string @person.save @@ -41,7 +51,7 @@ describe ValidatesTimeliness::AttributeMethods do unless RAILS_VER < '2.1' it "should return stored time string as Time with correct timezone" do Time.zone = 'Melbourne' - time_string = "2000-06-01 01:02:03" + time_string = "2000-06-01 02:03:04" @person.birth_date_and_time = time_string @person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000' end @@ -57,17 +67,30 @@ describe ValidatesTimeliness::AttributeMethods do end it "should return true for attribute changed?" do - time_string = "2000-06-01 01:02:03" + time_string = "2000-06-01 02:03:04" @person.birth_date_and_time = time_string @person.birth_date_and_time_changed?.should be_true end it "should show changes for time attribute as nil to Time object" do - time_string = "2000-06-01 01:02:03" + time_string = "2000-06-01 02:03:04" @person.birth_date_and_time = time_string time = @person.birth_date_and_time @person.changes.should == {"birth_date_and_time" => [nil, time]} end + + else + + it "should return time object from database in default timezone" do + ActiveRecord::Base.default_timezone = :utc + time_string = "2000-06-01 09:00:00" + @person = Person.new + @person.birth_date_and_time = time_string + @person.save + @person.reload + @person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z').should == time_string + ' GMT' + end + end it "should return same time object on repeat reads" do