ev/event/aggregate_test.go

44 lines
662 B
Go
Raw Normal View History

2022-08-14 10:04:15 -06:00
package event_test
import (
"testing"
2023-09-29 10:07:24 -06:00
"go.sour.is/ev/event"
2022-08-14 10:04:15 -06:00
)
type Agg struct {
Value string
2023-04-02 21:00:22 -06:00
event.IsAggregate
2022-08-14 10:04:15 -06:00
}
var _ event.Aggregate = (*Agg)(nil)
func (a *Agg) streamID() string {
return "value-" + a.Value
}
// ApplyEvent applies the event to the aggrigate state
func (a *Agg) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
switch e := e.(type) {
case *ValueApplied:
a.Value = e.Value
a.SetStreamID(a.streamID())
}
}
}
type ValueApplied struct {
Value string
2023-04-02 16:45:17 -06:00
event.IsEvent
2022-08-14 10:04:15 -06:00
}
var _ event.Event = (*ValueApplied)(nil)
func TestAggregate(t *testing.T) {
agg := &Agg{}
event.Append(agg, &ValueApplied{Value: "one"})
}