Skip to content

Commit

Permalink
Merge pull request #356 from dogmatiq/use-enginekit-fixtures
Browse files Browse the repository at this point in the history
Use enginekit fixtures
  • Loading branch information
jmalloc authored Sep 24, 2024
2 parents bcbebdc + 30c05ee commit 827c0ef
Show file tree
Hide file tree
Showing 47 changed files with 1,126 additions and 1,043 deletions.
41 changes: 9 additions & 32 deletions action.call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"time"

"github.com/dogmatiq/configkit"
. "github.com/dogmatiq/configkit/fixtures"
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/dogma/fixtures"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/dogmatiq/testkit"
"github.com/dogmatiq/testkit/engine"
Expand All @@ -33,34 +31,13 @@ var _ = g.Describe("func Call()", func() {
app = &ApplicationStub{
ConfigureFunc: func(c dogma.ApplicationConfigurer) {
c.Identity("<app>", "b51cde16-aaec-4d75-ae14-06282e3a72c8")
c.RegisterAggregate(&AggregateMessageHandlerStub{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<aggregate>", "832d78d7-a006-414f-b6d7-3153aa7c9ab8")
c.RegisterIntegration(&IntegrationMessageHandlerStub{
ConfigureFunc: func(c dogma.IntegrationConfigurer) {
c.Identity("<integration>", "832d78d7-a006-414f-b6d7-3153aa7c9ab8")
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
dogma.HandlesCommand[CommandStub[TypeA]](),
)
},
RouteCommandToInstanceFunc: func(
dogma.Command,
) string {
return "<instance>"
},
})
c.RegisterProcess(&ProcessMessageHandlerStub{
ConfigureFunc: func(c dogma.ProcessConfigurer) {
c.Identity("<process>", "b64cdd19-782e-4e4e-9e5f-a95a943d6340")
c.Routes(
dogma.HandlesEvent[MessageE](),
dogma.ExecutesCommand[MessageC](),
)
},
RouteEventToInstanceFunc: func(
context.Context,
dogma.Event,
) (string, bool, error) {
return "<instance>", true, nil
},
})
},
}
Expand All @@ -86,7 +63,7 @@ var _ = g.Describe("func Call()", func() {
Call(func() {
e.ExecuteCommand(
context.Background(),
MessageC1,
CommandA1,
)
}),
)
Expand All @@ -97,8 +74,8 @@ var _ = g.Describe("func Call()", func() {
MessageID: "1",
CausationID: "1",
CorrelationID: "1",
Message: MessageC1,
Type: MessageCType,
Message: CommandA1,
Type: message.TypeOf(CommandA1),
Role: message.CommandRole,
CreatedAt: startTime,
},
Expand All @@ -121,10 +98,10 @@ var _ = g.Describe("func Call()", func() {
Call(func() {
e.ExecuteCommand(
context.Background(),
MessageC1,
CommandA1,
)
}),
ToExecuteCommand(MessageC1),
ToExecuteCommand(CommandA1),
)
})

Expand Down
41 changes: 17 additions & 24 deletions action.dispatch.command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"time"

"github.com/dogmatiq/configkit"
. "github.com/dogmatiq/configkit/fixtures"
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/dogma/fixtures"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/dogmatiq/testkit"
"github.com/dogmatiq/testkit/engine"
Expand All @@ -32,19 +30,14 @@ var _ = g.Describe("func ExecuteCommand()", func() {
app = &ApplicationStub{
ConfigureFunc: func(c dogma.ApplicationConfigurer) {
c.Identity("<app>", "a84b2620-4675-4024-b55b-cd5dbeb6e293")
c.RegisterAggregate(&AggregateMessageHandlerStub{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<aggregate>", "d1cf3af1-6c20-4125-8e68-192a6075d0b4")
c.RegisterIntegration(&IntegrationMessageHandlerStub{
ConfigureFunc: func(c dogma.IntegrationConfigurer) {
c.Identity("<integration>", "d1cf3af1-6c20-4125-8e68-192a6075d0b4")
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
dogma.HandlesCommand[CommandStub[TypeA]](),
dogma.RecordsEvent[EventStub[TypeA]](),
)
},
RouteCommandToInstanceFunc: func(
dogma.Command,
) string {
return "<instance>"
},
})
},
}
Expand All @@ -65,7 +58,7 @@ var _ = g.Describe("func ExecuteCommand()", func() {

g.It("dispatches the message", func() {
test.Prepare(
ExecuteCommand(MessageC1),
ExecuteCommand(CommandA1),
)

Expect(buf.Facts()).To(ContainElement(
Expand All @@ -74,8 +67,8 @@ var _ = g.Describe("func ExecuteCommand()", func() {
MessageID: "1",
CausationID: "1",
CorrelationID: "1",
Message: MessageC1,
Type: MessageCType,
Message: CommandA1,
Type: message.TypeOf(CommandA1),
Role: message.CommandRole,
CreatedAt: startTime,
},
Expand All @@ -95,46 +88,46 @@ var _ = g.Describe("func ExecuteCommand()", func() {
t.FailSilently = true

test.Prepare(
ExecuteCommand(MessageX1),
ExecuteCommand(CommandX1),
)

Expect(t.Failed()).To(BeTrue())
Expect(t.Logs).To(ContainElement(
"cannot execute command, fixtures.MessageX is a not a recognized message type",
"cannot execute command, stubs.CommandStub[TypeX] is a not a recognized message type",
))
})

g.It("fails the test if the message type is not a command", func() {
t.FailSilently = true

test.Prepare(
ExecuteCommand(MessageE1),
ExecuteCommand(EventA1),
)

Expect(t.Failed()).To(BeTrue())
Expect(t.Logs).To(ContainElement(
"cannot execute command, fixtures.MessageE is configured as an event",
"cannot execute command, stubs.EventStub[TypeA] is configured as an event",
))
})

g.It("does not satisfy its own expectations", func() {
t.FailSilently = true

test.Expect(
ExecuteCommand(MessageC1),
ToExecuteCommand(MessageC1),
ExecuteCommand(CommandA1),
ToExecuteCommand(CommandA1),
)

Expect(t.Failed()).To(BeTrue())
})

g.It("produces the expected caption", func() {
test.Prepare(
ExecuteCommand(MessageC1),
ExecuteCommand(CommandA1),
)

Expect(t.Logs).To(ContainElement(
"--- executing fixtures.MessageC command ---",
"--- executing stubs.CommandStub[TypeA] command ---",
))
})

Expand All @@ -145,7 +138,7 @@ var _ = g.Describe("func ExecuteCommand()", func() {
})

g.It("captures the location that the action was created", func() {
act := executeCommand(MessageC1)
act := executeCommand(CommandA1)
Expect(act.Location()).To(MatchAllFields(
Fields{
"Func": Equal("github.com/dogmatiq/testkit_test.executeCommand"),
Expand Down
30 changes: 14 additions & 16 deletions action.dispatch.event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"time"

"github.com/dogmatiq/configkit"
. "github.com/dogmatiq/configkit/fixtures"
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/dogma/fixtures"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/dogmatiq/testkit"
"github.com/dogmatiq/testkit/engine"
Expand Down Expand Up @@ -37,8 +35,8 @@ var _ = g.Describe("func RecordEvent()", func() {
ConfigureFunc: func(c dogma.ProcessConfigurer) {
c.Identity("<process>", "1c0dd111-fe12-4dee-a8bc-64abea1dce8f")
c.Routes(
dogma.HandlesEvent[MessageE](),
dogma.ExecutesCommand[MessageC](),
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
RouteEventToInstanceFunc: func(
Expand Down Expand Up @@ -67,7 +65,7 @@ var _ = g.Describe("func RecordEvent()", func() {

g.It("dispatches the message", func() {
test.Prepare(
RecordEvent(MessageE1),
RecordEvent(EventA1),
)

Expect(buf.Facts()).To(ContainElement(
Expand All @@ -76,8 +74,8 @@ var _ = g.Describe("func RecordEvent()", func() {
MessageID: "1",
CausationID: "1",
CorrelationID: "1",
Message: MessageE1,
Type: MessageEType,
Message: EventA1,
Type: message.TypeOf(EventA1),
Role: message.EventRole,
CreatedAt: startTime,
},
Expand All @@ -97,46 +95,46 @@ var _ = g.Describe("func RecordEvent()", func() {
t.FailSilently = true

test.Prepare(
RecordEvent(MessageX1),
RecordEvent(EventX1),
)

Expect(t.Failed()).To(BeTrue())
Expect(t.Logs).To(ContainElement(
"cannot record event, fixtures.MessageX is a not a recognized message type",
"cannot record event, stubs.EventStub[TypeX] is a not a recognized message type",
))
})

g.It("fails the test if the message type is not an event", func() {
t.FailSilently = true

test.Prepare(
RecordEvent(MessageC1),
RecordEvent(CommandA1),
)

Expect(t.Failed()).To(BeTrue())
Expect(t.Logs).To(ContainElement(
"cannot record event, fixtures.MessageC is configured as a command",
"cannot record event, stubs.CommandStub[TypeA] is configured as a command",
))
})

g.It("does not satisfy its own expectations", func() {
t.FailSilently = true

test.Expect(
RecordEvent(MessageE1),
ToRecordEvent(MessageE1),
RecordEvent(EventA1),
ToRecordEvent(EventA1),
)

Expect(t.Failed()).To(BeTrue())
})

g.It("produces the expected caption", func() {
test.Prepare(
RecordEvent(MessageE1),
RecordEvent(EventA1),
)

Expect(t.Logs).To(ContainElement(
"--- recording fixtures.MessageE event ---",
"--- recording stubs.EventStub[TypeA] event ---",
))
})

Expand All @@ -147,7 +145,7 @@ var _ = g.Describe("func RecordEvent()", func() {
})

g.It("captures the location that the action was created", func() {
act := recordEvent(MessageE1)
act := recordEvent(EventA1)
Expect(act.Location()).To(MatchAllFields(
Fields{
"Func": Equal("github.com/dogmatiq/testkit_test.recordEvent"),
Expand Down
32 changes: 22 additions & 10 deletions action.dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import (

// ExecuteCommand returns an Action that executes a command message.
func ExecuteCommand(m dogma.Command) Action {
if err := validateMessage(m); err != nil {
panic(fmt.Sprintf("ExecuteCommand(%T): %s", m, err))
if m == nil {
panic("ExecuteCommand(<nil>): message must not be nil")
}

mt := message.TypeOf(m)

if err := m.Validate(); err != nil {
panic(fmt.Sprintf("ToRecordEvent(%s): %s", mt, err))
}

return dispatchAction{
Expand All @@ -25,8 +31,14 @@ func ExecuteCommand(m dogma.Command) Action {

// RecordEvent returns an Action that records an event message.
func RecordEvent(m dogma.Event) Action {
if err := validateMessage(m); err != nil {
panic(fmt.Sprintf("RecordEvent(%T): %s", m, err))
if m == nil {
panic("RecordEvent(<nil>): message must not be nil")
}

mt := message.TypeOf(m)

if err := m.Validate(); err != nil {
panic(fmt.Sprintf("RecordEvent(%s): %s", mt, err))
}

return dispatchAction{
Expand All @@ -47,8 +59,8 @@ type dispatchAction struct {
func (a dispatchAction) Caption() string {
return inflect.Sprintf(
a.r,
"<producing> %T <message>",
a.m,
"<producing> %s <message>",
message.TypeOf(a.m),
)
}

Expand All @@ -70,17 +82,17 @@ func (a dispatchAction) Do(ctx context.Context, s ActionScope) error {
if !ok {
return inflect.Errorf(
a.r,
"cannot <produce> <message>, %T is a not a recognized message type",
a.m,
"cannot <produce> <message>, %s is a not a recognized message type",
mt,
)
} else if r != a.r {
return inflect.Errorf(
a.r,
"cannot <produce> <message>, %s",
inflect.Sprintf(
r,
"%T is configured as a <message>",
a.m,
"%s is configured as a <message>",
mt,
),
)
}
Expand Down
Loading

0 comments on commit 827c0ef

Please sign in to comment.