(t *testing.T)
| 302 | } |
| 303 | |
| 304 | func TestCreateNote(t *testing.T) { |
| 305 | db := testutils.InitMemoryDB(t) |
| 306 | |
| 307 | // Setup |
| 308 | a := app.NewTest() |
| 309 | a.DB = db |
| 310 | a.Clock = clock.NewMock() |
| 311 | server := MustNewServer(t, &a) |
| 312 | defer server.Close() |
| 313 | |
| 314 | user := testutils.SetupUserData(db, "alice@test.com", "pass1234") |
| 315 | testutils.MustExec(t, db.Model(&user).Update("max_usn", 101), "preparing user max_usn") |
| 316 | |
| 317 | b1 := database.Book{ |
| 318 | UUID: testutils.MustUUID(t), |
| 319 | UserID: user.ID, |
| 320 | Label: "js", |
| 321 | USN: 58, |
| 322 | } |
| 323 | testutils.MustExec(t, db.Save(&b1), "preparing b1") |
| 324 | |
| 325 | // Execute |
| 326 | |
| 327 | dat := fmt.Sprintf(`{"book_uuid": "%s", "content": "note content"}`, b1.UUID) |
| 328 | req := testutils.MakeReq(server.URL, "POST", "/api/v3/notes", dat) |
| 329 | res := testutils.HTTPAuthDo(t, db, req, user) |
| 330 | |
| 331 | // Test |
| 332 | assert.StatusCodeEquals(t, res, http.StatusCreated, "") |
| 333 | |
| 334 | var noteRecord database.Note |
| 335 | var bookRecord database.Book |
| 336 | var userRecord database.User |
| 337 | var bookCount, noteCount int64 |
| 338 | testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") |
| 339 | testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") |
| 340 | testutils.MustExec(t, db.First(¬eRecord), "finding note") |
| 341 | testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&bookRecord), "finding book") |
| 342 | testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") |
| 343 | |
| 344 | assert.Equalf(t, bookCount, int64(1), "book count mismatch") |
| 345 | assert.Equalf(t, noteCount, int64(1), "note count mismatch") |
| 346 | |
| 347 | assert.Equal(t, bookRecord.Label, b1.Label, "book name mismatch") |
| 348 | assert.Equal(t, bookRecord.UUID, b1.UUID, "book uuid mismatch") |
| 349 | assert.Equal(t, bookRecord.UserID, b1.UserID, "book user_id mismatch") |
| 350 | assert.Equal(t, bookRecord.USN, 58, "book usn mismatch") |
| 351 | |
| 352 | assert.NotEqual(t, noteRecord.UUID, "", "note uuid should have been generated") |
| 353 | assert.Equal(t, noteRecord.BookUUID, b1.UUID, "note book_uuid mismatch") |
| 354 | assert.Equal(t, noteRecord.Body, "note content", "note content mismatch") |
| 355 | assert.Equal(t, noteRecord.USN, 102, "note usn mismatch") |
| 356 | } |
| 357 | |
| 358 | func TestDeleteNote(t *testing.T) { |
| 359 | b1UUID := "37868a8e-a844-4265-9a4f-0be598084733" |
nothing calls this directly
no test coverage detected