@@ -38,6 +38,77 @@ func TestAddTodoListItem(t *testing.T) {
38
38
AssertOn (t , commandHandlerFactory )
39
39
})
40
40
41
+ t .Run ("it fails when the same item has already been added" , 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
+ }, event.Persisted {
53
+ StreamID : event .StreamID (todoListID .String ()),
54
+ Version : 2 ,
55
+ Envelope : event .ToEnvelope (todolist.ItemWasAdded {
56
+ ID : todoItemID ,
57
+ Title : "a todo item that should succeed" ,
58
+ CreationTime : now ,
59
+ }),
60
+ }).
61
+ When (command .ToEnvelope (appcommand.AddTodoListItem {
62
+ TodoListID : todoListID ,
63
+ TodoItemID : todoItemID ,
64
+ Title : "uh oh, this is gonna fail" ,
65
+ })).
66
+ ThenError (todolist .ErrItemAlreadyExists ).
67
+ AssertOn (t , commandHandlerFactory )
68
+ })
69
+
70
+ t .Run ("it fails when the item id provided is empty" , func (t * testing.T ) {
71
+ scenario .CommandHandler [appcommand.AddTodoListItem , appcommand.AddTodoListItemHandler ]().
72
+ Given (event.Persisted {
73
+ StreamID : event .StreamID (todoListID .String ()),
74
+ Version : 1 ,
75
+ Envelope : event .ToEnvelope (todolist.WasCreated {
76
+ ID : todoListID ,
77
+ Title : listTitle ,
78
+ Owner : listOwner ,
79
+ CreationTime : now .Add (- 2 * time .Minute ),
80
+ }),
81
+ }).
82
+ When (command .ToEnvelope (appcommand.AddTodoListItem {
83
+ TodoListID : todoListID ,
84
+ TodoItemID : todolist .ItemID (uuid .Nil ),
85
+ Title : "i think i forgot to add an id..." ,
86
+ })).
87
+ ThenError (todolist .ErrEmptyItemID ).
88
+ AssertOn (t , commandHandlerFactory )
89
+ })
90
+
91
+ t .Run ("it fails when an empty item title is provided" , func (t * testing.T ) {
92
+ scenario .CommandHandler [appcommand.AddTodoListItem , appcommand.AddTodoListItemHandler ]().
93
+ Given (event.Persisted {
94
+ StreamID : event .StreamID (todoListID .String ()),
95
+ Version : 1 ,
96
+ Envelope : event .ToEnvelope (todolist.WasCreated {
97
+ ID : todoListID ,
98
+ Title : listTitle ,
99
+ Owner : listOwner ,
100
+ CreationTime : now .Add (- 2 * time .Minute ),
101
+ }),
102
+ }).
103
+ When (command .ToEnvelope (appcommand.AddTodoListItem {
104
+ TodoListID : todoListID ,
105
+ TodoItemID : todoItemID ,
106
+ Title : "" ,
107
+ })).
108
+ ThenError (todolist .ErrEmptyItemTitle ).
109
+ AssertOn (t , commandHandlerFactory )
110
+ })
111
+
41
112
t .Run ("it works" , func (t * testing.T ) {
42
113
scenario .CommandHandler [appcommand.AddTodoListItem , appcommand.AddTodoListItemHandler ]().
43
114
Given (event.Persisted {
@@ -66,5 +137,4 @@ func TestAddTodoListItem(t *testing.T) {
66
137
}).
67
138
AssertOn (t , commandHandlerFactory )
68
139
})
69
-
70
140
}
0 commit comments