/
active_record_spec.rb
413 lines (344 loc) · 14.1 KB
/
active_record_spec.rb
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# frozen_string_literal: true
require "spec_helper"
require "timecop"
require "statesman/adapters/shared_examples"
require "statesman/exceptions"
describe Statesman::Adapters::ActiveRecord, active_record: true do
before do
prepare_model_table
prepare_transitions_table
MyActiveRecordModelTransition.serialize(:metadata, JSON)
Statesman.configure do
# Rubocop requires described_class to be used, but this block
# is instance_eval'd and described_class won't be defined
# rubocop:disable RSpec/DescribedClass
storage_adapter(Statesman::Adapters::ActiveRecord)
# rubocop:enable RSpec/DescribedClass
end
end
after { Statesman.configure { storage_adapter(Statesman::Adapters::Memory) } }
let(:observer) { double(Statesman::Machine, execute: nil) }
let(:model) { MyActiveRecordModel.create(current_state: :pending) }
it_behaves_like "an adapter", described_class, MyActiveRecordModelTransition
describe "#initialize" do
context "with unserialized metadata and non json column type" do
before do
metadata_column = double
allow(metadata_column).to receive_messages(sql_type: "")
allow(MyActiveRecordModelTransition).to receive_messages(columns_hash:
{ "metadata" => metadata_column })
if ActiveRecord.respond_to?(:gem_version) &&
ActiveRecord.gem_version >= Gem::Version.new("4.2.0.a")
expect(MyActiveRecordModelTransition).
to receive(:type_for_attribute).with("metadata").
and_return(ActiveRecord::Type::Value.new)
else
expect(MyActiveRecordModelTransition).
to receive_messages(serialized_attributes: {})
end
end
it "raises an exception" do
expect do
described_class.new(MyActiveRecordModelTransition,
MyActiveRecordModel, observer)
end.to raise_exception(Statesman::UnserializedMetadataError)
end
end
context "with serialized metadata and json column type" do
before do
metadata_column = double
allow(metadata_column).to receive_messages(sql_type: "json")
allow(MyActiveRecordModelTransition).to receive_messages(columns_hash:
{ "metadata" => metadata_column })
if ActiveRecord.respond_to?(:gem_version) &&
ActiveRecord.gem_version >= Gem::Version.new("4.2.0.a")
serialized_type = ActiveRecord::Type::Serialized.new(
"", ActiveRecord::Coders::JSON
)
expect(MyActiveRecordModelTransition).
to receive(:type_for_attribute).with("metadata").
and_return(serialized_type)
else
expect(MyActiveRecordModelTransition).
to receive_messages(serialized_attributes: { "metadata" => "" })
end
end
it "raises an exception" do
expect do
described_class.new(MyActiveRecordModelTransition,
MyActiveRecordModel, observer)
end.to raise_exception(Statesman::IncompatibleSerializationError)
end
end
context "with serialized metadata and jsonb column type" do
before do
metadata_column = double
allow(metadata_column).to receive_messages(sql_type: "jsonb")
allow(MyActiveRecordModelTransition).to receive_messages(columns_hash:
{ "metadata" => metadata_column })
if ActiveRecord.respond_to?(:gem_version) &&
ActiveRecord.gem_version >= Gem::Version.new("4.2.0.a")
serialized_type = ActiveRecord::Type::Serialized.new(
"", ActiveRecord::Coders::JSON
)
expect(MyActiveRecordModelTransition).
to receive(:type_for_attribute).with("metadata").
and_return(serialized_type)
else
expect(MyActiveRecordModelTransition).
to receive_messages(serialized_attributes: { "metadata" => "" })
end
end
it "raises an exception" do
expect do
described_class.new(MyActiveRecordModelTransition,
MyActiveRecordModel, observer)
end.to raise_exception(Statesman::IncompatibleSerializationError)
end
end
end
describe "#create" do
subject(:transition) { create }
let!(:adapter) do
described_class.new(MyActiveRecordModelTransition, model, observer)
end
let(:from) { :x }
let(:to) { :y }
let(:create) { adapter.create(from, to) }
context "when there is a race" do
it "raises a TransitionConflictError" do
adapter2 = adapter.dup
adapter2.create(:x, :y)
adapter.last
adapter2.create(:y, :z)
expect { adapter.create(:y, :z) }.
to raise_exception(Statesman::TransitionConflictError)
end
it "does not pollute the state when the transition fails" do
# this increments the sort_key in the database
adapter.create(:x, :y)
# we then pre-load the transitions for efficiency
preloaded_model = MyActiveRecordModel.
includes(:my_active_record_model_transitions).
find(model.id)
adapter2 = described_class.
new(MyActiveRecordModelTransition, preloaded_model, observer)
# Now we generate a race
adapter.create(:y, :z)
expect { adapter2.create(:y, :a) }.
to raise_error(Statesman::TransitionConflictError)
# The preloaded adapter should discard the preloaded info
expect(adapter2.last).to have_attributes(to_state: "z")
expect(adapter2.history).to contain_exactly(
have_attributes(to_state: "y"),
have_attributes(to_state: "z"),
)
end
end
context "when other exceptions occur" do
before do
allow_any_instance_of(MyActiveRecordModelTransition).
to receive(:save!).and_raise(error)
end
context "ActiveRecord::RecordNotUnique unrelated to this transition" do
let(:error) do
if ActiveRecord.respond_to?(:gem_version) &&
ActiveRecord.gem_version >= Gem::Version.new("4.0.0")
ActiveRecord::RecordNotUnique.new("unrelated")
else
ActiveRecord::RecordNotUnique.new("unrelated", nil)
end
end
it { expect { transition }.to raise_exception(ActiveRecord::RecordNotUnique) }
end
context "other errors" do
let(:error) { StandardError }
it { expect { transition }.to raise_exception(StandardError) }
end
end
describe "updating the most_recent column" do
context "with no previous transition" do
its(:most_recent) { is_expected.to eq(true) }
end
context "with a previous transition" do
let!(:previous_transition) { adapter.create(from, to) }
its(:most_recent) { is_expected.to eq(true) }
it "updates the previous transition's most_recent flag" do
expect { create }.
to change { previous_transition.reload.most_recent }.
from(true).to be_falsey
end
it "touches the previous transition's updated_at timestamp" do
expect { Timecop.freeze(Time.now + 5.seconds) { create } }.
to(change { previous_transition.reload.updated_at })
end
context "for a transition class without an updated timestamp column attribute" do
let!(:adapter) do
described_class.new(MyActiveRecordModelTransitionWithoutInclude,
model,
observer)
end
it "defaults to touching the previous transition's updated_at timestamp" do
expect { Timecop.freeze(Time.now + 5.seconds) { create } }.
to(change { previous_transition.reload.updated_at })
end
end
context "with a custom updated timestamp column set" do
around do |example|
MyActiveRecordModelTransition.updated_timestamp_column.tap do |original_value|
MyActiveRecordModelTransition.updated_timestamp_column = :updated_on
example.run
MyActiveRecordModelTransition.updated_timestamp_column = original_value
end
end
it "touches the previous transition's updated_on timestamp" do
expect { Timecop.freeze(Time.now + 1.day) { create } }.
to(change { previous_transition.reload.updated_on })
end
it "doesn't update the updated_at column" do
expect { Timecop.freeze(Time.now + 5.seconds) { create } }.
to_not(change { previous_transition.reload.updated_at })
end
end
context "with no updated timestamp column set" do
around do |example|
MyActiveRecordModelTransition.updated_timestamp_column.tap do |original_value|
MyActiveRecordModelTransition.updated_timestamp_column = nil
example.run
MyActiveRecordModelTransition.updated_timestamp_column = original_value
end
end
it "just updates the most_recent" do
expect { Timecop.freeze(Time.now + 5.seconds) { create } }.
to(change { previous_transition.reload.most_recent })
end
it "doesn't update the updated_at column" do
expect { Timecop.freeze(Time.now + 5.seconds) { create } }.
to_not(change { previous_transition.reload.updated_at })
end
end
context "and a query on the parent model's state is made" do
context "in a before action" do
it "still has the old state" do
allow(observer).to receive(:execute) do |phase|
next unless phase == :before
expect(
model.transitions.where(most_recent: true).first.to_state,
).to eq("y")
end
adapter.create(:y, :z)
end
end
context "in an after action" do
it "still has the old state" do
allow(observer).to receive(:execute) do |phase|
next unless phase == :after
expect(
model.transitions.where(most_recent: true).first.to_state,
).to eq("z")
end
adapter.create(:y, :z)
end
end
end
end
context "with two previous transitions" do
let!(:previous_transition) { adapter.create(from, to) }
let!(:another_previous_transition) { adapter.create(from, to) }
its(:most_recent) { is_expected.to eq(true) }
it "updates the previous transition's most_recent flag" do
expect { create }.
to change { another_previous_transition.reload.most_recent }.
from(true).to be_falsey
end
end
end
end
describe "#last" do
let(:adapter) do
described_class.new(MyActiveRecordModelTransition, model, observer)
end
before { adapter.create(:x, :y) }
context "with a previously looked up transition" do
before { adapter.last }
it "caches the transition" do
expect_any_instance_of(MyActiveRecordModel).
to_not receive(:my_active_record_model_transitions)
adapter.last
end
context "after then creating a new transition" do
before { adapter.create(:y, :z, []) }
it "retrieves the new transition from the database" do
expect(adapter.last.to_state).to eq("z")
end
end
context "when a new transition has been created elsewhere" do
let(:alternate_adapter) do
described_class.new(MyActiveRecordModelTransition, model, observer)
end
it "still returns the cached value" do
alternate_adapter.create(:y, :z, [])
expect_any_instance_of(MyActiveRecordModel).
to_not receive(:my_active_record_model_transitions)
expect(adapter.last.to_state).to eq("y")
end
context "when explicitly not using the cache" do
context "when the transitions are in memory" do
before do
model.my_active_record_model_transitions.load
alternate_adapter.create(:y, :z, [])
end
it "reloads the value" do
expect(adapter.last(force_reload: true).to_state).to eq("z")
end
end
context "when the transitions are not in memory" do
before do
model.my_active_record_model_transitions.reset
alternate_adapter.create(:y, :z, [])
end
it "reloads the value" do
expect(adapter.last(force_reload: true).to_state).to eq("z")
end
end
end
end
end
context "with a pre-fetched transition history" do
before { adapter.create(:x, :y) }
before { model.my_active_record_model_transitions.load_target }
it "doesn't query the database" do
expect(MyActiveRecordModelTransition).to_not receive(:connection)
expect(adapter.last.to_state).to eq("y")
end
end
end
it "resets last with #reload" do
model.save!
ActiveRecord::Base.transaction do
model.state_machine.transition_to!(:succeeded)
# force to cache value in last_transition instance variable
expect(model.state_machine.current_state).to eq("succeeded")
raise ActiveRecord::Rollback
end
expect(model.state_machine.current_state).to eq("succeeded")
model.reload
expect(model.state_machine.current_state).to eq("initial")
end
context "with a namespaced model" do
before do
CreateNamespacedARModelMigration.migrate(:up)
CreateNamespacedARModelTransitionMigration.migrate(:up)
end
before do
MyNamespace::MyActiveRecordModelTransition.serialize(:metadata, JSON)
end
let(:observer) { double(Statesman::Machine, execute: nil) }
let(:model) do
MyNamespace::MyActiveRecordModel.create(current_state: :pending)
end
it_behaves_like "an adapter",
described_class, MyNamespace::MyActiveRecordModelTransition,
association_name: :my_active_record_model_transitions
end
end