| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 31 |
|
| 32 |
func TestBookstore(t *testing.T) { |
| 33 |
|
| 34 |
b := bookstore.NewClient(service, nil) |
| 35 |
|
| 36 |
{ |
| 37 |
err := b.DeleteShelves() |
| 38 |
if err != nil { |
| 39 |
t.Log("delete shelves failed") |
| 40 |
t.Fail() |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 128 |
{ |
| 129 |
err := b.DeleteShelf(2) |
| 130 |
if err != nil { |
| 131 |
t.Log("delete shelf failed") |
| 132 |
t.Fail() |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 182 |
{ |
| 183 |
_, err := b.GetBook(1, 1) |
| 184 |
if err != nil { |
| 185 |
t.Log("get book failed") |
| 186 |
t.Fail() |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 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 |
|
| 202 |
{ |
| 203 |
err := b.DeleteBook(1, 2) |
| 204 |
if err != nil { |
| 205 |
t.Log("delete book failed") |
| 206 |
t.Fail() |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 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 |
|
| 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 |
|
| 233 |
if resp.StatusCode != 400 { |
| 234 |
t.Log("bad request failed") |
| 235 |
t.Fail() |
| 236 |
} |
| 237 |
return |
| 238 |
} |
| 239 |
} |