|
| 1 | +package command_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/get-eventually/go-eventually/core/aggregate" |
| 8 | + "github.com/get-eventually/go-eventually/core/command" |
| 9 | + "github.com/get-eventually/go-eventually/core/event" |
| 10 | + "github.com/get-eventually/go-eventually/core/test/scenario" |
| 11 | + appcommand "github.com/get-eventually/go-eventually/examples/todolist/command" |
| 12 | + "github.com/get-eventually/go-eventually/examples/todolist/domain/todolist" |
| 13 | + "github.com/google/uuid" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAddTodoListItem(t *testing.T) { |
| 17 | + now := time.Now() |
| 18 | + commandHandlerFactory := func(es event.Store) appcommand.AddTodoListItemHandler { |
| 19 | + return appcommand.AddTodoListItemHandler{ |
| 20 | + Clock: func() time.Time { return now }, |
| 21 | + Repository: aggregate.NewEventSourcedRepository(es, todolist.Type), |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + todoListID := todolist.ID(uuid.New()) |
| 26 | + todoItemID := todolist.ItemID(uuid.New()) |
| 27 | + listTitle := "my list" |
| 28 | + listOwner := "me" |
| 29 | + |
| 30 | + t.Run("it fails when the target TodoList does not exist", func(t *testing.T) { |
| 31 | + scenario.CommandHandler[appcommand.AddTodoListItem, appcommand.AddTodoListItemHandler](). |
| 32 | + When(command.ToEnvelope(appcommand.AddTodoListItem{ |
| 33 | + TodoListID: todoListID, |
| 34 | + TodoItemID: todoItemID, |
| 35 | + Title: "a todo item that should fail", |
| 36 | + })). |
| 37 | + ThenError(aggregate.ErrRootNotFound). |
| 38 | + AssertOn(t, commandHandlerFactory) |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("it works", func(t *testing.T) { |
| 42 | + scenario.CommandHandler[appcommand.AddTodoListItem, appcommand.AddTodoListItemHandler](). |
| 43 | + Given(event.Persisted{ |
| 44 | + StreamID: event.StreamID(todoListID.String()), |
| 45 | + Version: 1, |
| 46 | + Envelope: event.ToEnvelope(todolist.WasCreated{ |
| 47 | + ID: todoListID, |
| 48 | + Title: listTitle, |
| 49 | + Owner: listOwner, |
| 50 | + CreationTime: now.Add(-2 * time.Minute), |
| 51 | + }), |
| 52 | + }). |
| 53 | + When(command.ToEnvelope(appcommand.AddTodoListItem{ |
| 54 | + TodoListID: todoListID, |
| 55 | + TodoItemID: todoItemID, |
| 56 | + Title: "a todo item that should succeed", |
| 57 | + })). |
| 58 | + Then(event.Persisted{ |
| 59 | + StreamID: event.StreamID(todoListID.String()), |
| 60 | + Version: 2, |
| 61 | + Envelope: event.ToEnvelope(todolist.ItemWasAdded{ |
| 62 | + ID: todoItemID, |
| 63 | + Title: "a todo item that should succeed", |
| 64 | + CreationTime: now, |
| 65 | + }), |
| 66 | + }). |
| 67 | + AssertOn(t, commandHandlerFactory) |
| 68 | + }) |
| 69 | + |
| 70 | +} |
0 commit comments