forked from google/gnostic-go-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookstore_test.go
285 lines (276 loc) · 6.23 KB
/
bookstore_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright 2019 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"context"
"fmt"
"net/http"
"strings"
"testing"
"github.com/googleapis/gnostic-go-generator/examples/v2.0/bookstore/bookstore"
)
const service = "http://localhost:8080"
//const service = "http://generated-bookstore.appspot.com"
func TestBookstore(t *testing.T) {
// create a client
b := bookstore.NewClient(service, nil)
// reset the service by deleting all shelves
{
ctx := context.Background()
err := b.DeleteShelves(ctx)
if err != nil {
t.Log("delete shelves failed")
t.Fail()
}
}
// verify that the service has no shelves
{
ctx := context.Background()
response, err := b.ListShelves(ctx)
if err != nil {
t.Log("list shelves failed")
t.Fail()
}
if response != nil && (len(response.Shelves) != 0) {
t.Log(fmt.Sprintf("list shelves failed %+v", response.Shelves))
t.Log(fmt.Sprintf("list shelves failed len=%d", len(response.Shelves)))
t.Fail()
}
}
// attempting to get a shelf should return an error
{
ctx := context.Background()
_, err := b.GetShelf(ctx, bookstore.GetShelfParameters{
Shelf: 1,
})
if err == nil {
t.Log("get shelf failed to return an error")
t.Fail()
}
}
// attempting to get a book should return an error
{
ctx := context.Background()
_, err := b.GetBook(ctx, bookstore.GetBookParameters{
Shelf: 1,
Book: 2,
})
if err == nil {
t.Log("get book failed to return an error")
t.Fail()
}
}
// add a shelf
{
ctx := context.Background()
var shelf bookstore.Shelf
shelf.Theme = "mysteries"
response, err := b.CreateShelf(ctx, bookstore.CreateShelfParameters{
Shelf: &shelf,
})
if err != nil {
t.Log("create shelf mysteries failed")
t.Fail()
}
if response != nil && ((response.Name != "shelves/1") || (response.Theme != "mysteries")) {
t.Log("create shelf mysteries failed")
t.Fail()
}
}
// add another shelf
{
ctx := context.Background()
var shelf bookstore.Shelf
shelf.Theme = "comedies"
response, err := b.CreateShelf(ctx, bookstore.CreateShelfParameters{
Shelf: &shelf,
})
if err != nil {
t.Log("create shelf comedies failed")
t.Fail()
}
if (response.Name != "shelves/2") || (response.Theme != "comedies") {
t.Log("create shelf comedies failed")
t.Fail()
}
}
// get the first shelf that was added
{
ctx := context.Background()
response, err := b.GetShelf(ctx, bookstore.GetShelfParameters{
1,
})
if err != nil {
t.Log("get shelf mysteries failed")
t.Fail()
}
if (response.Name != "shelves/1") || (response.Theme != "mysteries") {
t.Log("get shelf mysteries failed")
t.Fail()
}
}
// list shelves and verify that there are 2
{
ctx := context.Background()
response, err := b.ListShelves(ctx)
if err != nil {
t.Log("list shelves failed")
t.Fail()
}
if len(response.Shelves) != 2 {
t.Log("list shelves failed")
t.Fail()
}
}
// delete a shelf
{
ctx := context.Background()
err := b.DeleteShelf(ctx, bookstore.DeleteShelfParameters{
Shelf: 2,
})
if err != nil {
t.Log("delete shelf failed")
t.Fail()
}
}
// list shelves and verify that there is only 1
{
ctx := context.Background()
response, err := b.ListShelves(ctx)
if err != nil {
t.Log("list shelves failed")
t.Fail()
}
if len(response.Shelves) != 1 {
t.Log("list shelves failed")
t.Fail()
}
}
// list books on a shelf, verify that there are none
{
ctx := context.Background()
response, err := b.ListBooks(ctx, bookstore.ListBooksParameters{
1,
})
if err != nil {
t.Log("list books failed")
t.Fail()
}
if len(response.Books) != 0 {
t.Log("list books failed")
t.Fail()
}
}
// create a book
{
ctx := context.Background()
var book bookstore.Book
book.Author = "Agatha Christie"
book.Title = "And Then There Were None"
_, err := b.CreateBook(ctx, bookstore.CreateBookParameters{
Shelf: 1,
Book: &book,
})
if err != nil {
t.Log("create book failed")
t.Fail()
}
}
// create another book
{
ctx := context.Background()
var book bookstore.Book
book.Author = "Agatha Christie"
book.Title = "Murder on the Orient Express"
_, err := b.CreateBook(ctx, bookstore.CreateBookParameters{
Shelf: 1,
Book: &book,
})
if err != nil {
t.Log("create book failed")
t.Fail()
}
}
// get the first book that was added
{
ctx := context.Background()
_, err := b.GetBook(ctx, bookstore.GetBookParameters{
Shelf: 1,
Book: 1,
})
if err != nil {
t.Log("get book failed")
t.Fail()
}
}
// list the books on a shelf and verify that there are 2
{
ctx := context.Background()
response, err := b.ListBooks(ctx, bookstore.ListBooksParameters{
Shelf: 1,
})
if err != nil {
t.Log("list books failed")
t.Fail()
}
if len(response.Books) != 2 {
t.Log("list books failed")
t.Fail()
}
}
// delete a book
{
ctx := context.Background()
err := b.DeleteBook(ctx, bookstore.DeleteBookParameters{
Shelf: 1,
Book: 1,
})
if err != nil {
t.Log("delete book failed")
t.Fail()
}
}
// list the books on a shelf and verify that is only 1
{
ctx := context.Background()
response, err := b.ListBooks(ctx, bookstore.ListBooksParameters{
Shelf: 1,
})
if err != nil {
t.Log("list books failed")
t.Fail()
}
if len(response.Books) != 1 {
t.Log("list books failed")
t.Fail()
}
}
// verify the handling of a badly-formed request
{
req, err := http.NewRequest("POST", service+"/shelves", strings.NewReader(""))
if err != nil {
t.Log("bad request failed")
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
// we expect a 400 (Bad Request) code
if resp.StatusCode != 400 {
t.Log("bad request failed")
t.Fail()
}
return
}
}