forked from jetbasrawi/go.cqrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_test.go
104 lines (77 loc) · 2.34 KB
/
aggregate_test.go
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
// Copyright 2016 Jet Basrawi. All rights reserved.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package ycq
import . "gopkg.in/check.v1"
var _ = Suite(&AggregateBaseSuite{})
type AggregateBaseSuite struct{}
func (s *AggregateBaseSuite) TestNewAggregateBase(c *C) {
id := NewUUID()
agg := NewAggregateBase(id)
c.Assert(agg, NotNil)
c.Assert(agg.AggregateID(), Equals, id)
c.Assert(agg.OriginalVersion(), Equals, -1)
c.Assert(agg.CurrentVersion(), Equals, -1)
}
func (s *AggregateBaseSuite) TestIncrementVersion(c *C) {
agg := NewAggregateBase(NewUUID())
c.Assert(agg.CurrentVersion(), Equals, -1)
agg.IncrementVersion()
c.Assert(agg.CurrentVersion(), Equals, 0)
}
func (s *AggregateBaseSuite) TestTrackOneChange(c *C) {
ev := NewTestEventMessage(NewUUID())
agg := NewSomeAggregate(ev.AggregateID())
agg.TrackChange(ev)
c.Assert(agg.GetChanges(), DeepEquals, []EventMessage{ev})
}
func (s *AggregateBaseSuite) TestTrackMultipleChanges(c *C) {
agg := NewAggregateBase(NewUUID())
ev1 := NewTestEventMessage(agg.AggregateID())
ev2 := NewTestEventMessage(agg.AggregateID())
agg.TrackChange(ev1)
agg.TrackChange(ev2)
c.Assert(agg.GetChanges(), DeepEquals, []EventMessage{ev1, ev2})
}
func (s *AggregateBaseSuite) TestClearChanges(c *C) {
agg := NewAggregateBase(NewUUID())
ev := NewTestEventMessage(agg.AggregateID())
agg.TrackChange(ev)
c.Assert(agg.GetChanges(), DeepEquals, []EventMessage{ev})
agg.ClearChanges()
c.Assert(agg.GetChanges(), DeepEquals, []EventMessage{})
}
type SomeAggregate struct {
*AggregateBase
events []EventMessage
}
func NewSomeAggregate(id string) AggregateRoot {
return &SomeAggregate{
AggregateBase: NewAggregateBase(id),
}
}
func (t *SomeAggregate) Apply(event EventMessage, isNew bool) {
t.events = append(t.events, event)
}
func (t *SomeAggregate) Handle(command CommandMessage) error {
return nil
}
type SomeOtherAggregate struct {
*AggregateBase
changes []EventMessage
}
func NewSomeOtherAggregate(id string) AggregateRoot {
return &SomeOtherAggregate{
AggregateBase: NewAggregateBase(id),
}
}
//TODO: No tests for isNew
func (t *SomeOtherAggregate) Apply(event EventMessage, isNew bool) {
t.changes = append(t.changes, event)
}
func (t *SomeOtherAggregate) Handle(command CommandMessage) error {
return nil
}
type EmptyAggregate struct {
}