Skip to content

Commit

Permalink
Bump version to v0.15.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jun 26, 2024
1 parent 1ff7c3a commit ea3f2e8
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 8 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ The format is based on [Keep a Changelog], and this project adheres to
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [0.15.0] - 2024-03-26

### Added

- Added `Test.EnableHandlersLike()` and `DisableHandlersLike()`, which
enable/disable any handler with a name matches at least one of a given set of
regular expressions.

### Changed

- **[BC]** `Test.EnableHandlers()` and `DisableHandlers()` will now panic when
called with names of handlers that do not exist.

## [0.14.0] - 2024-03-26

This release updates the `testkit` implementation to adhere to Dogma v0.13.0
Expand Down Expand Up @@ -359,7 +372,8 @@ guide][0.11.0 migration guide] for detailed instructions.
[0.13.10]: https://github.com/dogmatiq/testkit/releases/tag/v0.13.10
[0.13.11]: https://github.com/dogmatiq/testkit/releases/tag/v0.13.11
[0.13.12]: https://github.com/dogmatiq/testkit/releases/tag/v0.13.12
[0.14.12]: https://github.com/dogmatiq/testkit/releases/tag/v0.14.0
[0.14.0]: https://github.com/dogmatiq/testkit/releases/tag/v0.14.0
[0.15.0]: https://github.com/dogmatiq/testkit/releases/tag/v0.15.0

<!-- version template
## [0.0.1] - YYYY-MM-DD
Expand Down
77 changes: 70 additions & 7 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testkit
import (
"context"
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -150,26 +151,88 @@ func (t *Test) CommandExecutor() dogma.CommandExecutor {

// EnableHandlers enables a set of handlers by name.
//
// It panics if any of the handler names are not recognized.
//
// By default all integration and projection handlers are disabled.
func (t *Test) EnableHandlers(names ...string) *Test {
return t.enableHandlers(names, true)
}

// DisableHandlers disables a set of handlers by name.
//
// It panics if any of the handler names are not recognized.
//
// By default all integration and projection handlers are disabled.
func (t *Test) DisableHandlers(names ...string) *Test {
return t.enableHandlers(names, false)
}

// EnableHandlersLike enables any handlers with a name that matches one of
// the given regular expression patterns.
//
// It panics if any of patterns do not match any handlers.
//
// By default all integration and projection handlers are disabled.
func (t *Test) EnableHandlersLike(patterns ...string) *Test {
return t.enableHandlersLike(patterns, true)
}

// DisableHandlersLike enables any handlers with a name that matches one of
// the given regular expression patterns.
//
// It panics if any of patterns do not match any handlers.
//
// By default all integration and projection handlers are disabled.
func (t *Test) DisableHandlersLike(patterns ...string) *Test {
return t.enableHandlersLike(patterns, false)
}

func (t *Test) enableHandlers(names []string, enable bool) *Test {
for _, n := range names {
if _, ok := t.app.Handlers().ByName(n); !ok {
panic(fmt.Sprintf(
"the %q application does not have a handler named %q",
t.app.Identity().Name,
n,
))
}

t.operationOptions = append(
t.operationOptions,
engine.EnableHandler(n, true),
engine.EnableHandler(n, enable),
)
}

return t
}

// DisableHandlers disables a set of handlers by name.
//
// By default all integration and projection handlers are disabled.
func (t *Test) DisableHandlers(names ...string) *Test {
for _, n := range names {
func (t *Test) enableHandlersLike(patterns []string, enable bool) *Test {
names := map[string]struct{}{}

for _, p := range patterns {
re := regexp.MustCompile(p)
matched := false

for ident := range t.app.Handlers() {
if re.MatchString(ident.Name) {
names[ident.Name] = struct{}{}
matched = true
}
}

if !matched {
panic(fmt.Sprintf(
"the %q application does not have any handlers with names that match the regular expression: %s",
t.app.Identity().Name,
p,
))
}
}

for n := range names {
t.operationOptions = append(
t.operationOptions,
engine.EnableHandler(n, false),
engine.EnableHandler(n, enable),
)
}

Expand Down
67 changes: 67 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,40 @@ var _ = g.Describe("type Test", func() {
})
})

g.Describe("func EnableHandlersLike()", func() {
g.It("enables handlers with matching names", func() {
called := false
app := &Application{
ConfigureFunc: func(c dogma.ApplicationConfigurer) {
c.Identity("<app>", "7d5b218d-d69b-48d5-8831-2af77561ee62")
c.RegisterProjection(&ProjectionMessageHandler{
ConfigureFunc: func(c dogma.ProjectionConfigurer) {
c.Identity("<projection>", "fb5f05c0-589c-4d64-9599-a4875b5a3569")
c.Routes(
dogma.HandlesEvent[MessageE](),
)
},
HandleEventFunc: func(
_ context.Context,
_, _, _ []byte,
_ dogma.ProjectionEventScope,
_ dogma.Message,
) (bool, error) {
called = true
return true, nil
},
})
},
}

Begin(&testingmock.T{}, app).
EnableHandlersLike(`^\<proj`).
Prepare(RecordEvent(MessageE1))

Expect(called).To(BeTrue())
})
})

g.Describe("func DisableHandlers()", func() {
g.It("disables the handler", func() {
app := &Application{
Expand Down Expand Up @@ -118,4 +152,37 @@ var _ = g.Describe("type Test", func() {
Prepare(ExecuteCommand(MessageC1))
})
})

g.Describe("func DisableHandlersLike()", func() {
g.It("disables the handlers with matching names", func() {
app := &Application{
ConfigureFunc: func(c dogma.ApplicationConfigurer) {
c.Identity("<app>", "e79bcae1-8b9a-4755-a15a-dd56f2bb2fdb")
c.RegisterAggregate(&AggregateMessageHandler{
ConfigureFunc: func(c dogma.AggregateConfigurer) {
c.Identity("<aggregate>", "524f7944-a252-48e0-864b-503a903067c2")
c.Routes(
dogma.HandlesCommand[MessageC](),
dogma.RecordsEvent[MessageE](),
)
},
RouteCommandToInstanceFunc: func(dogma.Message) string {
return "<instance>"
},
HandleCommandFunc: func(
dogma.AggregateRoot,
dogma.AggregateCommandScope,
dogma.Message,
) {
g.Fail("unexpected call")
},
})
},
}

Begin(&testingmock.T{}, app).
DisableHandlersLike(`^\<agg`).
Prepare(ExecuteCommand(MessageC1))
})
})
})

0 comments on commit ea3f2e8

Please sign in to comment.