|
| 1 | +package gtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/modernice/goes/aggregate" |
| 10 | + "github.com/modernice/goes/event" |
| 11 | + "github.com/modernice/goes/helper/pick" |
| 12 | +) |
| 13 | + |
| 14 | +// ConstructorTest is a test for aggregate constructors. It checks if the |
| 15 | +// constructor properly sets the AggregateID and calls the OnCreated hook, if |
| 16 | +// provided, with the created aggregate. |
| 17 | +type ConstructorTest[A aggregate.Aggregate] struct { |
| 18 | + Constructor func(uuid.UUID) A |
| 19 | + OnCreated func(A) error |
| 20 | +} |
| 21 | + |
| 22 | +// ConstructorTestOption is a function that modifies a ConstructorTest for an |
| 23 | +// Aggregate. It is used to customize the behavior of a ConstructorTest, such as |
| 24 | +// providing a custom OnCreated hook function. |
| 25 | +type ConstructorTestOption[A aggregate.Aggregate] func(*ConstructorTest[A]) |
| 26 | + |
| 27 | +// Created configures a ConstructorTest with a custom function to be called when |
| 28 | +// an Aggregate is created, allowing for additional validation or setup steps. |
| 29 | +// The provided function takes the created Aggregate as its argument and returns |
| 30 | +// an error if any issues are encountered during execution. |
| 31 | +func Created[A aggregate.Aggregate](fn func(A) error) ConstructorTestOption[A] { |
| 32 | + return func(test *ConstructorTest[A]) { |
| 33 | + test.OnCreated = fn |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Constructor creates a new ConstructorTest with the specified constructor |
| 38 | +// function and optional test options. It returns a pointer to the created |
| 39 | +// ConstructorTest. |
| 40 | +func Constructor[A aggregate.Aggregate](constructor func(uuid.UUID) A, opts ...ConstructorTestOption[A]) *ConstructorTest[A] { |
| 41 | + test := &ConstructorTest[A]{Constructor: constructor} |
| 42 | + for _, opt := range opts { |
| 43 | + opt(test) |
| 44 | + } |
| 45 | + return test |
| 46 | +} |
| 47 | + |
| 48 | +// Run executes the ConstructorTest, ensuring that the constructed aggregate has |
| 49 | +// the correct UUID and, if provided, calls the OnCreated hook without errors. |
| 50 | +// If any of these checks fail, an error is reported to the given testing.T. |
| 51 | +func (test *ConstructorTest[A]) Run(t *testing.T) { |
| 52 | + t.Helper() |
| 53 | + |
| 54 | + id := uuid.New() |
| 55 | + a := test.Constructor(id) |
| 56 | + |
| 57 | + if pick.AggregateID(a) != id { |
| 58 | + t.Errorf("AggregateID should be %q; got %q", id, pick.AggregateID(a)) |
| 59 | + } |
| 60 | + |
| 61 | + if test.OnCreated != nil { |
| 62 | + if err := test.OnCreated(a); err != nil { |
| 63 | + t.Errorf("OnCreated hook failed with %q", err) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// TransitionTest represents a test that checks whether an aggregate transitions |
| 69 | +// to a specific event with the specified data. It can be used to ensure that an |
| 70 | +// aggregate properly handles its internal state changes and produces the |
| 71 | +// correct events with the expected data. |
| 72 | +type TransitionTest[EventData comparable] struct { |
| 73 | + transitionTestConfig |
| 74 | + |
| 75 | + Event string |
| 76 | + Data EventData |
| 77 | +} |
| 78 | + |
| 79 | +type transitionTestConfig struct { |
| 80 | + MatchCount uint |
| 81 | +} |
| 82 | + |
| 83 | +// TransitionTestOption is a function that modifies the behavior of a |
| 84 | +// TransitionTest, such as configuring the number of times an event should be |
| 85 | +// matched. It takes a transitionTestConfig struct and modifies its properties |
| 86 | +// based on the desired configuration. |
| 87 | +type TransitionTestOption func(*transitionTestConfig) |
| 88 | + |
| 89 | +// Times is a TransitionTestOption that configures the number of times an event |
| 90 | +// should match the expected data in a TransitionTest. It takes an unsigned |
| 91 | +// integer argument representing the number of matches expected. |
| 92 | +func Times(times uint) TransitionTestOption { |
| 93 | + return func(cfg *transitionTestConfig) { |
| 94 | + cfg.MatchCount = times |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// Once returns a TransitionTestOption that configures a TransitionTest to |
| 99 | +// expect the specified event and data exactly once. |
| 100 | +func Once() TransitionTestOption { |
| 101 | + return Times(1) |
| 102 | +} |
| 103 | + |
| 104 | +// Transition creates a new TransitionTest with the specified event name and |
| 105 | +// data. It can be used to test if an aggregate transitions to the specified |
| 106 | +// event with the provided data when running the Run method on a *testing.T |
| 107 | +// instance. |
| 108 | +func Transition[EventData comparable](event string, data EventData, opts ...TransitionTestOption) *TransitionTest[EventData] { |
| 109 | + test := TransitionTest[EventData]{ |
| 110 | + Event: event, |
| 111 | + Data: data, |
| 112 | + } |
| 113 | + |
| 114 | + for _, opt := range opts { |
| 115 | + opt(&test.transitionTestConfig) |
| 116 | + } |
| 117 | + |
| 118 | + return &test |
| 119 | +} |
| 120 | + |
| 121 | +// Signal returns a new TransitionTest with the specified event name and no |
| 122 | +// event data. It is used to test aggregate transitions for events without data. |
| 123 | +func Signal(event string, opts ...TransitionTestOption) *TransitionTest[any] { |
| 124 | + return Transition[any](event, nil, opts...) |
| 125 | +} |
| 126 | + |
| 127 | +// Run tests whether an aggregate transitions to the specified event with the |
| 128 | +// expected data. It reports an error if the aggregate does not transition to |
| 129 | +// the specified event, or if the event data does not match the expected data. |
| 130 | +func (test *TransitionTest[EventData]) Run(t *testing.T, a aggregate.Aggregate) { |
| 131 | + t.Helper() |
| 132 | + |
| 133 | + var matches uint |
| 134 | + for _, evt := range a.AggregateChanges() { |
| 135 | + if evt.Name() != test.Event { |
| 136 | + continue |
| 137 | + } |
| 138 | + |
| 139 | + if test.MatchCount == 0 { |
| 140 | + if err := test.testEquality(evt); err != nil { |
| 141 | + t.Errorf("Aggregate %q should transition to %q with %T; %s", pick.AggregateName(a), test.Event, test.Data, err) |
| 142 | + } |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + if test.testEquality(evt) == nil { |
| 147 | + matches++ |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if test.MatchCount == 0 { |
| 152 | + t.Errorf("Aggregate %q should transition to %q with %T", pick.AggregateName(a), test.Event, test.Data) |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + if matches != test.MatchCount { |
| 157 | + t.Errorf("Aggregate %q should transition to %q with %T %d times; got %d", pick.AggregateName(a), test.Event, test.Data, test.MatchCount, matches) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func (test *TransitionTest[EventData]) testEquality(evt event.Event) error { |
| 162 | + if evt.Name() != test.Event { |
| 163 | + return fmt.Errorf("event name %q does not match expected event name %q", evt.Name(), test.Event) |
| 164 | + } |
| 165 | + |
| 166 | + var zero EventData |
| 167 | + if test.Data != zero { |
| 168 | + data := evt.Data() |
| 169 | + if test.Data != data { |
| 170 | + return fmt.Errorf("event data %T does not match expected event data %T\n%s", evt.Data(), test.Data, cmp.Diff(test.Data, data)) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +// NonTransition represents an event that the aggregate should not transition |
| 178 | +// to. It's used in testing to ensure that a specific event does not occur |
| 179 | +// during the test run for a given aggregate. |
| 180 | +type NonTransition string |
| 181 | + |
| 182 | +// Run checks if the given aggregate a does not transition to the event |
| 183 | +// specified by the NonTransition type. If it does, an error is reported with |
| 184 | +// testing.T. |
| 185 | +func (event NonTransition) Run(t *testing.T, a aggregate.Aggregate) { |
| 186 | + t.Helper() |
| 187 | + |
| 188 | + for _, evt := range a.AggregateChanges() { |
| 189 | + if evt.Name() == string(event) { |
| 190 | + t.Errorf("Aggregate %q should not transition to %q", pick.AggregateName(a), string(event)) |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments