(t *testing.T)
| 293 | } |
| 294 | |
| 295 | func TestCreateBook(t *testing.T) { |
| 296 | t.Run("success", func(t *testing.T) { |
| 297 | db := testutils.InitMemoryDB(t) |
| 298 | |
| 299 | // Setup |
| 300 | a := app.NewTest() |
| 301 | a.DB = db |
| 302 | a.Clock = clock.NewMock() |
| 303 | server := MustNewServer(t, &a) |
| 304 | defer server.Close() |
| 305 | |
| 306 | user := testutils.SetupUserData(db, "alice@test.com", "pass1234") |
| 307 | testutils.MustExec(t, db.Model(&user).Update("max_usn", 101), "preparing user max_usn") |
| 308 | |
| 309 | req := testutils.MakeReq(server.URL, "POST", "/api/v3/books", `{"name": "js"}`) |
| 310 | |
| 311 | // Execute |
| 312 | res := testutils.HTTPAuthDo(t, db, req, user) |
| 313 | |
| 314 | // Test |
| 315 | assert.StatusCodeEquals(t, res, http.StatusCreated, "") |
| 316 | |
| 317 | var bookRecord database.Book |
| 318 | var userRecord database.User |
| 319 | var bookCount, noteCount int64 |
| 320 | testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") |
| 321 | testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") |
| 322 | testutils.MustExec(t, db.First(&bookRecord), "finding book") |
| 323 | testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") |
| 324 | |
| 325 | maxUSN := 102 |
| 326 | |
| 327 | assert.Equalf(t, bookCount, int64(1), "book count mismatch") |
| 328 | assert.Equalf(t, noteCount, int64(0), "note count mismatch") |
| 329 | |
| 330 | assert.NotEqual(t, bookRecord.UUID, "", "book uuid should have been generated") |
| 331 | assert.Equal(t, bookRecord.Label, "js", "book name mismatch") |
| 332 | assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") |
| 333 | assert.Equal(t, bookRecord.USN, maxUSN, "book user_id mismatch") |
| 334 | assert.Equal(t, userRecord.MaxUSN, maxUSN, "user max_usn mismatch") |
| 335 | |
| 336 | var got CreateBookResp |
| 337 | if err := json.NewDecoder(res.Body).Decode(&got); err != nil { |
| 338 | t.Fatal(errors.Wrap(err, "decoding")) |
| 339 | } |
| 340 | expected := CreateBookResp{ |
| 341 | Book: presenters.Book{ |
| 342 | UUID: bookRecord.UUID, |
| 343 | USN: bookRecord.USN, |
| 344 | CreatedAt: truncateMicro(bookRecord.CreatedAt), |
| 345 | UpdatedAt: truncateMicro(bookRecord.UpdatedAt), |
| 346 | Label: "js", |
| 347 | }, |
| 348 | } |
| 349 | |
| 350 | got.Book.CreatedAt = truncateMicro(got.Book.CreatedAt) |
| 351 | got.Book.UpdatedAt = truncateMicro(got.Book.UpdatedAt) |
| 352 |
nothing calls this directly
no test coverage detected