-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathginm_test.go
173 lines (143 loc) · 3.99 KB
/
ginm_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ginm_test
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/gavv/httpexpect"
"github.com/gin-gonic/gin"
ginm "github.com/senghoo/gin-mongodb-middleware"
)
const (
DatabaseName = "ginm_test"
CollectName = "article"
DBAddress = "127.0.0.1"
)
var server *httptest.Server
var session *mgo.Session
type Article struct {
ID bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
Title string `json:"title" bson:"title"`
Content string `json:"content" bson:"content"`
Tag []string `json:"tag" bson:"tag"`
Author struct {
Name string `json:"name" bson:"name"`
Email string `json:"email" bson:"email"`
} `json:"author" bson:"author"`
}
func (a *Article) PreCreate() error {
a.ID = bson.NewObjectId()
return nil
}
func init() {
var err error
// gin handle
handler := gin.Default()
// mongodb session
session, err = mgo.Dial(DBAddress)
if err != nil {
log.Fatal(err)
}
// middleware
middleware := ginm.NewMiddleware(session)
handler.Use(middleware.Connect)
// blueprint
group := handler.Group("/articles")
blueprint := ginm.NewBlueprint(new(Article), DatabaseName, CollectName)
blueprint.Routes(group)
server = httptest.NewServer(handler)
}
func TestMain(m *testing.M) {
setup()
ret := m.Run()
teardown()
os.Exit(ret)
}
func setup() {
}
func teardown() {
session.Clone().DB(DatabaseName).DropDatabase()
}
func TestAPI(t *testing.T) {
e := httpexpect.New(t, server.URL)
article := &Article{
Title: "title",
Content: "content",
Tag: []string{"tag1", "tag2"},
}
article.Author.Name = "name"
article.Author.Email = "name@example.com"
// new
e.POST("/articles/").WithJSON(article).
Expect().
Status(http.StatusCreated)
// list
val := e.GET("/articles/").
Expect().
Status(http.StatusOK).
JSON()
val.Array().Length().Equal(1)
val.Array().First().Object().Value("title").Equal("title")
val.Array().First().Object().Value("content").Equal("content")
tags := val.Array().First().Object().Value("tag").Array()
tags.Length().Equal(2)
tags.Element(0).Equal("tag1")
tags.Element(1).Equal("tag2")
author := val.Array().First().Object().Value("author").Object()
author.Value("name").Equal("name")
author.Value("email").Equal("name@example.com")
// get
id := val.Array().First().Object().Value("id").String().Raw()
articleURI := fmt.Sprintf("/articles/%s", id)
val = e.GET(articleURI).
Expect().
Status(http.StatusOK).
JSON()
val.Object().Value("title").Equal("title")
val.Object().Value("content").Equal("content")
tags = val.Object().Value("tag").Array()
tags.Length().Equal(2)
tags.Element(0).Equal("tag1")
tags.Element(1).Equal("tag2")
author = val.Object().Value("author").Object()
author.Value("name").Equal("name")
author.Value("email").Equal("name@example.com")
// patch
val = e.PATCH(articleURI).WithJSON(map[string]interface{}{
"title": "title2",
"tag.1": "updated_tag",
"author.name": "new name",
}).Expect().Status(http.StatusOK).JSON()
val.Object().Value("title").Equal("title2")
val.Object().Value("content").Equal("content")
tags = val.Object().Value("tag").Array()
tags.Length().Equal(2)
tags.Element(0).Equal("tag1")
tags.Element(1).Equal("updated_tag")
author = val.Object().Value("author").Object()
author.Value("name").Equal("new name")
author.Value("email").Equal("name@example.com")
// post
val = e.PUT(articleURI).WithJSON(article).
Expect().
Status(http.StatusOK).JSON()
val.Object().Value("title").Equal("title")
val.Object().Value("content").Equal("content")
tags = val.Object().Value("tag").Array()
tags.Length().Equal(2)
tags.Element(0).Equal("tag1")
tags.Element(1).Equal("tag2")
author = val.Object().Value("author").Object()
author.Value("name").Equal("name")
author.Value("email").Equal("name@example.com")
// delete
e.DELETE(articleURI).Expect().Status(http.StatusNoContent)
e.GET("/articles/").
Expect().
Status(http.StatusOK).
JSON().Array().Length().Equal(1)
}