Debian Codegraph @

plugins/gnostic-go-generator/examples/v2.0/bookstore/bookstore_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc. All Rights Reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package test
18
19 import (
20 "fmt"
21 "net/http"
22 "strings"
23 "testing"
24
25 "github.com/googleapis/gnostic/plugins/gnostic-go-generator/examples/v2.0/bookstore/bookstore"
26 )
27
28 const service = "http://localhost:8080"
29
30 //const service = "http://generated-bookstore.appspot.com"
31
32 func TestBookstore(t *testing.T) {
33 // create a client
34 b := bookstore.NewClient(service, nil)
35 // reset the service by deleting all shelves
36 {
37 err := b.DeleteShelves()
38 if err != nil {
39 t.Log("delete shelves failed")
40 t.Fail()
41 }
42 }
43 // verify that the service has no shelves
44 {
45 response, err := b.ListShelves()
46 if err != nil {
47 t.Log("list shelves failed")
48 t.Fail()
49 }
50 if (response == nil) || (response.OK == nil) || (response.OK.Shelves != nil) {
51 t.Log(fmt.Sprintf("list shelves failed %+v", response.OK))
52 t.Log(fmt.Sprintf("list shelves failed len=%d", len(response.OK.Shelves)))
53 t.Fail()
54 }
55 }
56 // attempting to get a shelf should return an error
57 {
58 _, err := b.GetShelf(1)
59 if err == nil {
60 t.Log("get shelf failed to return an error")
61 t.Fail()
62 }
63 }
64 // attempting to get a book should return an error
65 {
66 _, err := b.GetBook(1, 2)
67 if err == nil {
68 t.Log("get book failed to return an error")
69 t.Fail()
70 }
71 }
72 // add a shelf
73 {
74 var shelf bookstore.Shelf
75 shelf.Theme = "mysteries"
76 response, err := b.CreateShelf(shelf)
77 if err != nil {
78 t.Log("create shelf mysteries failed")
79 t.Fail()
80 }
81 if (response.OK.Name != "shelves/1") ||
82 (response.OK.Theme != "mysteries") {
83 t.Log("create shelf mysteries failed")
84 t.Fail()
85 }
86 }
87 // add another shelf
88 {
89 var shelf bookstore.Shelf
90 shelf.Theme = "comedies"
91 response, err := b.CreateShelf(shelf)
92 if err != nil {
93 t.Log("create shelf comedies failed")
94 t.Fail()
95 }
96 if (response.OK.Name != "shelves/2") ||
97 (response.OK.Theme != "comedies") {
98 t.Log("create shelf comedies failed")
99 t.Fail()
100 }
101 }
102 // get the first shelf that was added
103 {
104 response, err := b.GetShelf(1)
105 if err != nil {
106 t.Log("get shelf mysteries failed")
107 t.Fail()
108 }
109 if (response.OK.Name != "shelves/1") ||
110 (response.OK.Theme != "mysteries") {
111 t.Log("get shelf mysteries failed")
112 t.Fail()
113 }
114 }
115 // list shelves and verify that there are 2
116 {
117 response, err := b.ListShelves()
118 if err != nil {
119 t.Log("list shelves failed")
120 t.Fail()
121 }
122 if len(response.OK.Shelves) != 2 {
123 t.Log("list shelves failed")
124 t.Fail()
125 }
126 }
127 // delete a shelf
128 {
129 err := b.DeleteShelf(2)
130 if err != nil {
131 t.Log("delete shelf failed")
132 t.Fail()
133 }
134 }
135 // list shelves and verify that there is only 1
136 {
137 response, err := b.ListShelves()
138 if err != nil {
139 t.Log("list shelves failed")
140 t.Fail()
141 }
142 if len(response.OK.Shelves) != 1 {
143 t.Log("list shelves failed")
144 t.Fail()
145 }
146 }
147 // list books on a shelf, verify that there are none
148 {
149 response, err := b.ListBooks(1)
150 if err != nil {
151 t.Log("list books failed")
152 t.Fail()
153 }
154 if len(response.OK.Books) != 0 {
155 t.Log("list books failed")
156 t.Fail()
157 }
158 }
159 // create a book
160 {
161 var book bookstore.Book
162 book.Author = "Agatha Christie"
163 book.Title = "And Then There Were None"
164 _, err := b.CreateBook(1, book)
165 if err != nil {
166 t.Log("create book failed")
167 t.Fail()
168 }
169 }
170 // create another book
171 {
172 var book bookstore.Book
173 book.Author = "Agatha Christie"
174 book.Title = "Murder on the Orient Express"
175 _, err := b.CreateBook(1, book)
176 if err != nil {
177 t.Log("create book failed")
178 t.Fail()
179 }
180 }
181 // get the first book that was added
182 {
183 _, err := b.GetBook(1, 1)
184 if err != nil {
185 t.Log("get book failed")
186 t.Fail()
187 }
188 }
189 // list the books on a shelf and verify that there are 2
190 {
191 response, err := b.ListBooks(1)
192 if err != nil {
193 t.Log("list books failed")
194 t.Fail()
195 }
196 if len(response.OK.Books) != 2 {
197 t.Log("list books failed")
198 t.Fail()
199 }
200 }
201 // delete a book
202 {
203 err := b.DeleteBook(1, 2)
204 if err != nil {
205 t.Log("delete book failed")
206 t.Fail()
207 }
208 }
209 // list the books on a shelf and verify that is only 1
210 {
211 response, err := b.ListBooks(1)
212 if err != nil {
213 t.Log("list books failed")
214 t.Fail()
215 }
216 if len(response.OK.Books) != 1 {
217 t.Log("list books failed")
218 t.Fail()
219 }
220 }
221 // verify the handling of a badly-formed request
222 {
223 req, err := http.NewRequest("POST", service+"/shelves", strings.NewReader(""))
224 if err != nil {
225 t.Log("bad request failed")
226 return
227 }
228 resp, err := http.DefaultClient.Do(req)
229 if err != nil {
230 return
231 }
232 // we expect a 400 (Bad Request) code
233 if resp.StatusCode != 400 {
234 t.Log("bad request failed")
235 t.Fail()
236 }
237 return
238 }
239 }