Skip to content

Commit

Permalink
chore(test): update helper
Browse files Browse the repository at this point in the history
  • Loading branch information
BuonOmo authored and rafiss committed Sep 19, 2023
1 parent 1b17ecd commit 4bf5499
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require "cases/test_case"
require "active_support/dependencies"
require "active_support/logger"
require "active_support/core_ext/kernel/reporting"
require "active_support/core_ext/kernel/singleton_class"

require "support/config"
Expand Down Expand Up @@ -105,6 +106,7 @@ def with_timezone_config(cfg)

old_default_zone = ActiveRecord.default_timezone
old_awareness = ActiveRecord::Base.time_zone_aware_attributes
old_aware_types = ActiveRecord::Base.time_zone_aware_types
old_zone = Time.zone

if cfg.has_key?(:default)
Expand All @@ -113,19 +115,24 @@ def with_timezone_config(cfg)
if cfg.has_key?(:aware_attributes)
ActiveRecord::Base.time_zone_aware_attributes = cfg[:aware_attributes]
end
if cfg.has_key?(:aware_types)
ActiveRecord::Base.time_zone_aware_types = cfg[:aware_types]
end
if cfg.has_key?(:zone)
Time.zone = cfg[:zone]
end
yield
ensure
ActiveRecord.default_timezone = old_default_zone
ActiveRecord::Base.time_zone_aware_attributes = old_awareness
ActiveRecord::Base.time_zone_aware_types = old_aware_types
Time.zone = old_zone
end

# This method makes sure that tests don't leak global state related to time zones.
EXPECTED_ZONE = nil
EXPECTED_DEFAULT_TIMEZONE = :utc
EXPECTED_AWARE_TYPES = [:datetime, :time]
EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES = false
def verify_default_timezone_config
if Time.zone != EXPECTED_ZONE
Expand All @@ -152,6 +159,14 @@ def verify_default_timezone_config
Got: #{ActiveRecord::Base.time_zone_aware_attributes}
MSG
end
if ActiveRecord::Base.time_zone_aware_types != EXPECTED_AWARE_TYPES
$stderr.puts <<-MSG
\n#{self}
Global state `ActiveRecord::Base.time_zone_aware_types` was leaked.
Expected: #{EXPECTED_AWARE_TYPES}
Got: #{ActiveRecord::Base.time_zone_aware_types}
MSG
end
end

def enable_extension!(extension, connection)
Expand Down

0 comments on commit 4bf5499

Please sign in to comment.