helper.rb 5.3 KB
Newer Older
1 2
# frozen_string_literal: true

3
require "config"
4

5
require "stringio"
6

7 8 9 10
require "active_record"
require "cases/test_case"
require "active_support/dependencies"
require "active_support/logger"
11

12 13
require "support/config"
require "support/connection"
14 15

# TODO: Move all these random hacks into the ARTest namespace and into the support/ dir
16

17 18
Thread.abort_on_exception = true

19 20 21
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true

22 23 24
# Disable available locale checks to avoid warnings running the test suite.
I18n.enforce_available_locales = false

25 26 27 28
# Connect to the database
ARTest.connect

# Quote "type" if it's a reserved word for the current connection.
29
QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name("type")
30

31 32 33 34 35 36 37
def current_adapter?(*types)
  types.any? do |type|
    ActiveRecord::ConnectionAdapters.const_defined?(type) &&
      ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type))
  end
end

38
def in_memory_db?
A
Andrey Deryabin 已提交
39
  current_adapter?(:SQLite3Adapter) &&
40 41 42
  ActiveRecord::Base.connection_pool.spec.config[:database] == ":memory:"
end

43
def subsecond_precision_supported?
44
  ActiveRecord::Base.connection.supports_datetime_with_precision?
A
Arthur Neves 已提交
45 46
end

47
def mysql_enforcing_gtid_consistency?
48
  current_adapter?(:Mysql2Adapter) && "ON" == ActiveRecord::Base.connection.show_variable("enforce_gtid_consistency")
49 50
end

51 52 53 54 55 56 57 58 59
def supports_default_expression?
  if current_adapter?(:PostgreSQLAdapter)
    true
  elsif current_adapter?(:Mysql2Adapter)
    conn = ActiveRecord::Base.connection
    !conn.mariadb? && conn.version >= "8.0.13"
  end
end

60 61 62 63 64 65 66
%w[
  supports_savepoints?
  supports_partial_index?
  supports_insert_returning?
  supports_insert_on_duplicate_skip?
  supports_insert_on_duplicate_update?
  supports_insert_conflict_target?
R
Ryuta Kamizono 已提交
67
  supports_optimizer_hints?
68 69 70 71
].each do |method_name|
  define_method method_name do
    ActiveRecord::Base.connection.public_send(method_name)
  end
72 73
end

74 75
def with_env_tz(new_tz = "US/Eastern")
  old_tz, ENV["TZ"] = ENV["TZ"], new_tz
76 77
  yield
ensure
78
  old_tz ? ENV["TZ"] = old_tz : ENV.delete("TZ")
79 80
end

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
def with_timezone_config(cfg)
  verify_default_timezone_config

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

  if cfg.has_key?(:default)
    ActiveRecord::Base.default_timezone = cfg[:default]
  end
  if cfg.has_key?(:aware_attributes)
    ActiveRecord::Base.time_zone_aware_attributes = cfg[:aware_attributes]
  end
  if cfg.has_key?(:zone)
    Time.zone = cfg[:zone]
  end
97 98
  yield
ensure
99 100 101 102 103 104 105 106 107 108 109
  ActiveRecord::Base.default_timezone = old_default_zone
  ActiveRecord::Base.time_zone_aware_attributes = old_awareness
  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_TIME_ZONE_AWARE_ATTRIBUTES = false
def verify_default_timezone_config
  if Time.zone != EXPECTED_ZONE
110
    $stderr.puts <<-MSG
111
\n#{self}
112 113 114 115 116 117
    Global state `Time.zone` was leaked.
      Expected: #{EXPECTED_ZONE}
      Got: #{Time.zone}
    MSG
  end
  if ActiveRecord::Base.default_timezone != EXPECTED_DEFAULT_TIMEZONE
118
    $stderr.puts <<-MSG
119
\n#{self}
120 121 122 123 124 125
    Global state `ActiveRecord::Base.default_timezone` was leaked.
      Expected: #{EXPECTED_DEFAULT_TIMEZONE}
      Got: #{ActiveRecord::Base.default_timezone}
    MSG
  end
  if ActiveRecord::Base.time_zone_aware_attributes != EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES
126
    $stderr.puts <<-MSG
127
\n#{self}
128 129 130 131 132
    Global state `ActiveRecord::Base.time_zone_aware_attributes` was leaked.
      Expected: #{EXPECTED_TIME_ZONE_AWARE_ATTRIBUTES}
      Got: #{ActiveRecord::Base.time_zone_aware_attributes}
    MSG
  end
133 134
end

135
def enable_extension!(extension, connection)
136
  return false unless connection.supports_extensions?
137
  return connection.reconnect! if connection.extension_enabled?(extension)
138

139
  connection.enable_extension extension
140
  connection.commit_db_transaction if connection.transaction_open?
141 142 143
  connection.reconnect!
end

144 145 146 147 148 149 150 151
def disable_extension!(extension, connection)
  return false unless connection.supports_extensions?
  return true unless connection.extension_enabled?(extension)

  connection.disable_extension extension
  connection.reconnect!
end

152 153 154 155
def load_schema
  # silence verbose schema loading
  original_stdout = $stdout
  $stdout = StringIO.new
156 157 158 159 160 161

  adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
  adapter_specific_schema_file = SCHEMA_ROOT + "/#{adapter_name}_specific_schema.rb"

  load SCHEMA_ROOT + "/schema.rb"

A
Arun Agrawal 已提交
162
  if File.exist?(adapter_specific_schema_file)
163 164
    load adapter_specific_schema_file
  end
165 166

  ActiveRecord::FixtureSet.reset_cache
167 168 169
ensure
  $stdout = original_stdout
end
170

171 172
load_schema

173 174
class SQLSubscriber
  attr_reader :logged
175
  attr_reader :payloads
176 177 178

  def initialize
    @logged = []
179
    @payloads = []
180 181 182
  end

  def start(name, id, payload)
183
    @payloads << payload
G
Guo Xiang Tan 已提交
184
    @logged << [payload[:sql].squish, payload[:name], payload[:binds]]
185 186 187 188 189
  end

  def finish(name, id, payload); end
end

190 191 192
module InTimeZone
  private

193 194 195 196 197 198 199 200 201 202 203
    def in_time_zone(zone)
      old_zone  = Time.zone
      old_tz    = ActiveRecord::Base.time_zone_aware_attributes

      Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil
      ActiveRecord::Base.time_zone_aware_attributes = !zone.nil?
      yield
    ensure
      Time.zone = old_zone
      ActiveRecord::Base.time_zone_aware_attributes = old_tz
    end
204
end