sandthorn 0.13.0 → 1.0.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.
- checksums.yaml +4 -4
- data/.travis.yml +4 -2
- data/README.md +25 -147
- data/lib/sandthorn.rb +3 -33
- data/lib/sandthorn/aggregate_root_base.rb +35 -45
- data/lib/sandthorn/event.rb +3 -2
- data/lib/sandthorn/event_inspector.rb +12 -10
- data/lib/sandthorn/version.rb +1 -1
- data/sandthorn.gemspec +1 -1
- data/spec/aggregate_delta_spec.rb +4 -4
- data/spec/aggregate_events_spec.rb +2 -8
- data/spec/aggregate_root_spec.rb +8 -8
- data/spec/aggregate_snapshot_spec.rb +263 -263
- data/spec/complex_aggregate_spec.rb +2 -2
- data/spec/constructor_events_spec.rb +3 -7
- data/spec/event_spec.rb +3 -3
- data/spec/spec_helper.rb +14 -11
- data/spec/stateless_events_spec.rb +31 -15
- data/spec/tracing_spec.rb +9 -11
- metadata +5 -10
- data/spec/get_events_spec.rb +0 -74
- data/spec/sandthorn_spec.rb +0 -72
data/lib/sandthorn/event.rb
CHANGED
@@ -8,7 +8,8 @@ module Sandthorn
|
|
8
8
|
aggregate_version
|
9
9
|
timestamp
|
10
10
|
event_name
|
11
|
-
|
11
|
+
event_data
|
12
|
+
event_metadata
|
12
13
|
method_args
|
13
14
|
trace
|
14
15
|
)
|
@@ -40,7 +41,7 @@ module Sandthorn
|
|
40
41
|
end
|
41
42
|
|
42
43
|
def raw_deltas
|
43
|
-
fetch(:
|
44
|
+
fetch(:event_data, {}).fetch(:attribute_deltas, [])
|
44
45
|
end
|
45
46
|
|
46
47
|
class AttributeDelta < SimpleDelegator
|
@@ -46,27 +46,29 @@ module Sandthorn
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def events_with_trace_info
|
49
|
-
|
49
|
+
begin
|
50
|
+
saved = Sandthorn.find aggregate_id, self.class
|
51
|
+
rescue Exception
|
52
|
+
saved = []
|
53
|
+
end
|
54
|
+
|
50
55
|
unsaved = self.aggregate_events
|
51
56
|
all = saved
|
52
57
|
.concat(unsaved)
|
53
58
|
.sort { |a, b| a[:aggregate_version] <=> b[:aggregate_version] }
|
54
59
|
|
55
|
-
extracted = all.collect do |e|
|
56
|
-
if e[:
|
60
|
+
extracted = all.collect do |e|
|
61
|
+
if e[:event_data].nil? && !e[:event_data].nil?
|
57
62
|
data = Sandthorn.deserialize e[:event_data]
|
58
63
|
else
|
59
|
-
data = e[:
|
60
|
-
end
|
61
|
-
|
62
|
-
unless data.nil? || !data.is_a?(Hash)
|
63
|
-
trace = data[:trace]
|
64
|
+
data = e[:event_data]
|
64
65
|
end
|
65
66
|
|
66
67
|
{
|
67
68
|
aggregate_version: e[:aggregate_version],
|
68
69
|
event_name: e[:event_name].to_sym,
|
69
|
-
|
70
|
+
event_data: data,
|
71
|
+
event_metadata: e[:event_metadata]
|
70
72
|
}
|
71
73
|
end
|
72
74
|
|
@@ -80,7 +82,7 @@ module Sandthorn
|
|
80
82
|
end
|
81
83
|
|
82
84
|
def get_saved_events event_name
|
83
|
-
saved_events = Sandthorn.
|
85
|
+
saved_events = Sandthorn.find self.class, self.aggregate_id
|
84
86
|
saved_events.select { |e| e[:event_name] == event_name.to_s }
|
85
87
|
end
|
86
88
|
|
data/lib/sandthorn/version.rb
CHANGED
data/sandthorn.gemspec
CHANGED
@@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency "autotest-standalone"
|
31
31
|
spec.add_development_dependency "sqlite3"
|
32
32
|
spec.add_development_dependency "coveralls"
|
33
|
-
spec.add_development_dependency "sandthorn_driver_sequel", ">=
|
33
|
+
spec.add_development_dependency "sandthorn_driver_sequel", ">= 4.0"
|
34
34
|
end
|
@@ -20,22 +20,22 @@ class PersonTest
|
|
20
20
|
|
21
21
|
def change_name new_name
|
22
22
|
@name = new_name
|
23
|
-
record_event
|
23
|
+
record_event
|
24
24
|
end
|
25
25
|
|
26
26
|
def change_relationship new_relationship
|
27
27
|
@relationship_status = new_relationship
|
28
|
-
record_event
|
28
|
+
record_event
|
29
29
|
end
|
30
30
|
|
31
31
|
def add_to_array element
|
32
32
|
@my_array << element
|
33
|
-
record_event
|
33
|
+
record_event
|
34
34
|
end
|
35
35
|
|
36
36
|
def add_to_hash name,value
|
37
37
|
@my_hash[name] = value
|
38
|
-
record_event
|
38
|
+
record_event
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -19,7 +19,7 @@ module Sandthorn
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def old_way_event event_params
|
22
|
-
commit
|
22
|
+
commit
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -52,7 +52,7 @@ module Sandthorn
|
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should store the args to the event" do
|
55
|
-
expect(subject.aggregate_events[1][:
|
55
|
+
expect(subject.aggregate_events[1][:event_data][:attribute_deltas][0][:new_value]).to eql("new name")
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should store the event_name" do
|
@@ -70,9 +70,6 @@ module Sandthorn
|
|
70
70
|
expect(subject.has_event?(:some_other_event)).to be_truthy
|
71
71
|
end
|
72
72
|
|
73
|
-
it "should store the args to the event" do
|
74
|
-
expect(subject.aggregate_events[1][:event_args][:method_args]).to eql([1,2])
|
75
|
-
end
|
76
73
|
end
|
77
74
|
|
78
75
|
describe ".old_way_event" do
|
@@ -85,9 +82,6 @@ module Sandthorn
|
|
85
82
|
expect(subject.has_event?(:old_way_event)).to be_truthy
|
86
83
|
end
|
87
84
|
|
88
|
-
it "should store the args to the event" do
|
89
|
-
expect(subject.aggregate_events[1][:event_args][:method_args][0]).to eql("hej")
|
90
|
-
end
|
91
85
|
end
|
92
86
|
end
|
93
87
|
end
|
data/spec/aggregate_root_spec.rb
CHANGED
@@ -167,7 +167,7 @@ module Sandthorn
|
|
167
167
|
|
168
168
|
it "should set the old_value on the event" do
|
169
169
|
dirty_object_after_find.change_name "new_name"
|
170
|
-
expect(dirty_object_after_find.aggregate_events.last[:
|
170
|
+
expect(dirty_object_after_find.aggregate_events.last[:event_data][:attribute_deltas].first[:old_value]).to eql "old_value"
|
171
171
|
end
|
172
172
|
|
173
173
|
end
|
@@ -176,18 +176,18 @@ module Sandthorn
|
|
176
176
|
|
177
177
|
it "should set the old_value on the event" do
|
178
178
|
dirty_object.change_name "new_name"
|
179
|
-
expect(dirty_object.aggregate_events.last[:
|
179
|
+
expect(dirty_object.aggregate_events.last[:event_data][:attribute_deltas].first[:old_value]).to eql "old_value"
|
180
180
|
end
|
181
181
|
|
182
182
|
it "should not change aggregate_id" do
|
183
183
|
dirty_object.change_name "new_name"
|
184
|
-
expect(dirty_object.aggregate_events.last[:
|
184
|
+
expect(dirty_object.aggregate_events.last[:event_data][:attribute_deltas].last[:attribute_name]).not_to eql "aggregate_id"
|
185
185
|
end
|
186
186
|
|
187
187
|
it "should not change sex attribute if sex method is not runned" do
|
188
188
|
dirty_object.change_name "new_name"
|
189
189
|
dirty_object.aggregate_events.each do |event|
|
190
|
-
event[:
|
190
|
+
event[:event_data][:attribute_deltas].each do |attribute_delta|
|
191
191
|
expect(attribute_delta[:attribute_name]).not_to eql "sex"
|
192
192
|
end
|
193
193
|
end
|
@@ -196,7 +196,7 @@ module Sandthorn
|
|
196
196
|
it "should not change sex attribute if sex attribute is the same" do
|
197
197
|
dirty_object.change_sex "hen"
|
198
198
|
dirty_object.aggregate_events.each do |event|
|
199
|
-
event[:
|
199
|
+
event[:event_data][:attribute_deltas].each do |attribute_delta|
|
200
200
|
expect(attribute_delta[:attribute_name]).not_to eql "sex"
|
201
201
|
end
|
202
202
|
end
|
@@ -204,8 +204,8 @@ module Sandthorn
|
|
204
204
|
|
205
205
|
it "should set old_value and new_value on sex change" do
|
206
206
|
dirty_object.change_sex "shemale"
|
207
|
-
expect(dirty_object.aggregate_events.last[:
|
208
|
-
expect(dirty_object.aggregate_events.last[:
|
207
|
+
expect(dirty_object.aggregate_events.last[:event_data][:attribute_deltas].first[:old_value]).to eql "hen"
|
208
|
+
expect(dirty_object.aggregate_events.last[:event_data][:attribute_deltas].first[:new_value]).to eql "shemale"
|
209
209
|
end
|
210
210
|
end
|
211
211
|
end
|
@@ -222,7 +222,7 @@ module Sandthorn
|
|
222
222
|
end
|
223
223
|
|
224
224
|
it "should have attribute_deltas set to empty array" do
|
225
|
-
expect(dirty_object.aggregate_events.first[:
|
225
|
+
expect(dirty_object.aggregate_events.first[:event_data][:attribute_deltas]).to eql([])
|
226
226
|
end
|
227
227
|
|
228
228
|
end
|
@@ -1,263 +1,263 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'sandthorn/aggregate_root_snapshot'
|
3
|
-
require 'date'
|
4
|
-
|
5
|
-
|
6
|
-
module BankAccountInterestCommands
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
module BankAccountWithdrawalCommands
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
module BankAccountVisaCardPurchasesCommands
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
class VisaCardTransactionGateway
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
module BankAccountDepositCommmands
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
class BankAccount
|
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
|
-
end
|
139
|
-
|
140
|
-
def a_test_account
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
end
|
153
|
-
|
154
|
-
#Tests part
|
155
|
-
describe "when doing aggregate_find on an aggregate with a snapshot" do
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
end
|
172
|
-
|
173
|
-
describe 'when generating state on an aggregate root' do
|
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
|
-
end
|
206
|
-
|
207
|
-
describe Sandthorn::AggregateRootSnapshot do
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
end
|
219
|
-
|
220
|
-
|
221
|
-
describe 'when saving to repository' do
|
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
|
-
end
|
1
|
+
# require 'spec_helper'
|
2
|
+
# require 'sandthorn/aggregate_root_snapshot'
|
3
|
+
# require 'date'
|
4
|
+
|
5
|
+
|
6
|
+
# module BankAccountInterestCommands
|
7
|
+
# def calculate_interest! until_date = DateTime.now
|
8
|
+
# # skipping all safety-checks..
|
9
|
+
# # and this is of course horribly wrong financially speaking.. whatever
|
10
|
+
# pay_out_unpaid_interest!
|
11
|
+
# interest_calculation_time = until_date - @last_interest_calculation
|
12
|
+
# days_with_interest = interest_calculation_time.to_i
|
13
|
+
# unpaid_interest = @balance * @current_interest_info[:interest_rate] * days_with_interest / 365.2425
|
14
|
+
# added_unpaid_interest_event unpaid_interest,until_date
|
15
|
+
# end
|
16
|
+
|
17
|
+
# def pay_out_unpaid_interest!
|
18
|
+
# paid_out_unpaid_interest_balance_event @unpaid_interest_balance
|
19
|
+
# end
|
20
|
+
|
21
|
+
# def change_interest! new_interest_rate, interest_valid_from
|
22
|
+
# calculate_interest!
|
23
|
+
# changed_interest_rate_event new_interest_rate,interest_valid_from
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
# module BankAccountWithdrawalCommands
|
27
|
+
# def withdraw_from_atm! amount, atm_id
|
28
|
+
# withdrew_amount_from_atm_event amount, atm_id
|
29
|
+
# end
|
30
|
+
|
31
|
+
# def withdraw_from_cashier! amount, cashier_id
|
32
|
+
# withdrew_amount_from_cashier_event amount, cashier_id
|
33
|
+
# charged_cashier_withdrawal_fee_event 50
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
|
37
|
+
# module BankAccountVisaCardPurchasesCommands
|
38
|
+
# def charge_card! amount, merchant_id
|
39
|
+
# visa = VisaCardTransactionGateway.new
|
40
|
+
# transaction_id = visa.charge_card "3030-3333-4252-2535", merchant_id, amount
|
41
|
+
# paid_with_visa_card_event amount, transaction_id
|
42
|
+
# end
|
43
|
+
|
44
|
+
# end
|
45
|
+
|
46
|
+
# class VisaCardTransactionGateway
|
47
|
+
# def initialize
|
48
|
+
# @visa_connector = "foo_bar"
|
49
|
+
# end
|
50
|
+
# def charge_card visa_card_number, merchant_id, amount
|
51
|
+
# transaction_id = SecureRandom.uuid
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
|
55
|
+
# module BankAccountDepositCommmands
|
56
|
+
# def deposit_at_bank_office! amount, cashier_id
|
57
|
+
# deposited_to_cashier_event amount, cashier_id
|
58
|
+
# end
|
59
|
+
|
60
|
+
# def transfer_money_from_another_account! amount, from_account_number
|
61
|
+
# incoming_transfer_event amount,from_account_number
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
|
65
|
+
# class BankAccount
|
66
|
+
# include Sandthorn::AggregateRoot
|
67
|
+
|
68
|
+
# attr_reader :balance
|
69
|
+
# attr_reader :account_number
|
70
|
+
# attr_reader :current_interest_info
|
71
|
+
# attr_reader :account_creation_date
|
72
|
+
# attr_reader :unpaid_interest_balance
|
73
|
+
# attr_reader :last_interest_calculation
|
74
|
+
|
75
|
+
# def initialize *args
|
76
|
+
# account_number = args[0]
|
77
|
+
# interest_rate = args[1]
|
78
|
+
# creation_date = args[2]
|
79
|
+
|
80
|
+
# @current_interest_info = {}
|
81
|
+
# @current_interest_info[:interest_rate] = interest_rate
|
82
|
+
# @current_interest_info[:interest_valid_from] = creation_date
|
83
|
+
# @balance = 0
|
84
|
+
# @unpaid_interest_balance = 0
|
85
|
+
# @account_creation_date = creation_date
|
86
|
+
# @last_interest_calculation = creation_date
|
87
|
+
# end
|
88
|
+
|
89
|
+
# def changed_interest_rate_event new_interest_rate, interest_valid_from
|
90
|
+
# @current_interest_info[:interest_rate] = new_interest_rate
|
91
|
+
# @current_interest_info[:interest_valid_from] = interest_valid_from
|
92
|
+
# record_event
|
93
|
+
# end
|
94
|
+
|
95
|
+
# def added_unpaid_interest_event interest_amount, calculated_until
|
96
|
+
# @unpaid_interest_balance += interest_amount
|
97
|
+
# @last_interest_calculation = calculated_until
|
98
|
+
# record_event
|
99
|
+
# end
|
100
|
+
|
101
|
+
# def paid_out_unpaid_interest_balance_event interest_amount
|
102
|
+
# @unpaid_interest_balance -= interest_amount
|
103
|
+
# @balance += interest_amount
|
104
|
+
# record_event
|
105
|
+
# end
|
106
|
+
|
107
|
+
# def withdrew_amount_from_atm_event amount, atm_id
|
108
|
+
# @balance -= amount
|
109
|
+
# record_event
|
110
|
+
# end
|
111
|
+
|
112
|
+
# def withdrew_amount_from_cashier_event amount, cashier_id
|
113
|
+
# @balance -= amount
|
114
|
+
# record_event
|
115
|
+
# end
|
116
|
+
|
117
|
+
# def paid_with_visa_card_event amount, visa_card_transaction_id
|
118
|
+
# @balance -= amount
|
119
|
+
# record_event
|
120
|
+
# end
|
121
|
+
|
122
|
+
# def charged_cashier_withdrawal_fee_event amount
|
123
|
+
# @balance -= amount
|
124
|
+
# record_event
|
125
|
+
# end
|
126
|
+
|
127
|
+
# def deposited_to_cashier_event amount, cashier_id
|
128
|
+
# @balance = self.balance + amount
|
129
|
+
# record_event
|
130
|
+
# end
|
131
|
+
|
132
|
+
# def incoming_transfer_event amount, from_account_number
|
133
|
+
# current_balance = self.balance
|
134
|
+
# @balance = amount + current_balance
|
135
|
+
# record_event
|
136
|
+
# end
|
137
|
+
|
138
|
+
# end
|
139
|
+
|
140
|
+
# def a_test_account
|
141
|
+
# a = BankAccount.new "91503010111",0.031415, Date.new(2011,10,12)
|
142
|
+
# a.extend BankAccountDepositCommmands
|
143
|
+
# a.transfer_money_from_another_account! 90000, "FOOBAR"
|
144
|
+
# a.deposit_at_bank_office! 10000, "Lars Idorn"
|
145
|
+
|
146
|
+
# a.extend BankAccountVisaCardPurchasesCommands
|
147
|
+
# a.charge_card! 1000, "Starbucks Coffee"
|
148
|
+
|
149
|
+
# a.extend BankAccountInterestCommands
|
150
|
+
# a.calculate_interest!
|
151
|
+
# return a
|
152
|
+
# end
|
153
|
+
|
154
|
+
# #Tests part
|
155
|
+
# describe "when doing aggregate_find on an aggregate with a snapshot" do
|
156
|
+
# let(:aggregate) do
|
157
|
+
# a = a_test_account
|
158
|
+
# a.save
|
159
|
+
# a.extend Sandthorn::AggregateRootSnapshot
|
160
|
+
# a.aggregate_snapshot!
|
161
|
+
# a.save_snapshot
|
162
|
+
# a.charge_card! 9000, "Apple"
|
163
|
+
# a.save
|
164
|
+
# a
|
165
|
+
# end
|
166
|
+
# it "should be loaded with correct version" do
|
167
|
+
# org = aggregate
|
168
|
+
# loaded = BankAccount.find org.aggregate_id
|
169
|
+
# expect(loaded.balance).to eql org.balance
|
170
|
+
# end
|
171
|
+
# end
|
172
|
+
|
173
|
+
# describe 'when generating state on an aggregate root' do
|
174
|
+
|
175
|
+
# before(:each) do
|
176
|
+
# @original_account = a_test_account
|
177
|
+
# events = @original_account.aggregate_events
|
178
|
+
# @account = BankAccount.aggregate_build events
|
179
|
+
# @account.extend Sandthorn::AggregateRootSnapshot
|
180
|
+
# @account.aggregate_snapshot!
|
181
|
+
# end
|
182
|
+
|
183
|
+
# it 'account should have properties set' do
|
184
|
+
# expect(@account.balance).to eql 99000
|
185
|
+
# expect(@account.unpaid_interest_balance).to be > 1000
|
186
|
+
# end
|
187
|
+
|
188
|
+
|
189
|
+
# it 'should be able to load up from snapshot' do
|
190
|
+
|
191
|
+
# events = [aggregate: @account]
|
192
|
+
# loaded = BankAccount.aggregate_build events
|
193
|
+
|
194
|
+
# expect(loaded.balance).to eql(@original_account.balance)
|
195
|
+
# expect(loaded.account_number).to eql(@original_account.account_number)
|
196
|
+
# expect(loaded.current_interest_info).to eql(@original_account.current_interest_info)
|
197
|
+
# expect(loaded.account_creation_date).to eql(@original_account.account_creation_date)
|
198
|
+
# expect(loaded.unpaid_interest_balance).to eql(@original_account.unpaid_interest_balance)
|
199
|
+
# expect(loaded.last_interest_calculation).to eql(@original_account.last_interest_calculation)
|
200
|
+
# expect(loaded.aggregate_id).to eql(@original_account.aggregate_id)
|
201
|
+
# expect(loaded.aggregate_originating_version).to eql(@account.aggregate_originating_version)
|
202
|
+
|
203
|
+
# end
|
204
|
+
|
205
|
+
# end
|
206
|
+
|
207
|
+
# describe Sandthorn::AggregateRootSnapshot do
|
208
|
+
# let(:subject) { a_test_account.save.extend Sandthorn::AggregateRootSnapshot }
|
209
|
+
# context "when using :snapshot - method" do
|
210
|
+
# it "should return self" do
|
211
|
+
# expect(subject.snapshot).to eql subject
|
212
|
+
# end
|
213
|
+
# it "should raise SnapshotError if aggregate has unsaved events" do
|
214
|
+
# subject.paid_with_visa_card_event 2000, ""
|
215
|
+
# expect{subject.snapshot}.to raise_error Sandthorn::Errors::SnapshotError
|
216
|
+
# end
|
217
|
+
# end
|
218
|
+
# end
|
219
|
+
|
220
|
+
|
221
|
+
# describe 'when saving to repository' do
|
222
|
+
# let(:account) {a_test_account.extend Sandthorn::AggregateRootSnapshot}
|
223
|
+
|
224
|
+
# it 'should not raise an error if snapshot was created' do
|
225
|
+
# account.save
|
226
|
+
# account.aggregate_snapshot!
|
227
|
+
# expect(lambda {account.save_snapshot}).not_to raise_error
|
228
|
+
# end
|
229
|
+
# it 'should set aggregate_snapshot to nil' do
|
230
|
+
# account.save
|
231
|
+
# account.aggregate_snapshot!
|
232
|
+
# account.save_snapshot
|
233
|
+
# expect(account.aggregate_snapshot).to eql(nil)
|
234
|
+
# end
|
235
|
+
|
236
|
+
# it 'should raise error if trying to create snapshot before events are saved on object' do
|
237
|
+
# expect(lambda {account.aggregate_snapshot!}).to raise_error Sandthorn::Errors::SnapshotError
|
238
|
+
# end
|
239
|
+
|
240
|
+
# it 'should not raise an error if trying to create snapshot on object when events are saved' do
|
241
|
+
# account.save
|
242
|
+
# expect( lambda {account.aggregate_snapshot!}).not_to raise_error
|
243
|
+
# end
|
244
|
+
|
245
|
+
# it 'should get snapshot on account find when a snapshot is saved' do
|
246
|
+
|
247
|
+
# account.save
|
248
|
+
# account.aggregate_snapshot!
|
249
|
+
# account.save_snapshot
|
250
|
+
|
251
|
+
# loaded = BankAccount.find account.aggregate_id
|
252
|
+
|
253
|
+
# expect(loaded.balance).to eql(account.balance)
|
254
|
+
# expect(loaded.account_number).to eql(account.account_number)
|
255
|
+
# expect(loaded.current_interest_info).to eql(account.current_interest_info)
|
256
|
+
# expect(loaded.account_creation_date).to eql(account.account_creation_date)
|
257
|
+
# expect(loaded.unpaid_interest_balance).to eql(account.unpaid_interest_balance)
|
258
|
+
# expect(loaded.last_interest_calculation).to eql(account.last_interest_calculation)
|
259
|
+
# expect(loaded.aggregate_id).to eql(account.aggregate_id)
|
260
|
+
# expect(loaded.aggregate_originating_version).to eql(account.aggregate_originating_version)
|
261
|
+
|
262
|
+
# end
|
263
|
+
# end
|