sandthorn_driver_sequel 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/Guardfile +7 -0
  4. data/lib/sandthorn_driver_sequel/access/aggregate_access.rb +50 -0
  5. data/lib/sandthorn_driver_sequel/access/event_access.rb +81 -0
  6. data/lib/sandthorn_driver_sequel/access/snapshot_access.rb +87 -0
  7. data/lib/sandthorn_driver_sequel/access.rb +20 -0
  8. data/lib/sandthorn_driver_sequel/errors.rb +47 -5
  9. data/lib/sandthorn_driver_sequel/event_query.rb +90 -0
  10. data/lib/sandthorn_driver_sequel/event_store.rb +90 -153
  11. data/lib/sandthorn_driver_sequel/event_store_context.rb +1 -0
  12. data/lib/sandthorn_driver_sequel/migration.rb +9 -1
  13. data/lib/sandthorn_driver_sequel/old_event_store.rb +228 -0
  14. data/lib/sandthorn_driver_sequel/sequel_driver.rb +8 -25
  15. data/lib/sandthorn_driver_sequel/storage.rb +46 -0
  16. data/lib/sandthorn_driver_sequel/utilities/array.rb +13 -0
  17. data/lib/sandthorn_driver_sequel/utilities.rb +1 -0
  18. data/lib/sandthorn_driver_sequel/version.rb +1 -1
  19. data/lib/sandthorn_driver_sequel/wrappers/event_wrapper.rb +12 -0
  20. data/lib/sandthorn_driver_sequel/wrappers/snapshot_wrapper.rb +11 -0
  21. data/lib/sandthorn_driver_sequel/wrappers.rb +2 -0
  22. data/lib/sandthorn_driver_sequel.rb +5 -0
  23. data/sandthorn_driver_sequel.gemspec +2 -2
  24. data/spec/aggregate_access_spec.rb +97 -0
  25. data/spec/asking_for_aggregates_to_snapshot_spec.rb +7 -4
  26. data/spec/driver_interface_spec.rb +23 -40
  27. data/spec/event_access_spec.rb +96 -0
  28. data/spec/event_store_with_context_spec.rb +4 -4
  29. data/spec/get_events_spec.rb +20 -13
  30. data/spec/migration_specifying_domain_spec.rb +10 -10
  31. data/spec/saving_events_spec.rb +42 -39
  32. data/spec/saving_snapshot_spec.rb +7 -7
  33. data/spec/snapshot_access_spec.rb +119 -0
  34. data/spec/spec_helper.rb +0 -4
  35. data/spec/storage_spec.rb +66 -0
  36. metadata +39 -18
@@ -8,9 +8,9 @@ module SandthornDriverSequel
8
8
  let(:test_events) { [{aggregate_version: 1, event_data: nil, event_name: "new"},{aggregate_version: 2, event_data: nil, event_name: "foo"}] }
9
9
  let(:additional_events) { [{aggregate_version: 3, event_data: nil, event_name: "klopp"},{aggregate_version: 4, event_data: nil, event_name: "flipp"}] }
10
10
  let(:snapshot_data) { { event_data: YAML.dump(Object.new), aggregate_version: 2 } }
11
- let(:save_snapshot) {event_store.save_snapshot snapshot_data, aggregate_id, SandthornDriverSequel::EventStore}
12
- let(:save_events) {event_store.save_events test_events, 0, aggregate_id, SandthornDriverSequel::EventStore}
13
- let(:save_additional_events) {event_store.save_events additional_events, 2, aggregate_id, SandthornDriverSequel::EventStore}
11
+ let(:save_snapshot) { event_store.save_snapshot snapshot_data, aggregate_id }
12
+ let(:save_events) { event_store.save_events test_events, aggregate_id, SandthornDriverSequel::EventStore }
13
+ let(:save_additional_events) { event_store.save_events additional_events, aggregate_id, SandthornDriverSequel::EventStore }
14
14
  context "when loading an aggregate using get_aggregate" do
15
15
  context "and it has a snapshot" do
16
16
  before(:each) do
@@ -39,7 +39,7 @@ module SandthornDriverSequel
39
39
  end
40
40
  it "should be able to save and get snapshot" do
41
41
  save_snapshot
42
- snap = event_store.get_snapshot aggregate_id, SandthornDriverSequel::EventStore
42
+ snap = event_store.get_snapshot(aggregate_id)
43
43
  snap.should eql snapshot_data
44
44
  end
45
45
  end
@@ -53,7 +53,7 @@ module SandthornDriverSequel
53
53
  it "should raise a WrongAggregateVersion error" do
54
54
  data = snapshot_data
55
55
  data[:aggregate_version] = 100
56
- expect {event_store.save_snapshot data, aggregate_id, SandthornDriverSequel::EventStore}.to raise_error SandthornDriverSequel::Errors::WrongAggregateVersionError
56
+ expect { event_store.save_snapshot data, aggregate_id }.to raise_error SandthornDriverSequel::Errors::WrongSnapshotVersionError
57
57
  end
58
58
  end
59
59
  context "when saving a snapshot twice" do
@@ -67,8 +67,8 @@ module SandthornDriverSequel
67
67
  it "should save without protesting" do
68
68
  data = snapshot_data
69
69
  data[:aggregate_version] = 1
70
- event_store.save_snapshot data, aggregate_id, SandthornDriverSequel::EventStore
71
- snap = event_store.get_snapshot aggregate_id, SandthornDriverSequel::EventStore
70
+ event_store.save_snapshot(data, aggregate_id)
71
+ snap = event_store.get_snapshot(aggregate_id)
72
72
  snap.should eql data
73
73
  end
74
74
  end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ module SandthornDriverSequel
4
+ describe SnapshotAccess do
5
+ include EventStoreContext
6
+
7
+ before do
8
+ prepare_for_test
9
+ end
10
+
11
+ let(:context) { :test }
12
+ let(:db) { Sequel.connect(event_store_url)}
13
+ let(:aggregate_id) { generate_uuid }
14
+ let(:storage) { Storage.new(db, :test) }
15
+ let(:aggregate_access) { AggregateAccess.new(storage) }
16
+ let(:event_access) { EventAccess.new(storage) }
17
+ let(:aggregate) { aggregate_access.register_aggregate(aggregate_id, "foo") }
18
+ let(:access) { SnapshotAccess.new(storage) }
19
+ let(:events) do
20
+ [
21
+ {
22
+ aggregate_version: 1,
23
+ event_name: "new",
24
+ event_data: "new_data"
25
+ },{
26
+ aggregate_version: 2,
27
+ event_name: "foo",
28
+ event_data: "foo_data"
29
+ }
30
+ ]
31
+ end
32
+
33
+ describe "#find_by_aggregate_id" do
34
+ it "returns the correct data" do
35
+ aggregate = aggregate_access.register_aggregate(aggregate_id, "foo")
36
+ access.record_snapshot(aggregate.aggregate_id, { aggregate_version: 0, event_data: "data" })
37
+ aggregate.update(aggregate_version: 1)
38
+ snapshot = access.find_by_aggregate_id(aggregate.aggregate_id)
39
+ expected = {
40
+ aggregate_table_id: aggregate.id,
41
+ aggregate_version: 0,
42
+ snapshot_data: "data",
43
+ id: snapshot.id
44
+ }
45
+ expect(snapshot.values).to eq(expected)
46
+ end
47
+ end
48
+
49
+ describe "#record" do
50
+ context "when the aggregate doesn't exist" do
51
+ it "raises an error" do
52
+ expect { access.record_snapshot("qux", "data") }.to raise_error(Errors::NoAggregateError)
53
+ end
54
+ end
55
+ context "when the aggregate exists" do
56
+ context "when no previous snapshot exists" do
57
+ it "records the snapshot" do
58
+ aggregate_table_id = aggregate_access.register_aggregate(aggregate_id, "foo").id
59
+ expect(access.find_by_aggregate_id(aggregate_id)).to be_nil
60
+ access.record_snapshot(aggregate_id, { aggregate_version: 0, event_data: "data"})
61
+
62
+ snapshot = access.find_by_aggregate_id(aggregate_id)
63
+ expect(snapshot).to_not be_nil
64
+ expect(snapshot.aggregate_table_id).to eq(aggregate_table_id)
65
+ expect(snapshot.snapshot_data).to eq("data")
66
+ expect(snapshot.aggregate_version).to eq(0)
67
+ end
68
+ end
69
+ context "when the snapshot isn't fresh" do
70
+ context "when the versions match" do
71
+ it "records a new snapshot" do
72
+ aggregate = aggregate_access.register_aggregate(aggregate_id, "foo")
73
+ expect(access.find_by_aggregate_id(aggregate_id)).to be_nil
74
+ access.record_snapshot(aggregate_id, { aggregate_version: 0, event_data: "data"})
75
+ event_access.store_events(aggregate, events)
76
+ access.record_snapshot(aggregate_id, { aggregate_version: 2, event_data: "other_data"})
77
+
78
+ snapshot = access.find_by_aggregate_id(aggregate_id)
79
+ expect(snapshot).to_not be_nil
80
+ expect(snapshot.aggregate_table_id).to eq(aggregate.id)
81
+ expect(snapshot.snapshot_data).to eq("other_data")
82
+ expect(snapshot.aggregate_version).to eq(2)
83
+ end
84
+ end
85
+
86
+ context "when the versions don't match" do
87
+ it "raises an error" do
88
+ aggregate = aggregate_access.register_aggregate(aggregate_id, "foo")
89
+ aggregate.update(aggregate_version: 10)
90
+ expect { access.record_snapshot(aggregate_id, snapshot_data) }.to raise_error
91
+ end
92
+ end
93
+ end
94
+ context "when the snapshot is fresh" do
95
+ it "doesn't record a snapshot" do
96
+ aggregate = aggregate_access.register_aggregate(aggregate_id, "foo")
97
+ expect(access.find_by_aggregate_id(aggregate_id)).to be_nil
98
+ access.record_snapshot(aggregate_id, { aggregate_version: 0, event_data: "data"})
99
+ access.record_snapshot(aggregate_id, { aggregate_version: 0, event_data: "new_data"})
100
+ snapshot = access.find_by_aggregate_id(aggregate_id)
101
+ expect(snapshot.snapshot_data).to eq("data")
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ it "can write and read snapshots" do
108
+ snapshot_id = access.record_snapshot(aggregate.aggregate_id, { aggregate_version: 0, event_data: "data" })
109
+ snapshot = access.find(snapshot_id)
110
+ expect(snapshot).to_not be_nil
111
+ expect(snapshot.snapshot_data).to eq("data")
112
+ expect(snapshot)
113
+ end
114
+
115
+ def generate_uuid
116
+ SecureRandom.uuid
117
+ end
118
+ end
119
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,8 @@
1
- require 'coveralls'
2
1
  require 'sandthorn_driver_sequel'
3
2
  require 'sandthorn_driver_sequel/migration'
4
3
  require 'ap'
5
4
  require 'uuidtools'
6
5
 
7
- Bundler.require
8
- Coveralls.wear!
9
6
  # This file was generated by the `rspec --init` command. Conventionally, all
10
7
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
11
8
  # Require this file using `require "spec_helper"` to ensure that it is only
@@ -13,7 +10,6 @@ Coveralls.wear!
13
10
  #
14
11
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
15
12
  RSpec.configure do |config|
16
- config.treat_symbols_as_metadata_keys_with_true_values = true
17
13
  config.run_all_when_everything_filtered = true
18
14
  config.filter_run :focus
19
15
  config.filter_run_excluding benchmark: true
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ module SandthornDriverSequel
4
+
5
+ describe Storage do
6
+ let(:context) { :test }
7
+ before do
8
+ prepare_for_test(context: context)
9
+ end
10
+ let(:db) { Sequel.connect(event_store_url) }
11
+ let(:driver) { SequelDriver.new(event_store_url)}
12
+ let(:storage) { Storage.new(db, context) }
13
+
14
+ describe "anonymous aggegrate class" do
15
+ it "can insert and read data" do
16
+ create_aggregate
17
+ aggregate = storage.aggregates.first(aggregate_id: "foo", aggregate_type: "Foo")
18
+ expect(aggregate).to_not be_nil
19
+ end
20
+
21
+ it "can update data" do
22
+ create_aggregate
23
+ storage.aggregates.where(aggregate_id: "foo").update(aggregate_version: 2)
24
+ aggregate = storage.aggregates.first(aggregate_id: "foo")
25
+ expect(aggregate.aggregate_version).to eq(2)
26
+ end
27
+ end
28
+
29
+ describe "anonymous event class" do
30
+ it "can insert and read data" do
31
+ data, event_id = create_event
32
+ event = storage.events.first(sequence_number: event_id).values
33
+ expected = data.merge(sequence_number: event_id)
34
+ expected_time = expected.delete(:timestamp)
35
+ actual_time = event.delete(:timestamp)
36
+ expect(event).to eq(expected)
37
+ expect(actual_time.to_i).to eq(actual_time.to_i)
38
+ end
39
+
40
+ it "can update data" do
41
+ data, event_id = create_event
42
+ storage.events.where(sequence_number: event_id).update(event_name: "qux")
43
+ event = storage.events.first(sequence_number: event_id)
44
+ expect(event.event_name).to eq("qux")
45
+ end
46
+ end
47
+
48
+ def create_aggregate
49
+ storage.aggregates.insert(aggregate_id: "foo", aggregate_type: "Foo")
50
+ end
51
+
52
+ def create_event
53
+ aggregate_table_id = create_aggregate
54
+ data = {
55
+ aggregate_table_id: aggregate_table_id,
56
+ aggregate_version: 1,
57
+ event_name: "foo",
58
+ event_data: "bar",
59
+ timestamp: Time.now.utc
60
+ }
61
+ event_id = storage.events.insert(data)
62
+ return data, event_id
63
+ end
64
+
65
+ end
66
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sandthorn_driver_sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Krantz
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-08 00:00:00.000000000 Z
12
+ date: 2015-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -54,7 +54,7 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: coveralls
57
+ name: gem-release
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
@@ -68,7 +68,7 @@ dependencies:
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
- name: gem-release
71
+ name: sqlite3
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - ">="
@@ -82,7 +82,7 @@ dependencies:
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  - !ruby/object:Gem::Dependency
85
- name: sqlite3
85
+ name: pry
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
88
  - - ">="
@@ -96,7 +96,7 @@ dependencies:
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  - !ruby/object:Gem::Dependency
99
- name: pry
99
+ name: pry-doc
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
102
  - - ">="
@@ -110,7 +110,7 @@ dependencies:
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  - !ruby/object:Gem::Dependency
113
- name: pry-doc
113
+ name: awesome_print
114
114
  requirement: !ruby/object:Gem::Requirement
115
115
  requirements:
116
116
  - - ">="
@@ -124,7 +124,7 @@ dependencies:
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
126
  - !ruby/object:Gem::Dependency
127
- name: awesome_print
127
+ name: autotest-standalone
128
128
  requirement: !ruby/object:Gem::Requirement
129
129
  requirements:
130
130
  - - ">="
@@ -138,7 +138,7 @@ dependencies:
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0'
140
140
  - !ruby/object:Gem::Dependency
141
- name: autotest-standalone
141
+ name: uuidtools
142
142
  requirement: !ruby/object:Gem::Requirement
143
143
  requirements:
144
144
  - - ">="
@@ -152,7 +152,7 @@ dependencies:
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
154
  - !ruby/object:Gem::Dependency
155
- name: uuidtools
155
+ name: ruby-beautify
156
156
  requirement: !ruby/object:Gem::Requirement
157
157
  requirements:
158
158
  - - ">="
@@ -166,7 +166,7 @@ dependencies:
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
168
  - !ruby/object:Gem::Dependency
169
- name: ruby-beautify
169
+ name: msgpack
170
170
  requirement: !ruby/object:Gem::Requirement
171
171
  requirements:
172
172
  - - ">="
@@ -180,7 +180,7 @@ dependencies:
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
182
  - !ruby/object:Gem::Dependency
183
- name: msgpack
183
+ name: snappy
184
184
  requirement: !ruby/object:Gem::Requirement
185
185
  requirements:
186
186
  - - ">="
@@ -194,7 +194,7 @@ dependencies:
194
194
  - !ruby/object:Gem::Version
195
195
  version: '0'
196
196
  - !ruby/object:Gem::Dependency
197
- name: snappy
197
+ name: guard-rspec
198
198
  requirement: !ruby/object:Gem::Requirement
199
199
  requirements:
200
200
  - - ">="
@@ -211,16 +211,16 @@ dependencies:
211
211
  name: sequel
212
212
  requirement: !ruby/object:Gem::Requirement
213
213
  requirements:
214
- - - ">="
214
+ - - "~>"
215
215
  - !ruby/object:Gem::Version
216
- version: '0'
216
+ version: '4.17'
217
217
  type: :runtime
218
218
  prerelease: false
219
219
  version_requirements: !ruby/object:Gem::Requirement
220
220
  requirements:
221
- - - ">="
221
+ - - "~>"
222
222
  - !ruby/object:Gem::Version
223
- version: '0'
223
+ version: '4.17'
224
224
  - !ruby/object:Gem::Dependency
225
225
  name: pg
226
226
  requirement: !ruby/object:Gem::Requirement
@@ -250,27 +250,44 @@ files:
250
250
  - ".ruby-version"
251
251
  - ".travis.yml"
252
252
  - Gemfile
253
+ - Guardfile
253
254
  - LICENSE.txt
254
255
  - README.md
255
256
  - Rakefile
256
257
  - lib/sandthorn_driver_sequel.rb
258
+ - lib/sandthorn_driver_sequel/access.rb
259
+ - lib/sandthorn_driver_sequel/access/aggregate_access.rb
260
+ - lib/sandthorn_driver_sequel/access/event_access.rb
261
+ - lib/sandthorn_driver_sequel/access/snapshot_access.rb
257
262
  - lib/sandthorn_driver_sequel/errors.rb
263
+ - lib/sandthorn_driver_sequel/event_query.rb
258
264
  - lib/sandthorn_driver_sequel/event_store.rb
259
265
  - lib/sandthorn_driver_sequel/event_store_context.rb
260
266
  - lib/sandthorn_driver_sequel/migration.rb
267
+ - lib/sandthorn_driver_sequel/old_event_store.rb
261
268
  - lib/sandthorn_driver_sequel/sequel_driver.rb
269
+ - lib/sandthorn_driver_sequel/storage.rb
270
+ - lib/sandthorn_driver_sequel/utilities.rb
271
+ - lib/sandthorn_driver_sequel/utilities/array.rb
262
272
  - lib/sandthorn_driver_sequel/version.rb
273
+ - lib/sandthorn_driver_sequel/wrappers.rb
274
+ - lib/sandthorn_driver_sequel/wrappers/event_wrapper.rb
275
+ - lib/sandthorn_driver_sequel/wrappers/snapshot_wrapper.rb
263
276
  - sandthorn_driver_sequel.gemspec
277
+ - spec/aggregate_access_spec.rb
264
278
  - spec/asking_for_aggregates_to_snapshot_spec.rb
265
279
  - spec/benchmark_spec.rb
266
280
  - spec/db/.keep
267
281
  - spec/driver_interface_spec.rb
282
+ - spec/event_access_spec.rb
268
283
  - spec/event_store_with_context_spec.rb
269
284
  - spec/get_events_spec.rb
270
285
  - spec/migration_specifying_domain_spec.rb
271
286
  - spec/saving_events_spec.rb
272
287
  - spec/saving_snapshot_spec.rb
288
+ - spec/snapshot_access_spec.rb
273
289
  - spec/spec_helper.rb
290
+ - spec/storage_spec.rb
274
291
  homepage: ''
275
292
  licenses:
276
293
  - MIT
@@ -291,19 +308,23 @@ required_rubygems_version: !ruby/object:Gem::Requirement
291
308
  version: '0'
292
309
  requirements: []
293
310
  rubyforge_project:
294
- rubygems_version: 2.2.2
311
+ rubygems_version: 2.4.3
295
312
  signing_key:
296
313
  specification_version: 4
297
314
  summary: sequel driver for sandthorn
298
315
  test_files:
316
+ - spec/aggregate_access_spec.rb
299
317
  - spec/asking_for_aggregates_to_snapshot_spec.rb
300
318
  - spec/benchmark_spec.rb
301
319
  - spec/db/.keep
302
320
  - spec/driver_interface_spec.rb
321
+ - spec/event_access_spec.rb
303
322
  - spec/event_store_with_context_spec.rb
304
323
  - spec/get_events_spec.rb
305
324
  - spec/migration_specifying_domain_spec.rb
306
325
  - spec/saving_events_spec.rb
307
326
  - spec/saving_snapshot_spec.rb
327
+ - spec/snapshot_access_spec.rb
308
328
  - spec/spec_helper.rb
329
+ - spec/storage_spec.rb
309
330
  has_rdoc: