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

Function TestMergeBook

pkg/cli/cmd/sync/sync_test.go:1463–1793  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1461}
1462
1463func TestMergeBook(t *testing.T) {
1464 t.Run("insert, no duplicates", func(t *testing.T) {
1465 // set up
1466 db := database.InitTestMemoryDB(t)
1467
1468 // test
1469 tx, err := db.Begin()
1470 if err != nil {
1471 t.Fatal(errors.Wrap(err, "beginning a transaction").Error())
1472 }
1473
1474 b1 := client.SyncFragBook{
1475 UUID: "b1-uuid",
1476 USN: 12,
1477 AddedOn: 1541108743,
1478 Label: "b1-label",
1479 Deleted: false,
1480 }
1481
1482 if err := mergeBook(tx, b1, modeInsert); err != nil {
1483 tx.Rollback()
1484 t.Fatal(errors.Wrap(err, "executing").Error())
1485 }
1486
1487 tx.Commit()
1488
1489 // execute
1490 var noteCount, bookCount int
1491 database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), &noteCount)
1492 database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount)
1493
1494 assert.Equalf(t, noteCount, 0, "note count mismatch")
1495 assert.Equalf(t, bookCount, 1, "book count mismatch")
1496
1497 var b1Record database.Book
1498 database.MustScan(t, "getting b1",
1499 db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"),
1500 &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty)
1501
1502 assert.Equal(t, b1Record.UUID, b1.UUID, "b1 UUID mismatch")
1503 assert.Equal(t, b1Record.Label, b1.Label, "b1 Label mismatch")
1504 assert.Equal(t, b1Record.USN, b1.USN, "b1 USN mismatch")
1505 })
1506
1507 t.Run("insert, 1 duplicate", func(t *testing.T) {
1508 // set up
1509 db := database.InitTestMemoryDB(t)
1510 database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false)
1511
1512 // test
1513 tx, err := db.Begin()
1514 if err != nil {
1515 t.Fatal(errors.Wrap(err, "beginning a transaction").Error())
1516 }
1517
1518 b := client.SyncFragBook{
1519 UUID: "b2-uuid",
1520 USN: 12,

Callers

nothing calls this directly

Calls 12

InitTestMemoryDBFunction · 0.92
MustScanFunction · 0.92
EqualfFunction · 0.92
EqualFunction · 0.92
MustExecFunction · 0.92
MustGenerateUUIDFunction · 0.92
mergeBookFunction · 0.85
BeginMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
QueryRowMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected