Skip to content
This repository
Browse code

prevent global timezone state from leaking out of test cases.

  • Loading branch information...
commit abe91cb3bf1b408a23958e14a68077e56a5a3b84 1 parent 3e5bb86
Yves Senn senny authored
40 activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -589,38 +589,28 @@ def test_update_oid
589 589 end
590 590
591 591 def test_timestamp_with_zone_values_with_rails_time_zone_support
592   - old_tz = ActiveRecord::Base.time_zone_aware_attributes
593   - old_default_tz = ActiveRecord::Base.default_timezone
  592 + with_timezone_config default: :utc, aware_attributes: true do
  593 + @connection.reconnect!
594 594
595   - ActiveRecord::Base.time_zone_aware_attributes = true
596   - ActiveRecord::Base.default_timezone = :utc
597   -
598   - @connection.reconnect!
599   -
600   - @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
601   - assert_equal Time.utc(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
602   - assert_instance_of Time, @first_timestamp_with_zone.time
  595 + @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
  596 + assert_equal Time.utc(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
  597 + assert_instance_of Time, @first_timestamp_with_zone.time
  598 + end
603 599 ensure
604   - ActiveRecord::Base.default_timezone = old_default_tz
605   - ActiveRecord::Base.time_zone_aware_attributes = old_tz
606 600 @connection.reconnect!
607 601 end
608 602
609 603 def test_timestamp_with_zone_values_without_rails_time_zone_support
610   - old_tz = ActiveRecord::Base.time_zone_aware_attributes
611   - old_default_tz = ActiveRecord::Base.default_timezone
612   -
613   - ActiveRecord::Base.time_zone_aware_attributes = false
614   - ActiveRecord::Base.default_timezone = :local
615   -
616   - @connection.reconnect!
617   -
618   - @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
619   - assert_equal Time.local(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
620   - assert_instance_of Time, @first_timestamp_with_zone.time
  604 + with_timezone_config default: :local, aware_attributes: false do
  605 + @connection.reconnect!
  606 + # make sure to use a non-UTC time zone
  607 + @connection.execute("SET time zone 'America/Jamaica'", 'SCHEMA')
  608 +
  609 + @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
  610 + assert_equal Time.utc(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
  611 + assert_instance_of Time, @first_timestamp_with_zone.time
  612 + end
621 613 ensure
622   - ActiveRecord::Base.default_timezone = old_default_tz
623   - ActiveRecord::Base.time_zone_aware_attributes = old_tz
624 614 @connection.reconnect!
625 615 end
626 616 end
97 activerecord/test/cases/base_test.rb
@@ -78,12 +78,6 @@ def setup
78 78 class BasicsTest < ActiveRecord::TestCase
79 79 fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts
80 80
81   - def setup
82   - ActiveRecord::Base.time_zone_aware_attributes = false
83   - ActiveRecord::Base.default_timezone = :local
84   - Time.zone = nil
85   - end
86   -
87 81 def test_generated_methods_modules
88 82 modules = Computer.ancestors
89 83 assert modules.include?(Computer::GeneratedFeatureMethods)
@@ -234,7 +228,7 @@ def test_preserving_time_objects
234 228
235 229 def test_preserving_time_objects_with_local_time_conversion_to_default_timezone_utc
236 230 with_env_tz 'America/New_York' do
237   - with_active_record_default_timezone :utc do
  231 + with_timezone_config default: :utc do
238 232 time = Time.local(2000)
239 233 topic = Topic.create('written_on' => time)
240 234 saved_time = Topic.find(topic.id).reload.written_on
@@ -247,7 +241,7 @@ def test_preserving_time_objects_with_local_time_conversion_to_default_timezone_
247 241
248 242 def test_preserving_time_objects_with_time_with_zone_conversion_to_default_timezone_utc
249 243 with_env_tz 'America/New_York' do
250   - with_active_record_default_timezone :utc do
  244 + with_timezone_config default: :utc do
251 245 Time.use_zone 'Central Time (US & Canada)' do
252 246 time = Time.zone.local(2000)
253 247 topic = Topic.create('written_on' => time)
@@ -262,18 +256,20 @@ def test_preserving_time_objects_with_time_with_zone_conversion_to_default_timez
262 256
263 257 def test_preserving_time_objects_with_utc_time_conversion_to_default_timezone_local
264 258 with_env_tz 'America/New_York' do
265   - time = Time.utc(2000)
266   - topic = Topic.create('written_on' => time)
267   - saved_time = Topic.find(topic.id).reload.written_on
268   - assert_equal time, saved_time
269   - assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a
270   - assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a
  259 + with_timezone_config default: :local do
  260 + time = Time.utc(2000)
  261 + topic = Topic.create('written_on' => time)
  262 + saved_time = Topic.find(topic.id).reload.written_on
  263 + assert_equal time, saved_time
  264 + assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a
  265 + assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a
  266 + end
271 267 end
272 268 end
273 269
274 270 def test_preserving_time_objects_with_time_with_zone_conversion_to_default_timezone_local
275 271 with_env_tz 'America/New_York' do
276   - with_active_record_default_timezone :local do
  272 + with_timezone_config default: :local do
277 273 Time.use_zone 'Central Time (US & Canada)' do
278 274 time = Time.zone.local(2000)
279 275 topic = Topic.create('written_on' => time)
@@ -493,25 +489,25 @@ def test_default_values
493 489 # Oracle, and Sybase do not have a TIME datatype.
494 490 unless current_adapter?(:OracleAdapter, :SybaseAdapter)
495 491 def test_utc_as_time_zone
496   - Topic.default_timezone = :utc
497   - attributes = { "bonus_time" => "5:42:00AM" }
498   - topic = Topic.find(1)
499   - topic.attributes = attributes
500   - assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
501   - Topic.default_timezone = :local
  492 + with_timezone_config default: :utc do
  493 + attributes = { "bonus_time" => "5:42:00AM" }
  494 + topic = Topic.find(1)
  495 + topic.attributes = attributes
  496 + assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
  497 + end
502 498 end
503 499
504 500 def test_utc_as_time_zone_and_new
505   - Topic.default_timezone = :utc
506   - attributes = { "bonus_time(1i)"=>"2000",
507   - "bonus_time(2i)"=>"1",
508   - "bonus_time(3i)"=>"1",
509   - "bonus_time(4i)"=>"10",
510   - "bonus_time(5i)"=>"35",
511   - "bonus_time(6i)"=>"50" }
512   - topic = Topic.new(attributes)
513   - assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time
514   - Topic.default_timezone = :local
  501 + with_timezone_config default: :utc do
  502 + attributes = { "bonus_time(1i)"=>"2000",
  503 + "bonus_time(2i)"=>"1",
  504 + "bonus_time(3i)"=>"1",
  505 + "bonus_time(4i)"=>"10",
  506 + "bonus_time(5i)"=>"35",
  507 + "bonus_time(6i)"=>"50" }
  508 + topic = Topic.new(attributes)
  509 + assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time
  510 + end
515 511 end
516 512 end
517 513
@@ -634,12 +630,14 @@ def test_attributes_on_dummy_time
634 630 # Oracle, and Sybase do not have a TIME datatype.
635 631 return true if current_adapter?(:OracleAdapter, :SybaseAdapter)
636 632
637   - attributes = {
638   - "bonus_time" => "5:42:00AM"
639   - }
640   - topic = Topic.find(1)
641   - topic.attributes = attributes
642   - assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time
  633 + with_timezone_config default: :local do
  634 + attributes = {
  635 + "bonus_time" => "5:42:00AM"
  636 + }
  637 + topic = Topic.find(1)
  638 + topic.attributes = attributes
  639 + assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time
  640 + end
643 641 end
644 642
645 643 def test_attributes_on_dummy_time_with_invalid_time
@@ -827,19 +825,18 @@ def test_bignum
827 825 # TODO: extend defaults tests to other databases!
828 826 if current_adapter?(:PostgreSQLAdapter)
829 827 def test_default
830   - tz = Default.default_timezone
831   - Default.default_timezone = :local
832   - default = Default.new
833   - Default.default_timezone = tz
834   -
835   - # fixed dates / times
836   - assert_equal Date.new(2004, 1, 1), default.fixed_date
837   - assert_equal Time.local(2004, 1,1,0,0,0,0), default.fixed_time
838   -
839   - # char types
840   - assert_equal 'Y', default.char1
841   - assert_equal 'a varchar field', default.char2
842   - assert_equal 'a text field', default.char3
  828 + with_timezone_config default: :local do
  829 + default = Default.new
  830 +
  831 + # fixed dates / times
  832 + assert_equal Date.new(2004, 1, 1), default.fixed_date
  833 + assert_equal Time.local(2004, 1,1,0,0,0,0), default.fixed_time
  834 +
  835 + # char types
  836 + assert_equal 'Y', default.char1
  837 + assert_equal 'a varchar field', default.char2
  838 + assert_equal 'a text field', default.char3
  839 + end
843 840 end
844 841
845 842 class Geometric < ActiveRecord::Base; end
8 activerecord/test/cases/column_test.rb
@@ -112,13 +112,11 @@ def test_type_cast_duration_to_integer
112 112 end
113 113
114 114 def test_string_to_time_with_timezone
115   - old = ActiveRecord::Base.default_timezone
116 115 [:utc, :local].each do |zone|
117   - ActiveRecord::Base.default_timezone = zone
118   - assert_equal Time.utc(2013, 9, 4, 0, 0, 0), Column.string_to_time("Wed, 04 Sep 2013 03:00:00 EAT")
  116 + with_timezone_config default: zone do
  117 + assert_equal Time.utc(2013, 9, 4, 0, 0, 0), Column.string_to_time("Wed, 04 Sep 2013 03:00:00 EAT")
  118 + end
119 119 end
120   - rescue
121   - ActiveRecord::Base.default_timezone = old
122 120 end
123 121 end
124 122 end
2  activerecord/test/cases/date_time_test.rb
@@ -5,7 +5,7 @@
5 5 class DateTimeTest < ActiveRecord::TestCase
6 6 def test_saves_both_date_and_time
7 7 with_env_tz 'America/New_York' do
8   - with_active_record_default_timezone :utc do
  8 + with_timezone_config default: :utc do
9 9 time_values = [1807, 2, 10, 15, 30, 45]
10 10 # create DateTime value with local time zone offset
11 11 local_offset = Rational(Time.local(*time_values).utc_offset, 86400)
8 activerecord/test/cases/finder_test.rb
@@ -479,7 +479,7 @@ def test_hash_condition_find_with_one_condition_being_aggregate_and_another_not
479 479
480 480 def test_condition_utc_time_interpolation_with_default_timezone_local
481 481 with_env_tz 'America/New_York' do
482   - with_active_record_default_timezone :local do
  482 + with_timezone_config default: :local do
483 483 topic = Topic.first
484 484 assert_equal topic, Topic.all.merge!(:where => ['written_on = ?', topic.written_on.getutc]).first
485 485 end
@@ -488,7 +488,7 @@ def test_condition_utc_time_interpolation_with_default_timezone_local
488 488
489 489 def test_hash_condition_utc_time_interpolation_with_default_timezone_local
490 490 with_env_tz 'America/New_York' do
491   - with_active_record_default_timezone :local do
  491 + with_timezone_config default: :local do
492 492 topic = Topic.first
493 493 assert_equal topic, Topic.all.merge!(:where => {:written_on => topic.written_on.getutc}).first
494 494 end
@@ -497,7 +497,7 @@ def test_hash_condition_utc_time_interpolation_with_default_timezone_local
497 497
498 498 def test_condition_local_time_interpolation_with_default_timezone_utc
499 499 with_env_tz 'America/New_York' do
500   - with_active_record_default_timezone :utc do
  500 + with_timezone_config default: :utc do
501 501 topic = Topic.first
502 502 assert_equal topic, Topic.all.merge!(:where => ['written_on = ?', topic.written_on.getlocal]).first
503 503 end
@@ -506,7 +506,7 @@ def test_condition_local_time_interpolation_with_default_timezone_utc
506 506
507 507 def test_hash_condition_local_time_interpolation_with_default_timezone_utc
508 508 with_env_tz 'America/New_York' do
509   - with_active_record_default_timezone :utc do
  509 + with_timezone_config default: :utc do
510 510 topic = Topic.first
511 511 assert_equal topic, Topic.all.merge!(:where => {:written_on => topic.written_on.getlocal}).first
512 512 end
50 activerecord/test/cases/helper.rb
@@ -49,11 +49,55 @@ def with_env_tz(new_tz = 'US/Eastern')
49 49 old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
50 50 end
51 51
52   -def with_active_record_default_timezone(zone)
53   - old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone
  52 +def with_timezone_config(cfg)
  53 + verify_default_timezone_config
  54 +
  55 + old_default_zone = ActiveRecord::Base.default_timezone
  56 + old_awareness = ActiveRecord::Base.time_zone_aware_attributes
  57 + old_zone = Time.zone
  58 +
  59 + if cfg.has_key?(:default)
  60 + ActiveRecord::Base.default_timezone = cfg[:default]
  61 + end
  62 + if cfg.has_key?(:aware_attributes)
  63 + ActiveRecord::Base.time_zone_aware_attributes = cfg[:aware_attributes]
  64 + end
  65 + if cfg.has_key?(:zone)
  66 + Time.zone = cfg[:zone]
  67 + end
54 68 yield
55 69 ensure
56   - ActiveRecord::Base.default_timezone = old_zone
  70 + ActiveRecord::Base.default_timezone = old_default_zone
  71 + ActiveRecord::Base.time_zone_aware_attributes = old_awareness
  72 + Time.zone = old_zone
  73 +end
  74 +
  75 +# This method makes sure that tests don't leak global state related to time zones.
  76 +EXPECTED_ZONE = nil
  77 +EXPECTED_DEFAULT_TIMEZONE = :utc
  78 +EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES = false
  79 +def verify_default_timezone_config
  80 + if Time.zone != EXPECTED_ZONE
  81 + raise <<-MSG
  82 + Global state `Time.zone` was leaked.
  83 + Expected: #{EXPECTED_ZONE}
  84 + Got: #{Time.zone}
  85 + MSG
  86 + end
  87 + if ActiveRecord::Base.default_timezone != EXPECTED_DEFAULT_TIMEZONE
  88 + raise <<-MSG
  89 + Global state `ActiveRecord::Base.default_timezone` was leaked.
  90 + Expected: #{EXPECTED_DEFAULT_TIMEZONE}
  91 + Got: #{ActiveRecord::Base.default_timezone}
  92 + MSG
  93 + end
  94 + if ActiveRecord::Base.time_zone_aware_attributes != EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES
  95 + raise <<-MSG
  96 + Global state `ActiveRecord::Base.time_zone_aware_attributes` was leaked.
  97 + Expected: #{EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES}
  98 + Got: #{ActiveRecord::Base.time_zone_aware_attributes}
  99 + MSG
  100 + end
57 101 end
58 102
59 103 unless ENV['FIXTURE_DEBUG']
13 activerecord/test/cases/integration_test.rb
@@ -23,17 +23,12 @@ def test_to_param_returns_id_if_not_persisted_but_id_is_set
23 23 end
24 24
25 25 def test_cache_key_for_existing_record_is_not_timezone_dependent
26   - ActiveRecord::Base.time_zone_aware_attributes = true
27   -
28   - Time.zone = 'UTC'
29 26 utc_key = Developer.first.cache_key
30 27
31   - Time.zone = 'EST'
32   - est_key = Developer.first.cache_key
33   -
34   - assert_equal utc_key, est_key
35   - ensure
36   - Time.zone = 'UTC'
  28 + with_timezone_config zone: "EST" do
  29 + est_key = Developer.first.cache_key
  30 + assert_equal utc_key, est_key
  31 + end
37 32 end
38 33
39 34 def test_cache_key_format_for_existing_record_with_updated_at
160 activerecord/test/cases/multiparameter_attributes_test.rb
@@ -5,16 +5,6 @@
5 5 class MultiParameterAttributeTest < ActiveRecord::TestCase
6 6 fixtures :topics
7 7
8   - def setup
9   - ActiveRecord::Base.time_zone_aware_attributes = false
10   - ActiveRecord::Base.default_timezone = :local
11   - Time.zone = nil
12   - end
13   -
14   - def teardown
15   - ActiveRecord::Base.default_timezone = :utc
16   - end
17   -
18 8 def test_multiparameter_attributes_on_date
19 9 attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" }
20 10 topic = Topic.find(1)
@@ -86,13 +76,15 @@ def test_multiparameter_attributes_on_date_with_all_empty
86 76 end
87 77
88 78 def test_multiparameter_attributes_on_time
89   - attributes = {
90   - "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
91   - "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
92   - }
93   - topic = Topic.find(1)
94   - topic.attributes = attributes
95   - assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
  79 + with_timezone_config default: :local do
  80 + attributes = {
  81 + "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
  82 + "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
  83 + }
  84 + topic = Topic.find(1)
  85 + topic.attributes = attributes
  86 + assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
  87 + end
96 88 end
97 89
98 90 def test_multiparameter_attributes_on_time_with_no_date
@@ -152,13 +144,15 @@ def test_multiparameter_attributes_on_time_with_raise_on_small_time_if_missing_d
152 144 end
153 145
154 146 def test_multiparameter_attributes_on_time_will_ignore_hour_if_missing
155   - attributes = {
156   - "written_on(1i)" => "2004", "written_on(2i)" => "12", "written_on(3i)" => "12",
157   - "written_on(5i)" => "12", "written_on(6i)" => "02"
158   - }
159   - topic = Topic.find(1)
160   - topic.attributes = attributes
161   - assert_equal Time.local(2004, 12, 12, 0, 12, 2), topic.written_on
  147 + with_timezone_config default: :local do
  148 + attributes = {
  149 + "written_on(1i)" => "2004", "written_on(2i)" => "12", "written_on(3i)" => "12",
  150 + "written_on(5i)" => "12", "written_on(6i)" => "02"
  151 + }
  152 + topic = Topic.find(1)
  153 + topic.attributes = attributes
  154 + assert_equal Time.local(2004, 12, 12, 0, 12, 2), topic.written_on
  155 + end
162 156 end
163 157
164 158 def test_multiparameter_attributes_on_time_will_ignore_hour_if_blank
@@ -180,6 +174,7 @@ def test_multiparameter_attributes_on_time_will_ignore_date_if_empty
180 174 topic.attributes = attributes
181 175 assert_nil topic.written_on
182 176 end
  177 +
183 178 def test_multiparameter_attributes_on_time_with_seconds_will_ignore_date_if_empty
184 179 attributes = {
185 180 "written_on(1i)" => "", "written_on(2i)" => "", "written_on(3i)" => "",
@@ -191,56 +186,56 @@ def test_multiparameter_attributes_on_time_with_seconds_will_ignore_date_if_empt
191 186 end
192 187
193 188 def test_multiparameter_attributes_on_time_with_utc
194   - ActiveRecord::Base.default_timezone = :utc
195   - attributes = {
196   - "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
197   - "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
198   - }
199   - topic = Topic.find(1)
200   - topic.attributes = attributes
201   - assert_equal Time.utc(2004, 6, 24, 16, 24, 0), topic.written_on
  189 + with_timezone_config default: :utc do
  190 + attributes = {
  191 + "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
  192 + "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
  193 + }
  194 + topic = Topic.find(1)
  195 + topic.attributes = attributes
  196 + assert_equal Time.utc(2004, 6, 24, 16, 24, 0), topic.written_on
  197 + end
202 198 end
203 199
204 200 def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes
205   - ActiveRecord::Base.time_zone_aware_attributes = true
206   - ActiveRecord::Base.default_timezone = :utc
207   - Time.zone = ActiveSupport::TimeZone[-28800]
208   - attributes = {
209   - "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
210   - "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
211   - }
212   - topic = Topic.find(1)
213   - topic.attributes = attributes
214   - assert_equal Time.utc(2004, 6, 24, 23, 24, 0), topic.written_on
215   - assert_equal Time.utc(2004, 6, 24, 16, 24, 0), topic.written_on.time
216   - assert_equal Time.zone, topic.written_on.time_zone
  201 + with_timezone_config default: :utc, aware_attributes: true, zone: -28800 do
  202 + attributes = {
  203 + "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
  204 + "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
  205 + }
  206 + topic = Topic.find(1)
  207 + topic.attributes = attributes
  208 + assert_equal Time.utc(2004, 6, 24, 23, 24, 0), topic.written_on
  209 + assert_equal Time.utc(2004, 6, 24, 16, 24, 0), topic.written_on.time
  210 + assert_equal Time.zone, topic.written_on.time_zone
  211 + end
217 212 end
218 213
219 214 def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes_false
220   - Time.zone = ActiveSupport::TimeZone[-28800]
221   - attributes = {
222   - "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
223   - "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
224   - }
225   - topic = Topic.find(1)
226   - topic.attributes = attributes
227   - assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
228   - assert_equal false, topic.written_on.respond_to?(:time_zone)
  215 + with_timezone_config default: :local, aware_attributes: false, zone: -28800 do
  216 + attributes = {
  217 + "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
  218 + "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
  219 + }
  220 + topic = Topic.find(1)
  221 + topic.attributes = attributes
  222 + assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
  223 + assert_equal false, topic.written_on.respond_to?(:time_zone)
  224 + end
229 225 end
230 226
231 227 def test_multiparameter_attributes_on_time_with_skip_time_zone_conversion_for_attributes
232   - ActiveRecord::Base.time_zone_aware_attributes = true
233   - ActiveRecord::Base.default_timezone = :utc
234   - Time.zone = ActiveSupport::TimeZone[-28800]
235   - Topic.skip_time_zone_conversion_for_attributes = [:written_on]
236   - attributes = {
237   - "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
238   - "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
239   - }
240   - topic = Topic.find(1)
241   - topic.attributes = attributes
242   - assert_equal Time.utc(2004, 6, 24, 16, 24, 0), topic.written_on
243   - assert_equal false, topic.written_on.respond_to?(:time_zone)
  228 + with_timezone_config default: :utc, aware_attributes: true, zone: -28800 do
  229 + Topic.skip_time_zone_conversion_for_attributes = [:written_on]
  230 + attributes = {
  231 + "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
  232 + "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
  233 + }
  234 + topic = Topic.find(1)
  235 + topic.attributes = attributes
  236 + assert_equal Time.utc(2004, 6, 24, 16, 24, 0), topic.written_on
  237 + assert_equal false, topic.written_on.respond_to?(:time_zone)
  238 + end
244 239 ensure
245 240 Topic.skip_time_zone_conversion_for_attributes = []
246 241 end
@@ -248,30 +243,31 @@ def test_multiparameter_attributes_on_time_with_skip_time_zone_conversion_for_at
248 243 # Oracle, and Sybase do not have a TIME datatype.
249 244 unless current_adapter?(:OracleAdapter, :SybaseAdapter)
250 245 def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attributes_does_not_do_time_zone_conversion
251   - ActiveRecord::Base.time_zone_aware_attributes = true
252   - ActiveRecord::Base.default_timezone = :utc
253   - Time.zone = ActiveSupport::TimeZone[-28800]
  246 + with_timezone_config default: :utc, aware_attributes: true, zone: -28800 do
  247 + attributes = {
  248 + "bonus_time(1i)" => "2000", "bonus_time(2i)" => "1", "bonus_time(3i)" => "1",
  249 + "bonus_time(4i)" => "16", "bonus_time(5i)" => "24"
  250 + }
  251 + topic = Topic.find(1)
  252 + topic.attributes = attributes
  253 + assert_equal Time.utc(2000, 1, 1, 16, 24, 0), topic.bonus_time
  254 + assert topic.bonus_time.utc?
  255 + end
  256 + end
  257 + end
  258 +
  259 + def test_multiparameter_attributes_on_time_with_empty_seconds
  260 + with_timezone_config default: :local do
254 261 attributes = {
255   - "bonus_time(1i)" => "2000", "bonus_time(2i)" => "1", "bonus_time(3i)" => "1",
256   - "bonus_time(4i)" => "16", "bonus_time(5i)" => "24"
  262 + "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
  263 + "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => ""
257 264 }
258 265 topic = Topic.find(1)
259 266 topic.attributes = attributes
260   - assert_equal Time.utc(2000, 1, 1, 16, 24, 0), topic.bonus_time
261   - assert topic.bonus_time.utc?
  267 + assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
262 268 end
263 269 end
264 270
265   - def test_multiparameter_attributes_on_time_with_empty_seconds
266   - attributes = {
267   - "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24",
268   - "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => ""
269   - }
270   - topic = Topic.find(1)
271   - topic.attributes = attributes
272   - assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
273   - end
274   -
275 271 def test_multiparameter_attributes_setting_time_attribute
276 272 return skip "Oracle does not have TIME data type" if current_adapter? :OracleAdapter
277 273
10 activerecord/test/cases/quoting_test.rb
@@ -53,28 +53,28 @@ def test_quoted_date
53 53 end
54 54
55 55 def test_quoted_time_utc
56   - with_active_record_default_timezone :utc do
  56 + with_timezone_config default: :utc do
57 57 t = Time.now
58 58 assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t)
59 59 end
60 60 end
61 61
62 62 def test_quoted_time_local
63   - with_active_record_default_timezone :local do
  63 + with_timezone_config default: :local do
64 64 t = Time.now
65 65 assert_equal t.getlocal.to_s(:db), @quoter.quoted_date(t)
66 66 end
67 67 end
68 68
69 69 def test_quoted_time_crazy
70   - with_active_record_default_timezone :asdfasdf do
  70 + with_timezone_config default: :asdfasdf do
71 71 t = Time.now
72 72 assert_equal t.getlocal.to_s(:db), @quoter.quoted_date(t)
73 73 end
74 74 end
75 75
76 76 def test_quoted_datetime_utc
77   - with_active_record_default_timezone :utc do
  77 + with_timezone_config default: :utc do
78 78 t = DateTime.now
79 79 assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t)
80 80 end
@@ -83,7 +83,7 @@ def test_quoted_datetime_utc
83 83 ###
84 84 # DateTime doesn't define getlocal, so make sure it does nothing
85 85 def test_quoted_datetime_local
86   - with_active_record_default_timezone :local do
  86 + with_timezone_config default: :local do
87 87 t = DateTime.now
88 88 assert_equal t.to_s(:db), @quoter.quoted_date(t)
89 89 end
15 activerecord/test/cases/serialized_attribute_test.rb
@@ -211,16 +211,15 @@ def dump(thing)
211 211 end
212 212
213 213 def test_serialize_attribute_via_select_method_when_time_zone_available
214   - ActiveRecord::Base.time_zone_aware_attributes = true
215   - Topic.serialize(:content, MyObject)
  214 + with_timezone_config aware_attributes: true do
  215 + Topic.serialize(:content, MyObject)
216 216
217   - myobj = MyObject.new('value1', 'value2')
218   - topic = Topic.create(content: myobj)
  217 + myobj = MyObject.new('value1', 'value2')
  218 + topic = Topic.create(content: myobj)
219 219
220   - assert_equal(myobj, Topic.select(:content).find(topic.id).content)
221   - assert_raise(ActiveModel::MissingAttributeError) { Topic.select(:id).find(topic.id).content }
222   - ensure
223   - ActiveRecord::Base.time_zone_aware_attributes = false
  220 + assert_equal(myobj, Topic.select(:content).find(topic.id).content)
  221 + assert_raise(ActiveModel::MissingAttributeError) { Topic.select(:id).find(topic.id).content }
  222 + end
224 223 end
225 224
226 225 def test_serialize_attribute_can_be_serialized_in_an_integer_column
20 activerecord/test/cases/xml_serialization_test.rb
@@ -161,21 +161,17 @@ def @contact.serializable_hash(options={})
161 161
162 162 class DefaultXmlSerializationTimezoneTest < ActiveRecord::TestCase
163 163 def test_should_serialize_datetime_with_timezone
164   - timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
165   -
166   - toy = Toy.create(:name => 'Mickey', :updated_at => Time.utc(2006, 8, 1))
167   - assert_match %r{<updated-at type=\"dateTime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
168   - ensure
169   - Time.zone = timezone
  164 + with_timezone_config zone: "Pacific Time (US & Canada)" do
  165 + toy = Toy.create(:name => 'Mickey', :updated_at => Time.utc(2006, 8, 1))
  166 + assert_match %r{<updated-at type=\"dateTime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
  167 + end
170 168 end
171 169
172 170 def test_should_serialize_datetime_with_timezone_reloaded
173   - timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
174   -
175   - toy = Toy.create(:name => 'Minnie', :updated_at => Time.utc(2006, 8, 1)).reload
176   - assert_match %r{<updated-at type=\"dateTime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
177   - ensure
178   - Time.zone = timezone
  171 + with_timezone_config zone: "Pacific Time (US & Canada)" do
  172 + toy = Toy.create(:name => 'Minnie', :updated_at => Time.utc(2006, 8, 1)).reload
  173 + assert_match %r{<updated-at type=\"dateTime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
  174 + end
179 175 end
180 176 end
181 177
14 activerecord/test/cases/yaml_serialization_test.rb
@@ -5,16 +5,10 @@ class YamlSerializationTest < ActiveRecord::TestCase
5 5 fixtures :topics
6 6
7 7 def test_to_yaml_with_time_with_zone_should_not_raise_exception
8   - tz = Time.zone
9   - Time.zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
10   - ActiveRecord::Base.time_zone_aware_attributes = true
11   -
12   - topic = Topic.new(:written_on => DateTime.now)
13   - assert_nothing_raised { topic.to_yaml }
14   -
15   - ensure
16   - Time.zone = tz
17   - ActiveRecord::Base.time_zone_aware_attributes = false
  8 + with_timezone_config aware_attributes: true, zone: "Pacific Time (US & Canada)" do
  9 + topic = Topic.new(:written_on => DateTime.now)
  10 + assert_nothing_raised { topic.to_yaml }
  11 + end
18 12 end
19 13
20 14 def test_roundtrip

0 comments on commit abe91cb

Please sign in to comment.
Something went wrong with that request. Please try again.