stateful.rb 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +161 -0
  3. data/Gemfile +3 -0
  4. data/README.md +309 -0
  5. data/lib/Stateful/ActiveRecord/ClassMethods.rb +7 -65
  6. data/lib/Stateful/ClassMethods.rb +60 -0
  7. data/lib/Stateful/Poro/ClassMethods.rb +12 -70
  8. data/lib/Stateful/Sequel/ClassMethods.rb +59 -0
  9. data/lib/Stateful/Sequel.rb +30 -0
  10. data/lib/Stateful/VERSION.rb +1 -1
  11. data/lib/Stateful.rb +3 -0
  12. data/stateful.rb.gemspec +42 -0
  13. data/test/ActiveRecord/WithColumnName.rb +159 -0
  14. data/test/ActiveRecord/WithExplicitDeterministicEventOrdering.rb +159 -0
  15. data/test/ActiveRecord/WithExplicitGlobalNonDeterministicEventOrdering.rb +165 -0
  16. data/test/ActiveRecord/WithExplicitGlobalNonDeterministicEventOrderingAndInitialStateBlock.rb +163 -0
  17. data/test/ActiveRecord/WithExplicitNonDeterministicEventOrdering.rb +161 -0
  18. data/test/ActiveRecord/WithInitialStateBlock.rb +157 -0
  19. data/test/ActiveRecord/WithMultipleFinalStates.rb +188 -0
  20. data/test/ActiveRecord/WithMultipleStateMachines.rb +221 -0
  21. data/test/ActiveRecord/WithMultipleStateMachinesAndStatefulBlock.rb +223 -0
  22. data/test/ActiveRecord/WithPersistenceNonSpecificExtend.rb +159 -0
  23. data/test/ActiveRecord/WithPersistenceNonSpecificExtendAndStatefulBlock.rb +161 -0
  24. data/test/ActiveRecord/WithPersistenceSpecificExtend.rb +159 -0
  25. data/test/ActiveRecord/WithPersistenceSpecificExtendAndStatefulBlock.rb +161 -0
  26. data/test/ActiveRecord/test_helper.rb +9 -0
  27. data/test/ActiveRecord.rb +5 -0
  28. data/test/Poro/WithExplicitDeterministicEventOrdering.rb +147 -0
  29. data/test/Poro/WithExplicitGlobalNonDeterministicEventOrdering.rb +153 -0
  30. data/test/Poro/WithExplicitGlobalNonDeterministicEventOrderingAndInitialStateBlock.rb +151 -0
  31. data/test/Poro/WithExplicitNonDeterministicEventOrdering.rb +149 -0
  32. data/test/Poro/WithInitialStateBlock.rb +145 -0
  33. data/test/Poro/WithMultipleFinalStates.rb +176 -0
  34. data/test/Poro/WithMultipleStateMachines.rb +192 -0
  35. data/test/Poro/WithMultipleStateMachinesAndStatefulBlock.rb +194 -0
  36. data/test/Poro/WithPersistenceNonSpecificExtend.rb +147 -0
  37. data/test/Poro/WithPersistenceNonSpecificExtendAndStatefulBlock.rb +149 -0
  38. data/test/Poro/WithPersistenceSpecificExtend.rb +147 -0
  39. data/test/Poro/WithPersistenceSpecificExtendAndStatefulBlock.rb +149 -0
  40. data/test/Poro/WithVariableName.rb +147 -0
  41. data/test/Poro.rb +5 -0
  42. data/test/Sequel/WithColumnName.rb +154 -0
  43. data/test/Sequel/WithExplicitDeterministicEventOrdering.rb +154 -0
  44. data/test/Sequel/WithExplicitGlobalNonDeterministicEventOrdering.rb +160 -0
  45. data/test/Sequel/WithExplicitGlobalNonDeterministicEventOrderingAndInitialStateBlock.rb +158 -0
  46. data/test/Sequel/WithExplicitNonDeterministicEventOrdering.rb +156 -0
  47. data/test/Sequel/WithInitialStateBlock.rb +152 -0
  48. data/test/Sequel/WithMultipleFinalStates.rb +183 -0
  49. data/test/Sequel/WithMultipleStateMachines.rb +216 -0
  50. data/test/Sequel/WithMultipleStateMachinesAndStatefulBlock.rb +218 -0
  51. data/test/Sequel/WithPersistenceNonSpecificExtend.rb +154 -0
  52. data/test/Sequel/WithPersistenceNonSpecificExtendAndStatefulBlock.rb +156 -0
  53. data/test/Sequel/WithPersistenceSpecificExtend.rb +154 -0
  54. data/test/Sequel/WithPersistenceSpecificExtendAndStatefulBlock.rb +156 -0
  55. data/test/Sequel/test_helper.rb +5 -0
  56. data/test/Sequel.rb +5 -0
  57. data/test/Stateful.rb +15 -0
  58. metadata +69 -4
@@ -0,0 +1,216 @@
1
+ # test/Sequel/WithMultipleStateMachines.rb
2
+
3
+ gem 'minitest'
4
+ gem 'minitest-spec-context'
5
+
6
+ require 'minitest/autorun'
7
+ require 'minitest-spec-context'
8
+
9
+ require_relative './test_helper'
10
+
11
+ lib_dir = File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
12
+ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
13
+
14
+ require 'Stateful'
15
+
16
+ DB.create_table :sequel_machine11s do
17
+ primary_key :id
18
+ String :fulfilment_state
19
+ String :payment_state
20
+ end
21
+
22
+ class SequelMachine11 < Sequel::Model
23
+ extend Stateful
24
+
25
+ state_machine :fulfilment do
26
+ initial_state :pending do
27
+ on :items_collected => :ready
28
+ end
29
+
30
+ state :ready do
31
+ on :dispatch => :dispatched
32
+ end
33
+
34
+ final_state :dispatched
35
+ end
36
+
37
+ state_machine :payment do
38
+ initial_state :unpaid do
39
+ on :pay => :paid
40
+ end
41
+
42
+ state :paid do
43
+ on :refund => :refunded
44
+ end
45
+
46
+ final_state :refunded
47
+ end
48
+ end
49
+
50
+ describe Stateful::Sequel do
51
+ let(:machine){SequelMachine11.create}
52
+
53
+ context "initial states" do
54
+ it "must be in the fulfilment initial state" do
55
+ _(machine.fulfilment_initial_state?).must_equal true
56
+ end
57
+
58
+ it "must be in the payment initial state" do
59
+ _(machine.payment_initial_state?).must_equal true
60
+ end
61
+
62
+ it "must have the correct fulfilment state name" do
63
+ _(machine.fulfilment_state.name).must_equal :pending
64
+ end
65
+
66
+ it "must have the correct payment state name" do
67
+ _(machine.payment_state.name).must_equal :unpaid
68
+ end
69
+
70
+ it "must have fulfilment transitions" do
71
+ _(machine.fulfilment_transitions.size).must_equal 1
72
+ _(machine.fulfilment_transitions.first.event_name).must_equal :items_collected
73
+ end
74
+
75
+ it "must have payment transitions" do
76
+ _(machine.payment_transitions.size).must_equal 1
77
+ _(machine.payment_transitions.first.event_name).must_equal :pay
78
+ end
79
+
80
+ it "must not be in a fulfilment final state" do
81
+ _(machine.fulfilment_final_state?).must_equal false
82
+ end
83
+
84
+ it "must not be in a payment final state" do
85
+ _(machine.payment_final_state?).must_equal false
86
+ end
87
+
88
+ it "must have domain predicates" do
89
+ _(machine.pending?).must_equal true
90
+ _(machine.unpaid?).must_equal true
91
+ end
92
+ end
93
+
94
+ context "independent state transitions" do
95
+ before do
96
+ machine.pay
97
+ end
98
+
99
+ it "must still be in the fulfilment initial state" do
100
+ _(machine.fulfilment_initial_state?).must_equal true
101
+ _(machine.pending?).must_equal true
102
+ end
103
+
104
+ it "must have advanced the payment state" do
105
+ _(machine.payment_state.name).must_equal :paid
106
+ _(machine.paid?).must_equal true
107
+ end
108
+
109
+ it "must not be in the payment initial state" do
110
+ _(machine.payment_initial_state?).must_equal false
111
+ end
112
+
113
+ it "must have persisted the payment state" do
114
+ machine.refresh
115
+ _(machine.payment_state.name).must_equal :paid
116
+ end
117
+
118
+ it "must have persisted the fulfilment state as initial" do
119
+ machine.refresh
120
+ _(machine.fulfilment_state.name).must_equal :pending
121
+ end
122
+ end
123
+
124
+ context "both machines advanced" do
125
+ before do
126
+ machine.items_collected
127
+ machine.pay
128
+ end
129
+
130
+ it "must be in the ready fulfilment state" do
131
+ _(machine.fulfilment_state.name).must_equal :ready
132
+ _(machine.ready?).must_equal true
133
+ end
134
+
135
+ it "must be in the paid payment state" do
136
+ _(machine.payment_state.name).must_equal :paid
137
+ _(machine.paid?).must_equal true
138
+ end
139
+
140
+ it "must have correct fulfilment transitions" do
141
+ _(machine.fulfilment_transitions.size).must_equal 1
142
+ _(machine.fulfilment_transitions.first.event_name).must_equal :dispatch
143
+ end
144
+
145
+ it "must have correct payment transitions" do
146
+ _(machine.payment_transitions.size).must_equal 1
147
+ _(machine.payment_transitions.first.event_name).must_equal :refund
148
+ end
149
+
150
+ it "must have persisted both states" do
151
+ machine.refresh
152
+ _(machine.fulfilment_state.name).must_equal :ready
153
+ _(machine.payment_state.name).must_equal :paid
154
+ end
155
+ end
156
+
157
+ context "fulfilment final state" do
158
+ before do
159
+ machine.items_collected
160
+ machine.dispatch
161
+ end
162
+
163
+ it "must be in the fulfilment final state" do
164
+ _(machine.fulfilment_final_state?).must_equal true
165
+ _(machine.dispatched?).must_equal true
166
+ end
167
+
168
+ it "must have no fulfilment transitions" do
169
+ _(machine.fulfilment_transitions.empty?).must_equal true
170
+ end
171
+ end
172
+
173
+ context "payment final state" do
174
+ before do
175
+ machine.pay
176
+ machine.refund
177
+ end
178
+
179
+ it "must be in the payment final state" do
180
+ _(machine.payment_final_state?).must_equal true
181
+ _(machine.refunded?).must_equal true
182
+ end
183
+
184
+ it "must have no payment transitions" do
185
+ _(machine.payment_transitions.empty?).must_equal true
186
+ end
187
+ end
188
+
189
+ context "active?" do
190
+ it "must be active when all machines are in initial states" do
191
+ _(machine.active?).must_equal true
192
+ end
193
+
194
+ it "must be active when one machine has reached final state" do
195
+ machine.items_collected
196
+ machine.dispatch
197
+ _(machine.fulfilment_final_state?).must_equal true
198
+ _(machine.active?).must_equal true
199
+ end
200
+
201
+ it "must not be active when all machines have reached final states" do
202
+ machine.items_collected
203
+ machine.dispatch
204
+ machine.pay
205
+ machine.refund
206
+ _(machine.active?).must_equal false
207
+ end
208
+
209
+ it "must be able to check a specific machine" do
210
+ machine.items_collected
211
+ machine.dispatch
212
+ _(machine.active?(:fulfilment)).must_equal false
213
+ _(machine.active?(:payment)).must_equal true
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,218 @@
1
+ # test/Sequel/WithMultipleStateMachinesAndStatefulBlock.rb
2
+
3
+ gem 'minitest'
4
+ gem 'minitest-spec-context'
5
+
6
+ require 'minitest/autorun'
7
+ require 'minitest-spec-context'
8
+
9
+ require_relative './test_helper'
10
+
11
+ lib_dir = File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
12
+ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
13
+
14
+ require 'Stateful'
15
+
16
+ DB.create_table :sequel_machine12s do
17
+ primary_key :id
18
+ String :fulfilment_state
19
+ String :payment_state
20
+ end
21
+
22
+ class SequelMachine12 < Sequel::Model
23
+ extend Stateful
24
+
25
+ stateful do
26
+ state_machine :fulfilment do
27
+ initial_state :pending do
28
+ on :items_collected => :ready
29
+ end
30
+
31
+ state :ready do
32
+ on :dispatch => :dispatched
33
+ end
34
+
35
+ final_state :dispatched
36
+ end
37
+
38
+ state_machine :payment do
39
+ initial_state :unpaid do
40
+ on :pay => :paid
41
+ end
42
+
43
+ state :paid do
44
+ on :refund => :refunded
45
+ end
46
+
47
+ final_state :refunded
48
+ end
49
+ end
50
+ end
51
+
52
+ describe Stateful::Sequel do
53
+ let(:machine){SequelMachine12.create}
54
+
55
+ context "initial states" do
56
+ it "must be in the fulfilment initial state" do
57
+ _(machine.fulfilment_initial_state?).must_equal true
58
+ end
59
+
60
+ it "must be in the payment initial state" do
61
+ _(machine.payment_initial_state?).must_equal true
62
+ end
63
+
64
+ it "must have the correct fulfilment state name" do
65
+ _(machine.fulfilment_state.name).must_equal :pending
66
+ end
67
+
68
+ it "must have the correct payment state name" do
69
+ _(machine.payment_state.name).must_equal :unpaid
70
+ end
71
+
72
+ it "must have fulfilment transitions" do
73
+ _(machine.fulfilment_transitions.size).must_equal 1
74
+ _(machine.fulfilment_transitions.first.event_name).must_equal :items_collected
75
+ end
76
+
77
+ it "must have payment transitions" do
78
+ _(machine.payment_transitions.size).must_equal 1
79
+ _(machine.payment_transitions.first.event_name).must_equal :pay
80
+ end
81
+
82
+ it "must not be in a fulfilment final state" do
83
+ _(machine.fulfilment_final_state?).must_equal false
84
+ end
85
+
86
+ it "must not be in a payment final state" do
87
+ _(machine.payment_final_state?).must_equal false
88
+ end
89
+
90
+ it "must have domain predicates" do
91
+ _(machine.pending?).must_equal true
92
+ _(machine.unpaid?).must_equal true
93
+ end
94
+ end
95
+
96
+ context "independent state transitions" do
97
+ before do
98
+ machine.pay
99
+ end
100
+
101
+ it "must still be in the fulfilment initial state" do
102
+ _(machine.fulfilment_initial_state?).must_equal true
103
+ _(machine.pending?).must_equal true
104
+ end
105
+
106
+ it "must have advanced the payment state" do
107
+ _(machine.payment_state.name).must_equal :paid
108
+ _(machine.paid?).must_equal true
109
+ end
110
+
111
+ it "must not be in the payment initial state" do
112
+ _(machine.payment_initial_state?).must_equal false
113
+ end
114
+
115
+ it "must have persisted the payment state" do
116
+ machine.refresh
117
+ _(machine.payment_state.name).must_equal :paid
118
+ end
119
+
120
+ it "must have persisted the fulfilment state as initial" do
121
+ machine.refresh
122
+ _(machine.fulfilment_state.name).must_equal :pending
123
+ end
124
+ end
125
+
126
+ context "both machines advanced" do
127
+ before do
128
+ machine.items_collected
129
+ machine.pay
130
+ end
131
+
132
+ it "must be in the ready fulfilment state" do
133
+ _(machine.fulfilment_state.name).must_equal :ready
134
+ _(machine.ready?).must_equal true
135
+ end
136
+
137
+ it "must be in the paid payment state" do
138
+ _(machine.payment_state.name).must_equal :paid
139
+ _(machine.paid?).must_equal true
140
+ end
141
+
142
+ it "must have correct fulfilment transitions" do
143
+ _(machine.fulfilment_transitions.size).must_equal 1
144
+ _(machine.fulfilment_transitions.first.event_name).must_equal :dispatch
145
+ end
146
+
147
+ it "must have correct payment transitions" do
148
+ _(machine.payment_transitions.size).must_equal 1
149
+ _(machine.payment_transitions.first.event_name).must_equal :refund
150
+ end
151
+
152
+ it "must have persisted both states" do
153
+ machine.refresh
154
+ _(machine.fulfilment_state.name).must_equal :ready
155
+ _(machine.payment_state.name).must_equal :paid
156
+ end
157
+ end
158
+
159
+ context "fulfilment final state" do
160
+ before do
161
+ machine.items_collected
162
+ machine.dispatch
163
+ end
164
+
165
+ it "must be in the fulfilment final state" do
166
+ _(machine.fulfilment_final_state?).must_equal true
167
+ _(machine.dispatched?).must_equal true
168
+ end
169
+
170
+ it "must have no fulfilment transitions" do
171
+ _(machine.fulfilment_transitions.empty?).must_equal true
172
+ end
173
+ end
174
+
175
+ context "payment final state" do
176
+ before do
177
+ machine.pay
178
+ machine.refund
179
+ end
180
+
181
+ it "must be in the payment final state" do
182
+ _(machine.payment_final_state?).must_equal true
183
+ _(machine.refunded?).must_equal true
184
+ end
185
+
186
+ it "must have no payment transitions" do
187
+ _(machine.payment_transitions.empty?).must_equal true
188
+ end
189
+ end
190
+
191
+ context "active?" do
192
+ it "must be active when all machines are in initial states" do
193
+ _(machine.active?).must_equal true
194
+ end
195
+
196
+ it "must be active when one machine has reached final state" do
197
+ machine.items_collected
198
+ machine.dispatch
199
+ _(machine.fulfilment_final_state?).must_equal true
200
+ _(machine.active?).must_equal true
201
+ end
202
+
203
+ it "must not be active when all machines have reached final states" do
204
+ machine.items_collected
205
+ machine.dispatch
206
+ machine.pay
207
+ machine.refund
208
+ _(machine.active?).must_equal false
209
+ end
210
+
211
+ it "must be able to check a specific machine" do
212
+ machine.items_collected
213
+ machine.dispatch
214
+ _(machine.active?(:fulfilment)).must_equal false
215
+ _(machine.active?(:payment)).must_equal true
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,154 @@
1
+ # test/Sequel/WithPersistenceNonSpecificExtend.rb
2
+
3
+ gem 'minitest'
4
+ gem 'minitest-spec-context'
5
+
6
+ require 'minitest/autorun'
7
+ require 'minitest-spec-context'
8
+
9
+ require_relative './test_helper'
10
+
11
+ lib_dir = File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
12
+ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
13
+
14
+ require 'Stateful'
15
+
16
+ DB.create_table :sequel_machine0s do
17
+ primary_key :id
18
+ String :current_state
19
+ end
20
+
21
+ class SequelMachine0 < Sequel::Model
22
+ extend Stateful
23
+
24
+ initial_state :initial_state
25
+
26
+ state :initial_state do
27
+ on :an_event => :next_state
28
+ on :another_event => :final_state
29
+ end
30
+
31
+ state :next_state do
32
+ on :yet_another_event => :final_state
33
+ end
34
+
35
+ final_state :final_state
36
+ end
37
+
38
+ describe Stateful::Sequel do
39
+ let(:machine){SequelMachine0.create}
40
+
41
+ it "must have an initial state" do
42
+ _(machine.initial_state).wont_be_nil
43
+ end
44
+
45
+ it "must have a final state (if one has been specified)" do
46
+ if SequelMachine0.final_state?
47
+ _(machine.final_state).wont_be_nil
48
+ end
49
+ end
50
+
51
+ context "initial_state" do
52
+ it "must have an initial state before any events occur" do
53
+ _(machine.initial_state?).must_equal true
54
+ end
55
+
56
+ it "must have an initial state consistent with what is given" do
57
+ _(machine.initial_state).must_equal SequelMachine0.stateful_state_machine.initial_state
58
+ end
59
+
60
+ it "must have an initial state with name as per the name given" do
61
+ _(machine.initial_state.name).must_equal :initial_state
62
+ end
63
+
64
+ it "must not be in the final state (assuming that initial and final are different states)" do
65
+ _(machine.final_state?).must_equal false
66
+ end
67
+
68
+ it "must know what it's next state is given an event name" do
69
+ _(machine.next_state(:an_event)).must_equal SequelMachine0.stateful_state_machine.find(:next_state)
70
+ _(machine.next_state(:another_event)).must_equal SequelMachine0.stateful_state_machine.find(:final_state)
71
+ end
72
+
73
+ it "must have an intial state which has as set of transitions to other states" do
74
+ _(machine.transitions.class).must_equal Array
75
+ end
76
+
77
+ it "must have two transitions to other states" do
78
+ _(machine.transitions.size).must_equal 2
79
+ end
80
+
81
+ it "must know what transitions are available" do
82
+ _(machine.transitions.collect{|t| [t.event_name, t.next_state_name]}).must_equal [[:an_event, :next_state], [:another_event, :final_state]]
83
+ end
84
+
85
+ it "must be active" do
86
+ _(machine.active?).must_equal true
87
+ end
88
+ end
89
+
90
+ context "next_state" do
91
+ before do
92
+ machine.an_event
93
+ end
94
+
95
+ it "must not be in the initial state" do
96
+ _(machine.initial_state?).must_equal false
97
+ end
98
+
99
+ it "must not be in the final state" do
100
+ _(machine.final_state?).must_equal false
101
+ end
102
+
103
+ it "must be in the next_state state" do
104
+ _(machine.current_state.name).must_equal :next_state
105
+ _(machine.next_state?).must_equal true
106
+ end
107
+
108
+ it "must have a set of transitions to other states" do
109
+ _(machine.transitions.class).must_equal Array
110
+ end
111
+
112
+ it "must have one transition to other states" do
113
+ _(machine.transitions.size).must_equal 1
114
+ end
115
+
116
+ it "must know what transitions are available" do
117
+ _(machine.transitions.collect{|t| [t.event_name, t.next_state_name]}).must_equal [[:yet_another_event, :final_state]]
118
+ end
119
+
120
+ it "must be active" do
121
+ _(machine.active?).must_equal true
122
+ end
123
+ end
124
+
125
+ context "final_state" do
126
+ before do
127
+ machine.another_event
128
+ end
129
+
130
+ it "must have a final state" do
131
+ _(machine.final_state?).must_equal true
132
+ end
133
+
134
+ it "must have a final state consistent with what is given" do
135
+ _(machine.final_state).must_equal SequelMachine0.stateful_state_machine.final_state
136
+ end
137
+
138
+ it "must have a state with name as per the name given" do
139
+ _(machine.current_state.name).must_equal :final_state
140
+ end
141
+
142
+ it "must not be in the initial state (assuming that initial and final are different states)" do
143
+ _(machine.initial_state?).must_equal false
144
+ end
145
+
146
+ it "must have no transitions" do
147
+ _(machine.transitions.empty?).must_equal true
148
+ end
149
+
150
+ it "must not be active" do
151
+ _(machine.active?).must_equal false
152
+ end
153
+ end
154
+ end