/
mongoid_test.rb
496 lines (420 loc) · 12.7 KB
/
mongoid_test.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
test_dir = File.dirname(__FILE__)
$:.unshift test_dir unless $:.include?(test_dir)
require 'test_helper'
require 'mongoid'
require 'workflow'
require 'workflow_on_mongoid'
# redefine this so it works with mongoid!
def assert_state(title, expected_state, klass = Order)
puts 'mongoid assert_state'
o = klass.where(:title => title).first
assert_equal expected_state, o.send(klass.workflow_column)
o
end
class MongoidOrder
include Mongoid::Document
field :title
field :workflow_state
include Workflow
workflow do
state :submitted do
event :accept, :transitions_to => :accepted, :meta => {:doc_weight => 8} do |reviewer, args|
end
end
state :accepted do
event :ship, :transitions_to => :shipped
end
state :shipped
end
end
class MongoidLegacyOrder
include Mongoid::Document
field :title
field :foo_bar
include Workflow
workflow_column :foo_bar # use this legacy database column for persistence
workflow do
state :submitted do
event :accept, :transitions_to => :accepted, :meta => {:doc_weight => 8} do |reviewer, args|
end
end
state :accepted do
event :ship, :transitions_to => :shipped
end
state :shipped
end
end
class MongoidImage
include Mongoid::Document
field :title
field :status
include Workflow
workflow_column :status
workflow do
state :unconverted do
event :convert, :transitions_to => :converted
end
state :converted
end
end
class MongoidSmallImage < MongoidImage
end
class MongoidSpecialSmallImage < MongoidSmallImage
end
class MongoidTest < MongoidTestCase
def setup
super
MongoidOrder.create!(:title => 'some MongoidOrder', :workflow_state => 'accepted')
MongoidLegacyOrder.create!(:title => 'some MongoidOrder', :foo_bar => 'accepted')
end
def assert_state(title, expected_state, klass = MongoidOrder)
o = klass.where(:title => title).first
assert_equal expected_state, o.send(klass.workflow_column)
o
end
test 'immediately save the new workflow_state on state machine transition' do
o = assert_state 'some MongoidOrder', 'accepted'
assert o.ship!
assert_state 'some MongoidOrder', 'shipped'
end
test 'immediately save the new workflow_state on state machine transition with custom column name' do
o = assert_state 'some MongoidOrder', 'accepted', MongoidLegacyOrder
assert o.ship!
assert_state 'some MongoidOrder', 'shipped', MongoidLegacyOrder
end
# There was a bug where calling valid? on the record would cause it to be saved
# see https://github.com/bowsersenior/workflow_on_mongoid/pull/3
test 'does not save the record when setting initial state' do
new_order = MongoidOrder.new
assert new_order.new_record?
new_order.valid?
assert new_order.new_record?
end
test 'persist workflow_state in the db and reload' do
o = assert_state 'some MongoidOrder', 'accepted'
assert_equal :accepted, o.current_state.name
o.ship!
o.save!
assert_state 'some MongoidOrder', 'shipped'
o.reload
assert_equal 'shipped', o.send(:workflow_state)
end
test 'persist workflow_state in the db with_custom_name and reload' do
o = assert_state 'some MongoidOrder', 'accepted', MongoidLegacyOrder
assert_equal :accepted, o.current_state.name
o.ship!
o.save!
assert_state 'some MongoidOrder', 'shipped', MongoidLegacyOrder
o.reload
assert_equal 'shipped', o.send(:foo_bar)
end
test 'default workflow column should be workflow_state' do
o = assert_state 'some MongoidOrder', 'accepted'
assert_equal :workflow_state, o.class.workflow_column
assert MongoidOrder.fields.keys.include?('workflow_state')
end
test 'custom workflow column should be foo_bar' do
o = assert_state 'some MongoidOrder', 'accepted', MongoidLegacyOrder
assert_equal :foo_bar, o.class.workflow_column
end
test 'should not have workflow_state column if custom workflow_column is specified' do
assert !( MongoidLegacyOrder.fields.keys.include?('workflow_state') )
end
test 'access workflow specification' do
assert_equal 3, MongoidOrder.workflow_spec.states.length
assert_equal ['submitted', 'accepted', 'shipped'].sort,
MongoidOrder.workflow_spec.state_names.map{|n| n.to_s}.sort
end
test 'current state object' do
o = assert_state 'some MongoidOrder', 'accepted'
assert_equal 'accepted', o.current_state.to_s
assert_equal 1, o.current_state.events.length
end
test 'on_entry and on_exit invoked' do
c = Class.new
callbacks = mock()
callbacks.expects(:my_on_exit_new).once
callbacks.expects(:my_on_entry_old).once
c.class_eval do
include Workflow
workflow do
state :new do
event :age, :transitions_to => :old
end
on_exit do
callbacks.my_on_exit_new
end
state :old
on_entry do
callbacks.my_on_entry_old
end
on_exit do
fail "wrong on_exit executed"
end
end
end
o = c.new
assert_equal 'new', o.current_state.to_s
o.age!
end
test 'on_transition invoked' do
callbacks = mock()
callbacks.expects(:on_tran).once # this is validated at the end
c = Class.new
c.class_eval do
include Workflow
workflow do
state :one do
event :increment, :transitions_to => :two
end
state :two
on_transition do |from, to, triggering_event, *event_args|
callbacks.on_tran
end
end
end
assert_not_nil c.workflow_spec.on_transition_proc
c.new.increment!
end
test 'access event meta information' do
c = Class.new
c.class_eval do
include Workflow
workflow do
state :main, :meta => {:importance => 8}
state :supplemental, :meta => {:importance => 1}
end
end
assert_equal 1, c.workflow_spec.states[:supplemental].meta[:importance]
end
test 'initial state' do
c = Class.new
c.class_eval do
include Workflow
workflow { state :one; state :two }
end
assert_equal 'one', c.new.current_state.to_s
end
test 'nil as initial state' do
MongoidOrder.create!(:title => 'nil state', :workflow_state => nil)
o = MongoidOrder.where(:title =>'nil state').first
assert o.submitted?, 'if workflow_state is nil, the initial state should be assumed'
assert !o.shipped?
end
test 'initial state immediately set for new objects' do
o = MongoidOrder.create(:title => 'new object')
assert_equal 'submitted', o.send(:workflow_state)
end
test 'question methods for state' do
o = assert_state 'some MongoidOrder', 'accepted'
assert o.accepted?
assert !o.shipped?
end
test 'correct exception for event that is not allowed in current state' do
o = assert_state 'some MongoidOrder', 'accepted'
assert_raise Workflow::NoTransitionAllowed do
o.accept!
end
end
test 'multiple events with the same name and different arguments lists from different states'
test 'implicit transition callback' do
args = mock()
args.expects(:my_tran).once # this is validated at the end
c = Class.new
c.class_eval do
include Workflow
def my_transition(args)
args.my_tran
end
workflow do
state :one do
event :my_transition, :transitions_to => :two
end
state :two
end
end
c.new.my_transition!(args)
end
test 'Single table inheritance (STI)' do
class BigMongoidOrder < MongoidOrder
end
bo = BigMongoidOrder.new
assert bo.submitted?
assert !bo.accepted?
end
test 'STI when parent changed the workflow_state column' do
assert_equal 'status', MongoidImage.workflow_column.to_s
assert_equal 'status', MongoidSmallImage.workflow_column.to_s
assert_equal 'status', MongoidSpecialSmallImage.workflow_column.to_s
end
test 'Two-level inheritance' do
class BigMongoidOrder < MongoidOrder
end
class EvenBiggerMongoidOrder < BigMongoidOrder
end
assert EvenBiggerMongoidOrder.new.submitted?
end
test 'Iheritance with workflow definition override' do
class BigMongoidOrder < MongoidOrder
end
class SpecialBigMongoidOrder < BigMongoidOrder
workflow do
state :start_big
end
end
special = SpecialBigMongoidOrder.new
assert_equal 'start_big', special.current_state.to_s
end
test 'Better error message for missing target state' do
class Problem
include Workflow
workflow do
state :initial do
event :solve, :transitions_to => :solved
end
end
end
assert_raise Workflow::WorkflowError do
Problem.new.solve!
end
end
# Intermixing of transition graph definition (states, transitions)
# on the one side and implementation of the actions on the other side
# for a bigger state machine can introduce clutter.
#
# To reduce this clutter it is now possible to use state entry- and
# exit- hooks defined through a naming convention. For example, if there
# is a state :pending, then you can hook in by defining method
# `def on_pending_exit(new_state, event, *args)` instead of using a
# block:
#
# state :pending do
# on_entry do
# # your implementation here
# end
# end
#
# If both a function with a name according to naming convention and the
# on_entry/on_exit block are given, then only on_entry/on_exit block is used.
test 'on_entry and on_exit hooks in separate methods' do
c = Class.new
c.class_eval do
include Workflow
attr_reader :history
def initialize
@history = []
end
workflow do
state :new do
event :next, :transitions_to => :next_state
end
state :next_state
end
def on_next_state_entry(prior_state, event, *args)
@history << "on_next_state_entry #{event} #{prior_state} ->"
end
def on_new_exit(new_state, event, *args)
@history << "on_new_exit #{event} -> #{new_state}"
end
end
o = c.new
assert_equal 'new', o.current_state.to_s
assert_equal [], o.history
o.next!
assert_equal ['on_new_exit next -> next_state', 'on_next_state_entry next new ->'], o.history
end
test 'diagram generation' do
begin
$stdout = StringIO.new('', 'w')
Workflow::create_workflow_diagram(MongoidOrder, 'doc')
assert_match(/open.+\.pdf/, $stdout.string,
'PDF should be generate and a hint be given to the user.')
ensure
$stdout = STDOUT
end
end
test 'halt stops the transition' do
c = Class.new do
include Workflow
workflow do
state :young do
event :age, :transitions_to => :old
end
state :old
end
def age(by=1)
halt 'too fast' if by > 100
end
end
joe = c.new
assert joe.young?
joe.age! 120
assert joe.young?, 'Transition should have been halted'
assert_equal 'too fast', joe.halted_because
end
test 'halt! raises exception immediately' do
article_class = Class.new do
include Workflow
attr_accessor :too_far
workflow do
state :new do
event :reject, :transitions_to => :rejected
end
state :rejected
end
def reject(reason)
halt! 'We do not reject articles unless the reason is important' \
unless reason =~ /important/i
self.too_far = "This line should not be executed"
end
end
article = article_class.new
assert article.new?
assert_raise Workflow::TransitionHalted do
article.reject! 'Too funny'
end
assert_nil article.too_far
assert article.new?, 'Transition should have been halted'
article.reject! 'Important: too short'
assert article.rejected?, 'Transition should happen now'
end
# published gem doesn't have the `can_?` methods yet!
# test 'can fire event?' do
# c = Class.new do
# include Workflow
# workflow do
# state :newborn do
# event :go_to_school, :transitions_to => :schoolboy
# end
# state :schoolboy do
# event :go_to_college, :transitions_to => :student
# end
# state :student
# end
# end
#
# human = c.new
# assert human.can_go_to_school?
# assert_equal false, human.can_go_to_college?
# end
test 'workflow graph generation' do
Dir.chdir('tmp') do
capture_streams do
Workflow::create_workflow_diagram(MongoidOrder)
end
end
end
test 'workflow graph generation in path with spaces' do
`mkdir -p '/tmp/Workflow test'`
capture_streams do
Workflow::create_workflow_diagram(MongoidOrder, '/tmp/Workflow test')
end
end
def capture_streams
old_stdout = $stdout
$stdout = captured_stdout = StringIO.new
yield
$stdout = old_stdout
captured_stdout
end
end