-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
90 lines (65 loc) · 1.89 KB
/
update_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package bongoz
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestUpdate(t *testing.T) {
conn := getConnection()
collection := conn.Collection("pages")
defer conn.Session.Close()
Convey("Update", t, func() {
endpoint := NewEndpoint("/api/pages", conn, "pages")
Convey("basic", func() {
endpoint.Factory = Factory
router := endpoint.GetRouter()
w := httptest.NewRecorder()
obj := &Page{
Content: "Foo",
IntValue: 5,
}
err := collection.Save(obj)
So(err, ShouldEqual, nil)
updated := map[string]string{
"Content": "bar",
}
marshaled, err := json.Marshal(updated)
So(err, ShouldEqual, nil)
reader := strings.NewReader(string(marshaled))
req, _ := http.NewRequest("PUT", strings.Join([]string{"/api/pages", obj.Id.Hex()}, "/"), reader)
router.ServeHTTP(w, req)
response := &singleResponse{}
So(w.Code, ShouldEqual, 200)
err = json.Unmarshal(w.Body.Bytes(), response)
So(err, ShouldEqual, nil)
So(response.Data["content"], ShouldEqual, "bar")
So(response.Data["intValue"], ShouldEqual, 5.0)
})
Convey("validation errors", func() {
endpoint.Factory = ValidFactory
router := endpoint.GetRouter()
w := httptest.NewRecorder()
obj := &validatedModel{
Content: "Biff",
}
err := collection.Save(obj)
So(err, ShouldEqual, nil)
update := map[string]string{
"Content": "",
}
marshaled, err := json.Marshal(update)
So(err, ShouldEqual, nil)
reader := strings.NewReader(string(marshaled))
req, _ := http.NewRequest("PUT", strings.Join([]string{"/api/pages", obj.Id.Hex()}, "/"), reader)
router.ServeHTTP(w, req)
So(w.Code, ShouldEqual, 400)
So(w.Body.String(), ShouldEqual, "{\"errors\":[\"Content is required\"]}")
})
Reset(func() {
conn.Session.DB("dplservertest").DropDatabase()
})
})
}