aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/integration_test.rb
blob: 5687afbc71005b2afc63d64811870a44dbcbeeed (plain) (blame)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# frozen_string_literal: true

require "cases/helper"
require "models/company"
require "models/developer"
require "models/computer"
require "models/owner"
require "models/pet"

class IntegrationTest < ActiveRecord::TestCase
  fixtures :companies, :developers, :owners, :pets

  def test_to_param_should_return_string
    assert_kind_of String, Client.first.to_param
  end

  def test_to_param_returns_nil_if_not_persisted
    client = Client.new
    assert_nil client.to_param
  end

  def test_to_param_returns_id_if_not_persisted_but_id_is_set
    client = Client.new
    client.id = 1
    assert_equal "1", client.to_param
  end

  def test_to_param_class_method
    firm = Firm.find(4)
    assert_equal "4-flamboyant-software", firm.to_param
  end

  def test_to_param_class_method_truncates_words_properly
    firm = Firm.find(4)
    firm.name << ", Inc."
    assert_equal "4-flamboyant-software", firm.to_param
  end

  def test_to_param_class_method_truncates_after_parameterize
    firm = Firm.find(4)
    firm.name = "Huey, Dewey, & Louie LLC"
    #               123456789T123456789v
    assert_equal "4-huey-dewey-louie-llc", firm.to_param
  end

  def test_to_param_class_method_truncates_after_parameterize_with_hyphens
    firm = Firm.find(4)
    firm.name = "Door-to-Door Wash-n-Fold Service"
    #               123456789T123456789v
    assert_equal "4-door-to-door-wash-n", firm.to_param
  end

  def test_to_param_class_method_truncates
    firm = Firm.find(4)
    firm.name = "a " * 100
    assert_equal "4-a-a-a-a-a-a-a-a-a-a", firm.to_param
  end

  def test_to_param_class_method_truncates_edge_case
    firm = Firm.find(4)
    firm.name = "David HeinemeierHansson"
    assert_equal "4-david", firm.to_param
  end

  def test_to_param_class_method_truncates_case_shown_in_doc
    firm = Firm.find(4)
    firm.name = "David Heinemeier Hansson"
    assert_equal "4-david-heinemeier", firm.to_param
  end

  def test_to_param_class_method_squishes
    firm = Firm.find(4)
    firm.name = "ab \n" * 100
    assert_equal "4-ab-ab-ab-ab-ab-ab-ab", firm.to_param
  end

  def test_to_param_class_method_multibyte_character
    firm = Firm.find(4)
    firm.name = "戦場ヶ原 ひたぎ"
    assert_equal "4", firm.to_param
  end

  def test_to_param_class_method_uses_default_if_blank
    firm = Firm.find(4)
    firm.name = nil
    assert_equal "4", firm.to_param
    firm.name = " "
    assert_equal "4", firm.to_param
  end

  def test_to_param_class_method_uses_default_if_not_persisted
    firm = Firm.new(name: "Fancy Shirts")
    assert_nil firm.to_param
  end

  def test_to_param_with_no_arguments
    assert_equal "Firm", Firm.to_param
  end

  def test_cache_key_for_existing_record_is_not_timezone_dependent
    utc_key = Developer.first.cache_key

    with_timezone_config zone: "EST" do
      est_key = Developer.first.cache_key
      assert_equal utc_key, est_key
    end
  end

  def test_cache_key_format_for_existing_record_with_updated_at
    dev = Developer.first
    assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:usec)}", dev.cache_key
  end

  def test_cache_key_format_for_existing_record_with_updated_at_and_custom_cache_timestamp_format
    dev = CachedDeveloper.first
    assert_equal "cached_developers/#{dev.id}-#{dev.updated_at.utc.to_s(:number)}", dev.cache_key
  end

  def test_cache_key_changes_when_child_touched
    owner = owners(:blackbeard)
    pet   = pets(:parrot)

    owner.update_column :updated_at, Time.current
    key = owner.cache_key

    travel(1.second) do
      assert pet.touch
    end
    assert_not_equal key, owner.reload.cache_key
  end

  def test_cache_key_format_for_existing_record_with_nil_updated_timestamps
    dev = Developer.first
    dev.update_columns(updated_at: nil, updated_on: nil)
    assert_match(/\/#{dev.id}$/, dev.cache_key)
  end

  def test_cache_key_for_updated_on
    dev = Developer.first
    dev.updated_at = nil
    assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:usec)}", dev.cache_key
  end

  def test_cache_key_for_newer_updated_at
    dev = Developer.first
    dev.updated_at += 3600
    assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:usec)}", dev.cache_key
  end

  def test_cache_key_for_newer_updated_on
    dev = Developer.first
    dev.updated_on += 3600
    assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:usec)}", dev.cache_key
  end

  def test_cache_key_format_is_precise_enough
    skip("Subsecond precision is not supported") unless subsecond_precision_supported?
    dev = Developer.first
    key = dev.cache_key
    travel_to dev.updated_at + 0.000001 do
      dev.touch
    end
    assert_not_equal key, dev.cache_key
  end

  def test_cache_key_format_is_not_too_precise
    dev = Developer.first
    dev.touch
    key = dev.cache_key
    assert_equal key, dev.reload.cache_key
  end

  def test_cache_version_format_is_precise_enough
    skip("Subsecond precision is not supported") unless subsecond_precision_supported?
    with_cache_versioning do
      dev = Developer.first
      version = dev.cache_version.to_param
      travel_to Developer.first.updated_at + 0.000001 do
        dev.touch
      end
      assert_not_equal version, dev.cache_version.to_param
    end
  end

  def test_cache_version_format_is_not_too_precise
    with_cache_versioning do
      dev = Developer.first
      dev.touch
      key = dev.cache_version.to_param
      assert_equal key, dev.reload.cache_version.to_param
    end
  end

  def test_named_timestamps_for_cache_key
    assert_deprecated do
      owner = owners(:blackbeard)
      assert_equal "owners/#{owner.id}-#{owner.happy_at.utc.to_s(:usec)}", owner.cache_key(:updated_at, :happy_at)
    end
  end

  def test_cache_key_when_named_timestamp_is_nil
    assert_deprecated do
      owner = owners(:blackbeard)
      owner.happy_at = nil
      assert_equal "owners/#{owner.id}", owner.cache_key(:happy_at)
    end
  end

  def test_cache_key_is_stable_with_versioning_on
    with_cache_versioning do
      developer = Developer.first
      first_key = developer.cache_key

      developer.touch
      second_key = developer.cache_key

      assert_equal first_key, second_key
    end
  end

  def test_cache_version_changes_with_versioning_on
    with_cache_versioning do
      developer     = Developer.first
      first_version = developer.cache_version

      travel 10.seconds do
        developer.touch
      end

      second_version = developer.cache_version

      assert_not_equal first_version, second_version
    end
  end

  def test_cache_key_retains_version_when_custom_timestamp_is_used
    with_cache_versioning do
      developer = Developer.first
      first_key = developer.cache_key_with_version

      travel 10.seconds do
        developer.touch
      end

      second_key = developer.cache_key_with_version

      assert_not_equal first_key, second_key
    end
  end

  def with_cache_versioning(value = true)
    @old_cache_versioning = ActiveRecord::Base.cache_versioning
    ActiveRecord::Base.cache_versioning = value
    yield
  ensure
    ActiveRecord::Base.cache_versioning = @old_cache_versioning
  end
end