-
Notifications
You must be signed in to change notification settings - Fork 6
/
event_inspector_spec.rb
152 lines (139 loc) · 5.55 KB
/
event_inspector_spec.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
require 'spec_helper'
require 'sandthorn/event_inspector'
require 'sandthorn/aggregate_root_dirty_hashy'
class InspectorAggregate
include Sandthorn::AggregateRoot::DirtyHashy
attr_reader :foo_bar
def initialize args = {}
@foo_bar = args.fetch(:foo_bar, nil)
end
def this_is_an_event args = nil
record_event args
end
def another_event
record_event
end
def new_damaged_item_was_added hello
record_event hello
end
end
module Sandthorn
describe EventInspector do
let(:aggregate) {InspectorAggregate.new.extend EventInspector}
context "when using extract_trace_info from an event" do
let(:trace_info) {{user_id: "foo", ip: "bar"}}
let(:subject) do
aggregate.aggregate_trace trace_info do |traced|
traced.this_is_an_event
end
aggregate
end
context "and unsaved aggregate" do
it "should extract exact traceinfo from event" do
all_trace = subject.events_with_trace_info
all_trace.last[:trace].should eql trace_info
end
end
context "and saved aggregate" do
it "should extract exact traceinfo from event" do
subject.save
all_trace = subject.events_with_trace_info
all_trace.last[:trace].should eql trace_info
end
end
end
context "when inspecting non saved events" do
context "with no tracing information" do
let(:subject) { aggregate.this_is_an_event;aggregate }
it "should have the new event" do
subject.has_event?(:new).should be_true
end
it "should report true on has_unsaved_event? :this_is_an_event" do
subject.has_unsaved_event?(:this_is_an_event).should be_true
end
it "should report false on has_unsaved_event? :no_event_here" do
subject.has_unsaved_event?(:no_event_here).should be_false
end
end
context "with tracing information" do
let(:subject) do
aggregate.aggregate_trace user_id: 123, ip: "1234" do |traced|
traced.this_is_an_event "my name"
end
aggregate
end
it "should report true on has_unsaved_event? :this_is_an_event" do
subject.has_unsaved_event?(:this_is_an_event, trace: {user_id: 123, ip: "1234"}).should be_true
end
it "should report false on has_unsaved_event? :no_event_here" do
subject.has_unsaved_event?(:this_is_an_event, trace: {user_id: 321}).should be_false
subject.has_unsaved_event?(:this_is_an_event, trace: {another_user_id: 123}).should be_false
end
end
end
context "when inspecting saved events" do
context "with no tracing information" do
let(:subject) { aggregate.this_is_an_event;aggregate.save;aggregate }
it "should report true on has_unsaved_event? :this_is_an_event" do
subject.has_saved_event?(:this_is_an_event).should be_true
end
it "should report false on has_unsaved_event? :no_event_here" do
subject.has_saved_event?(:no_event_here).should be_false
end
end
context "with tracing information" do
let(:subject) do
aggregate.aggregate_trace user_id: 123, ip: "1234" do |traced|
traced.this_is_an_event "my name"
end
aggregate.save
aggregate
end
it "should report true on has_unsaved_event? :this_is_an_event" do
subject.has_saved_event?(:this_is_an_event, trace: {user_id: 123, ip: "1234"}).should be_true
end
it "should report false on has_unsaved_event? :no_event_here" do
subject.has_saved_event?(:this_is_an_event, trace: {user_id: 321}).should be_false
subject.has_saved_event?(:this_is_an_event, trace: {another_user_id: 123}).should be_false
end
it "should be able to check complex trace" do
subject.aggregate_trace client_ip: "10", user_id: "123" do |trace|
trace.new_damaged_item_was_added "foobar"
trace.save
end
subject.has_saved_event?(:new_damaged_item_was_added, trace: {user_id: "123", client_ip: "10"})
end
end
end
context "when inspecting any events" do
context "with no tracing information" do
let(:subject) { aggregate.this_is_an_event;aggregate.save;aggregate.another_event;aggregate }
it "should report true on has_unsaved_event? :this_is_an_event" do
subject.has_event?(:this_is_an_event).should be_true
subject.has_event?(:another_event).should be_true
end
it "should report false on has_unsaved_event? :no_event_here" do
subject.has_event?(:no_event_here).should be_false
end
end
context "with tracing information" do
let(:subject) do
aggregate.aggregate_trace user_id: 123, ip: "1234" do |traced|
traced.this_is_an_event "my name"
traced.save
traced.another_event
end
aggregate
end
it "should report true on has_unsaved_event? :this_is_an_event" do
subject.has_event?(:this_is_an_event, trace: {user_id: 123, ip: "1234"}).should be_true
subject.has_event?(:another_event, trace: {user_id: 123, ip: "1234"}).should be_true
end
it "should report false on has_unsaved_event? :no_event_here" do
subject.has_event?(:this_is_an_event, trace: {user_id: 321}).should be_false
subject.has_event?(:another_event, trace: {ip: "123"}).should be_false
end
end
end
end
end