From e5dd06bb90b28b14b9a476d999d22ca345dabf07 Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Thu, 11 Oct 2012 10:09:21 +1100 Subject: Rename models and tables exim -> mail_server --- spec/lib/timezone_fixes_spec.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'spec/lib/timezone_fixes_spec.rb') diff --git a/spec/lib/timezone_fixes_spec.rb b/spec/lib/timezone_fixes_spec.rb index eef7e865c..8dd1ff480 100644 --- a/spec/lib/timezone_fixes_spec.rb +++ b/spec/lib/timezone_fixes_spec.rb @@ -1,6 +1,6 @@ # This is a test of the monkey patches in timezone_fixes.rb -# We use EximLogDone here just as a totally random model that has a datetime type. +# We use MailServerLogDone here just as a totally random model that has a datetime type. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') @@ -10,8 +10,8 @@ describe "when doing things with timezones" do with_env_tz 'America/New_York' do with_active_record_default_timezone :utc do time = Time.local(2000) - exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - saved_time = EximLogDone.find(exim_log_done.id).last_stat + mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "EST"], time.to_a assert_equal [0, 0, 5, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a @@ -24,8 +24,8 @@ describe "when doing things with timezones" do with_active_record_default_timezone :utc do Time.use_zone 'Central Time (US & Canada)' do time = Time.zone.local(2000) - exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - saved_time = EximLogDone.find(exim_log_done.id).last_stat + mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a assert_equal [0, 0, 6, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a @@ -39,8 +39,8 @@ describe "when doing things with timezones" do #it "should preserve time objects with UTC time conversion to default timezone local" do # with_env_tz 'America/New_York' do # time = Time.utc(2000) - # exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - # saved_time = EximLogDone.find(exim_log_done.id).last_stat + # mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + # saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat # assert_equal time, saved_time # assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a # assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a @@ -52,8 +52,8 @@ describe "when doing things with timezones" do with_active_record_default_timezone :local do Time.use_zone 'Central Time (US & Canada)' do time = Time.zone.local(2000) - exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - saved_time = EximLogDone.find(exim_log_done.id).last_stat + mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a assert_equal [0, 0, 1, 1, 1, 2000, 6, 1, false, "EST"], saved_time.to_a -- cgit v1.2.3 From 85bd8e00a1af3c7de1514224a6b3ee47f9fc2d6a Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 25 Oct 2012 10:17:54 +0100 Subject: Fix spec - based on the spec description, what's being tested and the other specs, I assume that the activerecord default timezone is supposed to be set to :local as a precondition. Doing so makes the spec pass and make sense. --- spec/lib/timezone_fixes_spec.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'spec/lib/timezone_fixes_spec.rb') diff --git a/spec/lib/timezone_fixes_spec.rb b/spec/lib/timezone_fixes_spec.rb index 8dd1ff480..e8cf10b16 100644 --- a/spec/lib/timezone_fixes_spec.rb +++ b/spec/lib/timezone_fixes_spec.rb @@ -34,18 +34,18 @@ describe "when doing things with timezones" do end end - # XXX Couldn't get this test to work - but the other tests seem to detect presence of - # the monkey patch, so they will do for now. - #it "should preserve time objects with UTC time conversion to default timezone local" do - # with_env_tz 'America/New_York' do - # time = Time.utc(2000) - # mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') - # saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat - # assert_equal time, saved_time - # assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a - # assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a - # end - #end + it "should preserve time objects with UTC time conversion to default timezone local" do + with_env_tz 'America/New_York' do + with_active_record_default_timezone :local do + time = Time.utc(2000) + mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat + assert_equal time, saved_time + assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a + assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a + end + end + end it "should preserve time objects with time with zone conversion to default timezone local" do with_env_tz 'America/New_York' do -- cgit v1.2.3 From 218415e01624fdd33d577a9bd321516f8e4e4ee9 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 25 Oct 2012 11:03:57 +0100 Subject: Fix failing timezone specs. As we are now setting a value for timezone in config.timezone, this switches on ActiveRecord::time_zone_aware_attributes, so values are returned in the time zone specified (although still saved in whatever is specified in ActiveRecord.default_timezone in the db - in our case UTC). Previously with no timezone set, values would have been returned in UTC. So add some lines to look at the zone the data is saved in before the time_zone_aware_attributes kick in, and some lines to look at the values afterwards. Note that expected raw_saved time values are the same as the previous expected values for saved time (when there was no attribute time zone awareness), but that the time zone of values coming back on the model is set by config.time_zone and then overridden by Time.use_zone. --- spec/lib/timezone_fixes_spec.rb | 48 ++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'spec/lib/timezone_fixes_spec.rb') diff --git a/spec/lib/timezone_fixes_spec.rb b/spec/lib/timezone_fixes_spec.rb index e8cf10b16..a2bea5f64 100644 --- a/spec/lib/timezone_fixes_spec.rb +++ b/spec/lib/timezone_fixes_spec.rb @@ -1,68 +1,100 @@ # This is a test of the monkey patches in timezone_fixes.rb # We use MailServerLogDone here just as a totally random model that has a datetime type. - require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "when doing things with timezones" do - it "should preserve time objects with local time conversion to default timezone UTC" do + it "should preserve time objects with local time conversion to default timezone UTC + and return them as UTC times when config.time_zone is UTC" do with_env_tz 'America/New_York' do with_active_record_default_timezone :utc do time = Time.local(2000) mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + raw_saved_time = MailServerLogDone.find(mail_server_log_done.id).read_attribute(:last_stat) saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time + assert_equal saved_time, raw_saved_time + # Time is created in EST by local method (using ENV['TZ']) assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "EST"], time.to_a + # Due to :utc active_record_default_timezone, everything saved as UTC + assert_equal [0, 0, 5, 1, 1, 2000, 6, 1, false, "UTC"], raw_saved_time.to_a + # As config.time_zone is UTC (from config default), times returned in UTC assert_equal [0, 0, 5, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a end end end - it "should preserve time objects with time with zone conversion to default timezone UTC" do + it "should preserve time objects with time with zone conversion to default timezone UTC + and return them as local times in the zone set by Time.use_zone" do + with_env_tz 'America/New_York' do with_active_record_default_timezone :utc do Time.use_zone 'Central Time (US & Canada)' do time = Time.zone.local(2000) mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + raw_saved_time = MailServerLogDone.find(mail_server_log_done.id).read_attribute(:last_stat) saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time + assert_equal saved_time, raw_saved_time + # Time is created in CST by Time.local (as Time.zone has been set) assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a - assert_equal [0, 0, 6, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a + # Due to :utc active_record_default_timezone, everything saved as UTC + assert_equal [0, 0, 6, 1, 1, 2000, 6, 1, false, "UTC"], raw_saved_time.to_a + # Times returned in CST due to Time.use_zone and ActiveRecord::time_zone_aware_attributes + # being true + assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], saved_time.to_a end end end end - it "should preserve time objects with UTC time conversion to default timezone local" do + it "should preserve time objects with UTC time conversion to default timezone local + and return then as UTC times when config.time_zone is UTC" do with_env_tz 'America/New_York' do with_active_record_default_timezone :local do time = Time.utc(2000) mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + raw_saved_time = MailServerLogDone.find(mail_server_log_done.id).read_attribute(:last_stat) saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time + assert_equal saved_time, raw_saved_time + # Time is created in UTC by Time.utc method assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a - assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a + # Due to :local active_record_default_timezone, saved as EST + assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], raw_saved_time.to_a + # As config.time_zone is UTC (from config default), times returned in UTC + assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a end end end - it "should preserve time objects with time with zone conversion to default timezone local" do + it "should preserve time objects with time with zone conversion to default timezone local + and return them as local times in the zone set by Time.use_zone" do with_env_tz 'America/New_York' do with_active_record_default_timezone :local do Time.use_zone 'Central Time (US & Canada)' do time = Time.zone.local(2000) mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + raw_saved_time = MailServerLogDone.find(mail_server_log_done.id).read_attribute(:last_stat) saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time + assert_equal saved_time, raw_saved_time + # Time is created in CST by Time.zone.local assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a - assert_equal [0, 0, 1, 1, 1, 2000, 6, 1, false, "EST"], saved_time.to_a + # Due to :local active_record_default_timezone, saved as EST + assert_equal [0, 0, 1, 1, 1, 2000, 6, 1, false, "EST"], raw_saved_time.to_a + # Due to Time.use_zone, and ActiveRecord::time_zone_aware_attributes + # being true, time returned in CST + assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], saved_time.to_a end end end end + protected + def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz yield -- cgit v1.2.3