File: helper.rb

package info (click to toggle)
ruby-activemodel-serializers-xml 1.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 284 kB
  • sloc: ruby: 1,772; makefile: 4
file content (187 lines) | stat: -rw-r--r-- 4,840 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'active_model/serializers'

require 'active_support/testing/autorun'

TEST_ROOT       = File.expand_path(File.dirname(__FILE__))
FIXTURES_ROOT   = TEST_ROOT + "/fixtures"
require 'active_support/core_ext/string/access'
require 'stringio'

require 'active_record'
require 'active_support/dependencies'
require 'active_support/logger'
require 'active_support/core_ext/string/strip'

Thread.abort_on_exception = true

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

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

# Connect to the database
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

class ActiveRecord::TestCase < ActiveSupport::TestCase
  include ActiveRecord::TestFixtures

  self.fixture_path = FIXTURES_ROOT
  self.use_instantiated_fixtures  = false

  def create_fixtures(*fixture_set_names, &block)
    ActiveRecord::FixtureSet.create_fixtures(ActiveSupport::TestCase.fixture_path, fixture_set_names, fixture_class_names, &block)
  end

  def with_env_tz(new_tz = 'US/Eastern')
    old_tz, ENV['TZ'] = ENV['TZ'], new_tz
    yield
  ensure
    old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  end

  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
    yield
  ensure
    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
      $stderr.puts <<-MSG
  \n#{self}
      Global state `Time.zone` was leaked.
        Expected: #{EXPECTED_ZONE}
        Got: #{Time.zone}
      MSG
    end
    if ActiveRecord::Base.default_timezone != EXPECTED_DEFAULT_TIMEZONE
      $stderr.puts <<-MSG
  \n#{self}
      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
      $stderr.puts <<-MSG
  \n#{self}
      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
  end
end

ActiveRecord::Schema.verbose = false
ActiveRecord::Schema.define do
  create_table :addresses do |t|
    t.string :city
  end

  create_table :ar_contacts do |t|
    t.string :name
    t.integer :age
    t.binary :avatar
    t.boolean :awesome
    t.string :preferences
    t.integer :alternative_id
    t.string :address
    t.timestamps
  end

  create_table :contact_stis do |t|
    t.string :type
    t.string :name
    t.integer :age
    t.binary :avatar
    t.boolean :awesome
    t.string :preferences
    t.integer :alternative_id
    t.string :address
    t.timestamps
  end

  create_table :topics do |t|
    t.string :title
    t.string :author_name
    t.string :author_email_address
    t.datetime :written_on
    t.date :last_read
    t.datetime :bonus_time
    t.text :content
    t.boolean :approved
    t.integer :replies_count
    t.integer :parent_id
    t.string :type
  end

  create_table :companies do |t|
    t.integer :firm_id
    t.integer :client_of
    t.string :name
    t.string :firm_name
    t.string :type
  end

  create_table :accounts do |t|
    t.integer :firm_id
    t.integer :credit_limit
    t.string :firm_name
  end

  create_table :authors do |t|
    t.string :name
    t.integer :author_address_id
    t.integer :author_address_extra_id
    t.string :organization_id
    t.string :owned_essay_id
  end

  create_table :toys do |t|
    t.string :name
    t.timestamps
  end

  create_table :posts do |t|
    t.string :title
    t.string :category
    t.integer :author_id
    t.text :body
    t.integer :comments_count
    t.integer :tags_count
    t.string :type
  end

  create_table :comments do |t|
    t.string :title
    t.references :post
  end

  create_table :projects do |t|
    t.string :name
  end
end