MCPcopy
hub / github.com/dnote/dnote / TestLocalMigration5

Function TestLocalMigration5

pkg/cli/migrate/migrate_test.go:586–643  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

584}
585
586func TestLocalMigration5(t *testing.T) {
587 // set up
588 db := database.InitTestMemoryDBRaw(t, "./fixtures/local-5-pre-schema.sql")
589 ctx := context.InitTestCtxWithDB(t, db)
590
591 b1UUID := testutils.MustGenerateUUID(t)
592 database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css")
593 b2UUID := testutils.MustGenerateUUID(t)
594 database.MustExec(t, "inserting js book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "js")
595
596 n1UUID := testutils.MustGenerateUUID(t)
597 database.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n1UUID, b1UUID, "n1 content", time.Now().UnixNano())
598 n2UUID := testutils.MustGenerateUUID(t)
599 database.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n2UUID, b1UUID, "n2 content", time.Now().UnixNano())
600 n3UUID := testutils.MustGenerateUUID(t)
601 database.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n3UUID, b1UUID, "n3 content", time.Now().UnixNano())
602
603 data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"})
604 database.MustExec(t, "inserting a1", db,
605 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a1-uuid", 1, "add_book", string(data), 1537829463)
606
607 data = testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: n1UUID, BookName: "css", Content: "n1 content", Public: false})
608 database.MustExec(t, "inserting a2", db,
609 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a2-uuid", 1, "add_note", string(data), 1537829463)
610
611 updatedContent := "updated content"
612 data = testutils.MustMarshalJSON(t, actions.EditNoteDataV3{NoteUUID: n2UUID, BookName: (*string)(nil), Content: &updatedContent, Public: (*bool)(nil)})
613 database.MustExec(t, "inserting a3", db,
614 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a3-uuid", 1, "edit_note", string(data), 1537829463)
615
616 // Execute
617 tx, err := db.Begin()
618 if err != nil {
619 t.Fatal(errors.Wrap(err, "beginning a transaction"))
620 }
621
622 err = lm5.run(ctx, tx)
623 if err != nil {
624 tx.Rollback()
625 t.Fatal(errors.Wrap(err, "failed to run"))
626 }
627
628 tx.Commit()
629
630 // Test
631 var b1Dirty, b2Dirty, n1Dirty, n2Dirty, n3Dirty bool
632 database.MustScan(t, "scanning the newly added dirty flag of b1", db.QueryRow("SELECT dirty FROM books WHERE uuid = ?", b1UUID), &b1Dirty)
633 database.MustScan(t, "scanning the newly added dirty flag of b2", db.QueryRow("SELECT dirty FROM books WHERE uuid = ?", b2UUID), &b2Dirty)
634 database.MustScan(t, "scanning the newly added dirty flag of n1", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n1UUID), &n1Dirty)
635 database.MustScan(t, "scanning the newly added dirty flag of n2", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n2UUID), &n2Dirty)
636 database.MustScan(t, "scanning the newly added dirty flag of n3", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n3UUID), &n3Dirty)
637
638 assert.Equal(t, b1Dirty, false, "b1 dirty flag should be false by default")
639 assert.Equal(t, b2Dirty, true, "b2 dirty flag should be false by default")
640 assert.Equal(t, n1Dirty, true, "n1 dirty flag should be false by default")
641 assert.Equal(t, n2Dirty, true, "n2 dirty flag should be false by default")
642 assert.Equal(t, n3Dirty, false, "n3 dirty flag should be false by default")
643}

Callers

nothing calls this directly

Calls 12

InitTestMemoryDBRawFunction · 0.92
InitTestCtxWithDBFunction · 0.92
MustGenerateUUIDFunction · 0.92
MustExecFunction · 0.92
MustMarshalJSONFunction · 0.92
MustScanFunction · 0.92
EqualFunction · 0.92
NowMethod · 0.65
BeginMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
QueryRowMethod · 0.65

Tested by

no test coverage detected