feat(backend): add transactional outbox and embedded goose migrations
This commit is contained in:
1 parent
653f3775f8
commit
15e2e50561
40 files changed
+2471
-67
No files matched your search
@@ -0,0 +1,138 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func validDomainEventInput() DomainEventInput {
|
||||
return DomainEventInput{
|
||||
EventType: "identity.user_registered",
|
||||
SchemaVersion: 1,
|
||||
AggregateType: "user",
|
||||
AggregateID: uuid.MustParse("0192f0c4-7d4e-7a3b-9c1d-2e3f4a5b6c7d"),
|
||||
AggregateVersion: 1,
|
||||
Payload: []byte(` { "user_id" : "0192f0c4-7d4e-7a3b-9c1d-2e3f4a5b6c7d" } `),
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDomainEventAcceptsValidInput(t *testing.T) {
|
||||
input := validDomainEventInput()
|
||||
event, err := NewDomainEvent(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewDomainEvent() error = %v", err)
|
||||
}
|
||||
if event.EventID().Version() != 7 {
|
||||
t.Fatalf("EventID version = %d, want UUIDv7", event.EventID().Version())
|
||||
}
|
||||
if event.EventType() != input.EventType || event.SchemaVersion() != input.SchemaVersion ||
|
||||
event.AggregateType() != input.AggregateType || event.AggregateID() != input.AggregateID ||
|
||||
event.AggregateVersion() != input.AggregateVersion {
|
||||
t.Fatalf("NewDomainEvent() did not preserve identity fields: %+v", event)
|
||||
}
|
||||
if want := `{"user_id":"0192f0c4-7d4e-7a3b-9c1d-2e3f4a5b6c7d"}`; string(event.Payload()) != want {
|
||||
t.Fatalf("Payload() = %s, want compact %s", event.Payload(), want)
|
||||
}
|
||||
event.Payload()[0] = '['
|
||||
if event.Payload()[0] != '{' {
|
||||
t.Fatal("Payload() exposed the event's internal buffer")
|
||||
}
|
||||
|
||||
other, err := NewDomainEvent(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewDomainEvent() error = %v", err)
|
||||
}
|
||||
if other.EventID() == event.EventID() {
|
||||
t.Fatal("NewDomainEvent() reused an event ID")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDomainEventAcceptsPayloadAtSizeLimit(t *testing.T) {
|
||||
input := validDomainEventInput()
|
||||
prefix, suffix := `{"padding":"`, `"}`
|
||||
input.Payload = []byte(prefix + strings.Repeat("a", MaxDomainEventPayloadBytes-len(prefix)-len(suffix)) + suffix)
|
||||
if _, err := NewDomainEvent(input); err != nil {
|
||||
t.Fatalf("NewDomainEvent() rejected a payload at the limit: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDomainEventRejectsInvalidInput(t *testing.T) {
|
||||
prefix, suffix := `{"padding":"`, `"}`
|
||||
oversized := prefix + strings.Repeat("a", MaxDomainEventPayloadBytes-len(prefix)-len(suffix)+1) + suffix
|
||||
tests := []struct {
|
||||
name string
|
||||
change func(*DomainEventInput)
|
||||
}{
|
||||
{name: "empty event type", change: func(input *DomainEventInput) { input.EventType = "" }},
|
||||
{name: "event type without namespace", change: func(input *DomainEventInput) { input.EventType = "registered" }},
|
||||
{name: "uppercase event type", change: func(input *DomainEventInput) { input.EventType = "Identity.UserRegistered" }},
|
||||
{name: "long event type", change: func(input *DomainEventInput) { input.EventType = "a." + strings.Repeat("b", 127) }},
|
||||
{name: "zero schema version", change: func(input *DomainEventInput) { input.SchemaVersion = 0 }},
|
||||
{name: "negative schema version", change: func(input *DomainEventInput) { input.SchemaVersion = -1 }},
|
||||
{name: "empty aggregate type", change: func(input *DomainEventInput) { input.AggregateType = "" }},
|
||||
{name: "dotted aggregate type", change: func(input *DomainEventInput) { input.AggregateType = "identity.user" }},
|
||||
{name: "long aggregate type", change: func(input *DomainEventInput) { input.AggregateType = strings.Repeat("a", 65) }},
|
||||
{name: "nil aggregate ID", change: func(input *DomainEventInput) { input.AggregateID = uuid.Nil }},
|
||||
{name: "zero aggregate version", change: func(input *DomainEventInput) { input.AggregateVersion = 0 }},
|
||||
{name: "negative aggregate version", change: func(input *DomainEventInput) { input.AggregateVersion = -1 }},
|
||||
{name: "empty payload", change: func(input *DomainEventInput) { input.Payload = nil }},
|
||||
{name: "invalid JSON payload", change: func(input *DomainEventInput) { input.Payload = []byte(`{"user_id":`) }},
|
||||
{name: "array payload", change: func(input *DomainEventInput) { input.Payload = []byte(`[1]`) }},
|
||||
{name: "null payload", change: func(input *DomainEventInput) { input.Payload = []byte(`null`) }},
|
||||
{name: "invalid UTF-8 payload", change: func(input *DomainEventInput) { input.Payload = []byte("{\"a\":\"\xff\"}") }},
|
||||
{name: "escaped NUL in value", change: func(input *DomainEventInput) { input.Payload = []byte(`{"a":"x\u0000y"}`) }},
|
||||
{name: "escaped NUL in key", change: func(input *DomainEventInput) { input.Payload = []byte(`{"\u0000":1}`) }},
|
||||
{name: "escaped NUL in nested array", change: func(input *DomainEventInput) { input.Payload = []byte(`{"a":[{"b":"\u0000"}]}`) }},
|
||||
{name: "number outside float64 range", change: func(input *DomainEventInput) { input.Payload = []byte(`{"a":1e400}`) }},
|
||||
{name: "nesting too deep", change: func(input *DomainEventInput) { input.Payload = nestedPayload(maxDomainEventPayloadDepth + 1) }},
|
||||
{name: "oversized payload", change: func(input *DomainEventInput) { input.Payload = []byte(oversized) }},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
input := validDomainEventInput()
|
||||
test.change(&input)
|
||||
_, err := NewDomainEvent(input)
|
||||
if !errors.Is(err, ErrInvalidDomainEvent) {
|
||||
t.Fatalf("NewDomainEvent() error = %v, want ErrInvalidDomainEvent", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDomainEventDoesNotKeepCallerPayload(t *testing.T) {
|
||||
input := validDomainEventInput()
|
||||
input.Payload = []byte(`{"a":1}`)
|
||||
event, err := NewDomainEvent(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewDomainEvent() error = %v", err)
|
||||
}
|
||||
copy(input.Payload, `{"b":2}`)
|
||||
if !bytes.Equal(event.Payload(), []byte(`{"a":1}`)) {
|
||||
t.Fatalf("Payload() = %s, changed with the caller's buffer", event.Payload())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDomainEventAcceptsEdgePayloads(t *testing.T) {
|
||||
for name, payload := range map[string][]byte{
|
||||
"escaped backslash before u0000 text": []byte(`{"a":"\\u0000"}`),
|
||||
"maximum nesting": nestedPayload(maxDomainEventPayloadDepth),
|
||||
"large integer": []byte(`{"a":123456789012345678901234567890}`),
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
input := validDomainEventInput()
|
||||
input.Payload = payload
|
||||
if _, err := NewDomainEvent(input); err != nil {
|
||||
t.Fatalf("NewDomainEvent() error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// nestedPayload returns an object whose containers nest depth levels deep.
|
||||
func nestedPayload(depth int) []byte {
|
||||
return []byte(strings.Repeat(`{"a":`, depth) + "1" + strings.Repeat("}", depth))
|
||||
}
|
||||
Reference in new issue
Block a user