sandthorn_driver_sequel 2.1.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,8 +32,6 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "autotest-standalone"
33
33
  spec.add_development_dependency "uuidtools"
34
34
  spec.add_development_dependency "ruby-beautify"
35
- spec.add_development_dependency "msgpack"
36
- spec.add_development_dependency "snappy"
37
35
  spec.add_development_dependency "guard-rspec"
38
36
 
39
37
  spec.add_runtime_dependency "sequel", "~> 4.17"
@@ -7,25 +7,27 @@ module SandthornDriverSequel
7
7
 
8
8
  describe EventStore do
9
9
  before(:each) { prepare_for_test }
10
+ let(:aggregate) { Struct::AggregateMock.new(aggregate_id, 11) }
11
+ let(:aggregate_id) { @id ||= UUIDTools::UUID.random_create.to_s }
10
12
  context "when asking for aggregates to snapshot" do
11
13
  let(:aggregates) {
12
- [{id: "1", class_name: Foo}, {id: "2", class_name: Bar},{id: "3", class_name: Foo}]}
14
+ [{id: "1", class_name: Foo}, {id: aggregate_id, class_name: Struct::AggregateMock},{id: "3", class_name: Foo}]}
13
15
 
14
16
  before(:each) {save_test_events}
15
17
 
16
18
  context "when asking for type 'Bar' and max event count 5" do
17
- let(:needs_snapshot) { event_store.obsolete_snapshots aggregate_types: [Bar], max_event_distance: 5 }
19
+ let(:needs_snapshot) { event_store.obsolete_snapshots aggregate_types: [Struct::AggregateMock], max_event_distance: 5 }
18
20
  context "and no snapshots exist" do
19
- it "should return that id 2 with class Bar need to be snapshotted" do
21
+ it "should return that id 2 with class Struct::AggregateMock need to be snapshotted" do
20
22
  expect(needs_snapshot.length).to eql 1
21
23
  expect(needs_snapshot.first[:aggregate_id]).to eql aggregates[1][:id]
22
- expect(needs_snapshot.first[:aggregate_type]).to eql "Bar"
24
+ expect(needs_snapshot.first[:aggregate_type]).to eql "Struct::AggregateMock"
23
25
  end
24
26
  end
25
27
  context "and a recent snapshot exists" do
26
28
  before(:each) do
27
29
  snapshot_data = { event_data: "YO MAN", aggregate_version: 11 }
28
- event_store.save_snapshot(snapshot_data, aggregates[1][:id])
30
+ event_store.save_snapshot(aggregate)
29
31
  end
30
32
  it "should return nil" do
31
33
  expect(needs_snapshot).to be_empty
@@ -54,7 +56,7 @@ module SandthornDriverSequel
54
56
  events = []
55
57
  i = 0
56
58
  while i < count do
57
- events << { aggregate_version: i+start_at, event_data: nil, event_name: "event_foo_#{i}" }
59
+ events << { aggregate_version: i+start_at, event_args: nil, event_name: "event_foo_#{i}" }
58
60
  i += 1
59
61
  end
60
62
  events
@@ -10,20 +10,20 @@ module Sandthorn
10
10
  before(:each) { prepare_for_test }
11
11
  let(:test_events_20_events) do
12
12
  e = []
13
- e << {aggregate_version: 1, event_name: "new", event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
13
+ e << {aggregate_version: 1, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
14
14
  19.times do |i|
15
- e << {aggregate_version: i+2, event_name: "foo", event_data: "A2"}
15
+ e << {aggregate_version: i+2, event_name: "foo", event_args: "A2"}
16
16
  end
17
17
  e
18
18
  end
19
19
  let(:test_events_one_event) do
20
20
  e = []
21
- e << {aggregate_version: 1, event_name: "new", event_data: "B1" }
21
+ e << {aggregate_version: 1, event_name: "new", event_args: "B1" }
22
22
  end
23
23
  let(:test_events_two_events) do
24
24
  e = []
25
- e << {aggregate_version: 1, event_name: "new", event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
26
- e << {aggregate_version: 2, event_name: "foo", event_data: "A2"}
25
+ e << {aggregate_version: 1, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
26
+ e << {aggregate_version: 2, event_name: "foo", event_args: "A2"}
27
27
  end
28
28
  let(:aggregate_id) {"c0456e26-2345-4f67-92fa-130b3a31a39a"}
29
29
  let(:es) { event_store }
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ module SandthornDriverSequel
4
+ describe "Configuration" do
5
+
6
+ context "default configuration" do
7
+
8
+ let(:driver) { SandthornDriverSequel.driver_from_connection connection: Sequel.sqlite }
9
+
10
+ it "should respond_to save_events method" do
11
+ expect(driver.respond_to?(:save_events)).to be_truthy
12
+ end
13
+
14
+ it "should have the default event_serializer" do
15
+ expect(driver.instance_variable_get "@event_serializer".to_sym).to be_a Proc
16
+ end
17
+
18
+ it "should have the default event_deserializer" do
19
+ expect(driver.instance_variable_get "@event_deserializer".to_sym).to be_a Proc
20
+ end
21
+
22
+ it "should have the default snapshot_serializer" do
23
+ expect(driver.instance_variable_get "@snapshot_serializer".to_sym).to be_a Proc
24
+ end
25
+
26
+ it "should have the default snapshot_deserializer" do
27
+ expect(driver.instance_variable_get "@snapshot_deserializer".to_sym).to be_a Proc
28
+ end
29
+
30
+ context "change global configuration" do
31
+ before do
32
+ SandthornDriverSequel.configure { |conf|
33
+ conf.event_serializer = :serializer_event_global
34
+ conf.event_deserializer = :deserializer_event_global
35
+ conf.snapshot_serializer = :serializer_snapshot_global
36
+ conf.snapshot_deserializer = :deserializer_snapshot_global
37
+ }
38
+ end
39
+
40
+ after do
41
+ #Set the default configuration after test
42
+ SandthornDriverSequel.configure { |conf|
43
+ conf.event_serializer = -> (data) { YAML.dump(data) }
44
+ conf.event_deserializer = -> (data) { YAML.load(data) }
45
+ conf.snapshot_serializer = -> (data) { YAML.dump(data) }
46
+ conf.snapshot_deserializer = -> (data) { YAML.load(data) }
47
+ }
48
+ end
49
+
50
+ it "should have the new event_serializer" do
51
+ expect(driver.instance_variable_get "@event_serializer".to_sym).to eql :serializer_event_global
52
+ end
53
+
54
+ it "should have the default event_deserializer" do
55
+ expect(driver.instance_variable_get "@event_deserializer".to_sym).to eql :deserializer_event_global
56
+ end
57
+
58
+ it "should have the new snapshot_serializer" do
59
+ expect(driver.instance_variable_get "@snapshot_serializer".to_sym).to eql :serializer_snapshot_global
60
+ end
61
+
62
+ it "should have the default snapshot_deserializer" do
63
+ expect(driver.instance_variable_get "@snapshot_deserializer".to_sym).to eql :deserializer_snapshot_global
64
+ end
65
+ end
66
+ end
67
+
68
+ context "session configuration" do
69
+ let(:driver) do
70
+ SandthornDriverSequel.driver_from_connection(connection: Sequel.sqlite) { |conf|
71
+ conf.event_serializer = :event_serializer
72
+ conf.event_deserializer = :event_deserializer
73
+ conf.snapshot_serializer = :snapshot_serializer
74
+ conf.snapshot_deserializer = :snapshot_deserializer
75
+ }
76
+ end
77
+
78
+ it "should respond_to save_events method" do
79
+ expect(driver.respond_to?(:save_events)).to be_truthy
80
+ end
81
+
82
+ it "should have a configuration event_serializer" do
83
+ expect(driver.instance_variable_get "@event_serializer".to_sym).to eql :event_serializer
84
+ end
85
+
86
+ it "should have a configuration event_deserializer" do
87
+ expect(driver.instance_variable_get "@event_deserializer".to_sym).to eql :event_deserializer
88
+ end
89
+
90
+ it "should have a configuration snapshot_serializer" do
91
+ expect(driver.instance_variable_get "@snapshot_serializer".to_sym).to eql :snapshot_serializer
92
+ end
93
+
94
+ it "should have a configuration snapshot_deserializer" do
95
+ expect(driver.instance_variable_get "@snapshot_deserializer".to_sym).to eql :snapshot_deserializer
96
+ end
97
+
98
+ end
99
+
100
+
101
+ end
102
+ end
@@ -15,7 +15,6 @@ module SandthornDriverSequel
15
15
  :get_all_types,
16
16
  :get_snapshot,
17
17
  :get_events,
18
- :url,
19
18
  :context,
20
19
  :driver
21
20
  ]
@@ -12,23 +12,28 @@ module SandthornDriverSequel
12
12
  let(:db) { Sequel.connect(event_store_url)}
13
13
  let(:aggregate_id) { SecureRandom.uuid }
14
14
  let(:aggregate) do
15
- aggregate_access.register_aggregate(aggregate_id, "foo")
15
+ aggregate_access.register_aggregate(aggregate_id, "AggregateMock")
16
16
  end
17
17
  let(:storage) { Storage.new(db, :test) }
18
+ let(:event_serializer) { -> (data) { YAML.dump(data) } }
19
+ let(:event_deserializer) { -> (data) { YAML.load(data) } }
20
+ let(:snapshot_serializer) { -> (data) { YAML.dump(data) } }
21
+ let(:snapshot_deserializer) { -> (data) { YAML.load(data) } }
18
22
  let(:aggregate_access) { AggregateAccess.new(storage) }
19
- let(:snapshot_access) { SnapshotAccess.new(storage)}
20
- let(:access) { EventAccess.new(storage) }
23
+ let(:snapshot_access) { SnapshotAccess.new(storage, snapshot_serializer, snapshot_deserializer)}
24
+ let(:access) { EventAccess.new(storage, event_serializer, event_deserializer) }
25
+
21
26
 
22
27
  let(:events) do
23
28
  [
24
29
  {
25
30
  aggregate_version: 1,
26
31
  event_name: "new",
27
- event_data: "new_data"
32
+ event_args: "new_data"
28
33
  },{
29
34
  aggregate_version: 2,
30
35
  event_name: "foo",
31
- event_data: "foo_data"
36
+ event_args: "foo_data"
32
37
  }
33
38
  ]
34
39
  end
@@ -87,14 +92,16 @@ module SandthornDriverSequel
87
92
  it "returns events after the given snapshot" do
88
93
  access.store_events(aggregate, events.first)
89
94
 
90
- snapshot_id = snapshot_access.record_snapshot(aggregate.aggregate_id, { aggregate_version: 1, event_data: "foo"})
95
+ aggregate_struct = Struct::AggregateMock.new aggregate_id, events.first[:aggregate_version]
96
+
97
+ snapshot_id = snapshot_access.record_snapshot(aggregate_struct)
91
98
  snapshot = snapshot_access.find(snapshot_id)
92
99
 
93
100
  access.store_events(aggregate, events.last)
94
-
101
+
95
102
  events = access.after_snapshot(snapshot)
96
103
  expect(events.count).to eq(1)
97
- expect(events.first[:event_data]).to eq("foo_data")
104
+ expect(events.first[:event_args]).to eq("foo_data")
98
105
  end
99
106
  end
100
107
 
@@ -3,14 +3,14 @@ module SandthornDriverSequel
3
3
  describe EventStore do
4
4
  let(:context) { :event_store_spec }
5
5
  before(:each) { prepare_for_test context: context; prepare_for_test context: nil; }
6
- let(:event_store_with_context) { EventStore.new url: event_store_url, context: context }
7
- let(:event_store_without_context) { EventStore.new url: event_store_url }
6
+ let(:event_store_with_context) { SandthornDriverSequel.driver_from_url url: event_store_url, context: context }
7
+ let(:event_store_without_context) { SandthornDriverSequel.driver_from_url url: event_store_url }
8
8
  context("when saving in one context and retrieving in another") do
9
9
  let(:test_events) do
10
10
  e = []
11
- e << {aggregate_version: 1, event_name: "new", event_args: nil, event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
12
- e << {aggregate_version: 2, event_name: "foo", event_args: ["bar"], event_data: "noop"}
13
- e << {aggregate_version: 3, event_name: "flubber", event_args: ["bar"] , event_data: "noop"}
11
+ e << {aggregate_version: 1, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
12
+ e << {aggregate_version: 2, event_name: "foo", event_args: "noop"}
13
+ e << {aggregate_version: 3, event_name: "flubber", event_args: "noop"}
14
14
  end
15
15
  let(:aggregate_id) {"c0456e26-e29a-4f67-92fa-130b3a31a39b"}
16
16
  it "should not find them" do
@@ -5,25 +5,25 @@ module SandthornDriverSequel
5
5
  before(:each) { prepare_for_test }
6
6
  let(:test_events_a) do
7
7
  e = []
8
- e << {aggregate_version: 1, event_name: "new", event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
9
- e << {aggregate_version: 2, event_name: "foo", event_data: "A2"}
10
- e << {aggregate_version: 3, event_name: "bard", event_data: "A3"}
8
+ e << {aggregate_version: 1, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
9
+ e << {aggregate_version: 2, event_name: "foo", event_args: "A2"}
10
+ e << {aggregate_version: 3, event_name: "bard", event_args: "A3"}
11
11
  end
12
12
  let(:aggregate_id_a) {"c0456e26-e29a-4f67-92fa-130b3a31a39a"}
13
13
  let(:test_events_b) do
14
14
  e = []
15
- e << {aggregate_version: 1, event_name: "new", event_data: "B1" }
16
- e << {aggregate_version: 2, event_name: "foo", event_data: "B2"}
17
- e << {aggregate_version: 3, event_name: "bar", event_data: "B3"}
15
+ e << {aggregate_version: 1, event_name: "new", event_args: "B1" }
16
+ e << {aggregate_version: 2, event_name: "foo", event_args: "B2"}
17
+ e << {aggregate_version: 3, event_name: "bar", event_args: "B3"}
18
18
  end
19
19
  let(:aggregate_id_b) {"c0456e26-1234-4f67-92fa-130b3a31a39a"}
20
20
  let(:test_events_c) do
21
21
  e = []
22
- e << {aggregate_version: 1, event_name: "new", event_data: "C1" }
22
+ e << {aggregate_version: 1, event_name: "new", event_args: "C1" }
23
23
  end
24
24
  let(:test_events_c_2) do
25
25
  e = []
26
- e << {aggregate_version: 2, event_name: "flubber", event_data: "C2" }
26
+ e << {aggregate_version: 2, event_name: "flubber", event_args: "C2" }
27
27
  end
28
28
  let(:aggregate_id_c) {"c0456e26-2345-4f67-92fa-130b3a31a39a"}
29
29
  before(:each) do
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'msgpack'
3
2
 
4
3
  module SandthornDriverSequel
5
4
  describe EventStore do
@@ -7,9 +6,9 @@ module SandthornDriverSequel
7
6
  context "when saving a prefectly sane event stream" do
8
7
  let(:test_events) do
9
8
  e = []
10
- e << {aggregate_version: 1, event_name: "new", event_args: nil, event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
11
- e << {aggregate_version: 2, event_name: "foo", event_args: ["bar"], event_data: "noop"}
12
- e << {aggregate_version: 3, event_name: "flubber", event_args: ["bar"] , event_data: "noop"}
9
+ e << {aggregate_version: 1, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
10
+ e << {aggregate_version: 2, event_name: "foo", event_args: "noop"}
11
+ e << {aggregate_version: 3, event_name: "flubber", event_args: "noop"}
13
12
  end
14
13
 
15
14
  let(:aggregate_id) { "c0456e26-e29a-4f67-92fa-130b3a31a39a" }
@@ -24,7 +23,7 @@ module SandthornDriverSequel
24
23
  event_store.save_events test_events, aggregate_id, String
25
24
  events = event_store.get_aggregate aggregate_id, String
26
25
  event = events.first
27
- expect(event[:event_data]).to eql(test_events.first[:event_data])
26
+ expect(event[:event_args]).to eql(test_events.first[:event_args])
28
27
  expect(event[:event_name]).to eql("new")
29
28
  expect(event[:aggregate_id]).to eql aggregate_id
30
29
  expect(event[:aggregate_version]).to eql 1
@@ -36,13 +35,13 @@ module SandthornDriverSequel
36
35
  context "when saving two aggregate types" do
37
36
  let(:test_events_1) do
38
37
  e = []
39
- e << {aggregate_version: 1, event_name: "new", event_args: nil, event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
40
- e << {aggregate_version: 2, event_name: "foo", event_args: "bar", event_data: "noop"}
41
- e << {aggregate_version: 3, event_name: "flubber", event_args: "bar", event_data: "noop"}
38
+ e << {aggregate_version: 1, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
39
+ e << {aggregate_version: 2, event_name: "foo", event_args: "noop"}
40
+ e << {aggregate_version: 3, event_name: "flubber", event_args: "noop"}
42
41
  end
43
42
  let(:test_events_2) do
44
43
  e = []
45
- e << {aggregate_version: 1, event_name: "new", event_args: nil, event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
44
+ e << {aggregate_version: 1, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
46
45
  end
47
46
  let(:aggregate_id_1) {"c0456e26-e29a-4f67-92fa-130b3a31a39a"}
48
47
  let(:aggregate_id_2) {"c0456e26-e92b-4f67-92fa-130b3a31b93b"}
@@ -69,29 +68,12 @@ module SandthornDriverSequel
69
68
  end
70
69
  end
71
70
 
72
- context "when saving events that are serilized with msgpack" do
73
- let(:test_data) { {name: "test", hash: {t: 123}} }
74
- let(:test_events) do
75
- e = []
76
- data = MessagePack.pack(test_data, symbolize_keys: true)
77
- e << {aggregate_version: 1, event_name: "new", event_args: nil, event_data: data}
78
- end
79
- let(:aggregate_id_1) {"c0456e26-e29a-4f67-92fa-130b3a31a39a"}
80
-
81
- it "should save and get events that are serialized with msgpack" do
82
- event_store.save_events test_events, aggregate_id_1, String
83
- events = event_store.get_aggregate aggregate_id_1, String
84
- expect(MessagePack.unpack(events.first[:event_data], symbolize_keys: true)).to eql test_data
85
- end
86
-
87
- end
88
-
89
71
  context "when saving events that have no aggregate_version" do
90
72
  let(:test_events) do
91
73
  e = []
92
- e << {aggregate_version: nil, event_name: "new", event_args: nil, event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
93
- e << {aggregate_version: nil, event_name: "foo", event_args: ["bar"], event_data: "noop"}
94
- e << {aggregate_version: nil, event_name: "flubber", event_args: ["bar"] , event_data: "noop"}
74
+ e << {aggregate_version: nil, event_name: "new", event_args: {:method_name=>"new", :method_args=>[], :attribute_deltas=>[{:attribute_name=>"aggregate_id", :old_value=>nil, :new_value=>"0a74e545-be84-4506-8b0a-73e947856327"}]}}
75
+ e << {aggregate_version: nil, event_name: "foo", event_args: "noop"}
76
+ e << {aggregate_version: nil, event_name: "flubber", event_args: "noop"}
95
77
  end
96
78
 
97
79
  let(:aggregate_id) { "c0456e26-e29a-4f67-92fa-130b3a31a39a" }
@@ -106,7 +88,8 @@ module SandthornDriverSequel
106
88
  event_store.save_events test_events, aggregate_id, String
107
89
  events = event_store.get_aggregate aggregate_id, String
108
90
  event = events.first
109
- expect(event[:event_data]).to eql(test_events.first[:event_data])
91
+
92
+ expect(event[:event_args]).to eql(test_events.first[:event_args])
110
93
  expect(event[:event_name]).to eql("new")
111
94
  expect(event[:aggregate_id]).to eql aggregate_id
112
95
  expect(event[:aggregate_version]).to eql 1
@@ -5,12 +5,12 @@ module SandthornDriverSequel
5
5
  describe EventStore do
6
6
  before(:each) { prepare_for_test }
7
7
  let(:aggregate_id) { @id ||= UUIDTools::UUID.random_create.to_s }
8
- let(:test_events) { [{aggregate_version: 1, event_data: nil, event_name: "new"},{aggregate_version: 2, event_data: nil, event_name: "foo"}] }
9
- let(:additional_events) { [{aggregate_version: 3, event_data: nil, event_name: "klopp"},{aggregate_version: 4, event_data: nil, event_name: "flipp"}] }
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 }
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 }
8
+ let(:test_events) { [{aggregate_version: 1, event_args: nil, event_name: "new"},{aggregate_version: 2, event_args: nil, event_name: "foo"}] }
9
+ let(:additional_events) { [{aggregate_version: 3, event_args: nil, event_name: "klopp"},{aggregate_version: 4, event_args: nil, event_name: "flipp"}] }
10
+ let(:aggregate) { Struct::AggregateMock.new aggregate_id, 2 }
11
+ let(:save_snapshot) { event_store.save_snapshot aggregate }
12
+ let(:save_events) { event_store.save_events test_events, aggregate_id, Struct::AggregateMock }
13
+ let(:save_additional_events) { event_store.save_events additional_events, aggregate_id, Struct::AggregateMock }
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
@@ -18,9 +18,9 @@ module SandthornDriverSequel
18
18
  save_snapshot
19
19
  save_additional_events
20
20
  end
21
- let(:events) { event_store.get_aggregate aggregate_id, SandthornDriverSequel::EventStore }
21
+ let(:events) { event_store.get_aggregate aggregate_id, Struct::AggregateMock }
22
22
  it "should have the first event as :aggregate_set_from_snapshot" do
23
- expect(events.first[:event_name]).to eql "aggregate_set_from_snapshot"
23
+ expect(events.first[:aggregate]).to eql aggregate
24
24
  end
25
25
  it "should have additional events after first snapshot-event" do
26
26
  expect(events.length).to eql 1+additional_events.length
@@ -40,7 +40,7 @@ module SandthornDriverSequel
40
40
  it "should be able to save and get snapshot" do
41
41
  save_snapshot
42
42
  snap = event_store.get_snapshot(aggregate_id)
43
- expect(snap).to eql snapshot_data
43
+ expect(snap).to eql aggregate
44
44
  end
45
45
  end
46
46
  context "when trying to save a snapshot on a non-existing aggregate" do
@@ -48,14 +48,7 @@ module SandthornDriverSequel
48
48
  expect { save_snapshot }.to raise_error SandthornDriverSequel::Errors::NoAggregateError
49
49
  end
50
50
  end
51
- context "when trying to save a snapshot with a non-existing aggregate_version" do
52
- before(:each) { save_events }
53
- it "should raise a WrongAggregateVersion error" do
54
- data = snapshot_data
55
- data[:aggregate_version] = 100
56
- expect { event_store.save_snapshot data, aggregate_id }.to raise_error SandthornDriverSequel::Errors::WrongSnapshotVersionError
57
- end
58
- end
51
+
59
52
  context "when saving a snapshot twice" do
60
53
  before(:each) { save_events; save_snapshot }
61
54
  it "should not raise error" do
@@ -65,9 +58,8 @@ module SandthornDriverSequel
65
58
  context "when saving a snapshot on a version less than current version" do
66
59
  before(:each) { save_events; }
67
60
  it "should save without protesting" do
68
- data = snapshot_data
69
- data[:aggregate_version] = 1
70
- event_store.save_snapshot(data, aggregate_id)
61
+ data = Struct::AggregateMock.new aggregate_id, 1
62
+ event_store.save_snapshot(data)
71
63
  snap = event_store.get_snapshot(aggregate_id)
72
64
  expect(snap).to eql data
73
65
  end