-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller_dep_test.go
68 lines (57 loc) · 1.56 KB
/
controller_dep_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package controller_test
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
apiv1 "github.com/gotestbootcamp/go-todo-app/api/v1"
"github.com/gotestbootcamp/go-todo-app/controller"
"github.com/gotestbootcamp/go-todo-app/ledger"
"github.com/gotestbootcamp/go-todo-app/model"
"github.com/gotestbootcamp/go-todo-app/store/fake"
)
// exercise
func TestTodoCreate(t *testing.T) {
handler := controller.New(memoryStorage())
t.Run("test post", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/todos", bodyFromTodo(model.Todo{Title: "foo", Assignee: "fede", Description: "todo"}))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()
apiRes := &apiv1.Response{}
err := json.NewDecoder(res.Body).Decode(&apiRes)
if err != nil {
t.Fatalf("expected error to be nil got %v", err)
}
if len(apiRes.Result.Items) != 1 {
t.Fatalf("expecting one item back")
}
// TODO the uuid is generated interacting with a remote server
// make it deterministic
if apiRes.Result.Items[0].ID != "1234" {
t.Fatalf("expecting id 1234 got %s", apiRes.Result.Items[0].ID)
}
})
}
func memoryStorage() *ledger.Ledger {
st, err := fake.NewMem()
if err != nil {
panic("failed to initialize the memory storage")
}
ldg, err := ledger.New(st)
if err != nil {
panic("failed to initialize the ledger")
}
return ldg
}
func bodyFromTodo(t model.Todo) io.Reader {
serialized, err := t.Serialize()
if err != nil {
panic("")
}
res := bytes.NewReader(serialized)
return res
}