Skip to content

Commit

Permalink
fix(gtest): fix TransitionTest when matching same event multiple ti…
Browse files Browse the repository at this point in the history
…mes with different event data
  • Loading branch information
bounoable committed Nov 28, 2023
1 parent 3fcc13e commit 83f0216
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
22 changes: 10 additions & 12 deletions exp/gtest/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,28 @@ func Signal(event string, opts ...TransitionTestOption) *TransitionTest[any] {
func (test *TransitionTest[EventData]) Run(t *testing.T, a aggregate.Aggregate) {
t.Helper()

wantMatches := test.MatchCount
if wantMatches == 0 {
wantMatches = 1
}

var matches uint
for _, evt := range a.AggregateChanges() {
if evt.Name() != test.Event {
continue
}

if test.MatchCount == 0 {
if err := test.testEquality(evt); err != nil {
t.Errorf("Aggregate %q should transition to %q with %T; %s", pick.AggregateName(a), test.Event, test.Data, err)
}
return
}

if test.testEquality(evt) == nil {
matches++
}
}

if test.MatchCount == 0 {
t.Errorf("Aggregate %q should transition to %q with %T", pick.AggregateName(a), test.Event, test.Data)
return
}
if matches != wantMatches {
if wantMatches == 1 {
t.Errorf("Aggregate %q should transition to %q with %T", pick.AggregateName(a), test.Event, test.Data)
return
}

if matches != test.MatchCount {
t.Errorf("Aggregate %q should transition to %q with %T %d times; got %d", pick.AggregateName(a), test.Event, test.Data, test.MatchCount, matches)
}
}
Expand Down
18 changes: 18 additions & 0 deletions exp/gtest/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ func TestTransition(t *testing.T) {
gtest.Transition("foo", d).Run(t, foo)
}

func TestTransition_sameEventNameOtherData(t *testing.T) {
type data struct {
Foo string
Bar bool
}

foo := aggregate.New("foo", uuid.New())

d1 := data{Foo: "foo", Bar: false}
d2 := data{Foo: "foo", Bar: true}

aggregate.Next(foo, "foo", d1)
aggregate.Next(foo, "foo", d2)

gtest.Transition("foo", d1).Run(t, foo)
gtest.Transition("foo", d2).Run(t, foo)
}

type comparableData struct {
Foo string
Bar int
Expand Down

0 comments on commit 83f0216

Please sign in to comment.