MCPcopy Index your code
hub / github.com/dnote/dnote / TestCreateNote

Function TestCreateNote

pkg/server/controllers/notes_test.go:304–356  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

302}
303
304func 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(&noteCount), "counting notes")
340 testutils.MustExec(t, db.First(&noteRecord), "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
358func TestDeleteNote(t *testing.T) {
359 b1UUID := "37868a8e-a844-4265-9a4f-0be598084733"

Callers

nothing calls this directly

Calls 15

InitMemoryDBFunction · 0.92
NewTestFunction · 0.92
NewMockFunction · 0.92
SetupUserDataFunction · 0.92
MustExecFunction · 0.92
MustUUIDFunction · 0.92
MakeReqFunction · 0.92
HTTPAuthDoFunction · 0.92
StatusCodeEqualsFunction · 0.92
EqualfFunction · 0.92
EqualFunction · 0.92
NotEqualFunction · 0.92

Tested by

no test coverage detected