mirror of
https://github.com/dnote/dnote
synced 2026-03-18 00:09:56 +01:00
Restructure packages to reduce duplication
This commit is contained in:
parent
5feed29df7
commit
d476fa02f8
15 changed files with 228 additions and 131 deletions
|
|
@ -110,7 +110,6 @@ func TestNoteInsert(t *testing.T) {
|
|||
func() {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
n := Note{
|
||||
UUID: tc.uuid,
|
||||
|
|
@ -244,7 +243,6 @@ func TestNoteUpdate(t *testing.T) {
|
|||
func() {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
n1 := Note{
|
||||
UUID: tc.uuid,
|
||||
|
|
@ -267,8 +265,8 @@ func TestNoteUpdate(t *testing.T) {
|
|||
Dirty: false,
|
||||
}
|
||||
|
||||
MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty)
|
||||
MustExec(t, fmt.Sprintf("inserting n2 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty)
|
||||
MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty)
|
||||
MustExec(t, fmt.Sprintf("inserting n2 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty)
|
||||
|
||||
// execute
|
||||
tx, err := db.Begin()
|
||||
|
|
@ -336,7 +334,6 @@ func TestNoteUpdateUUID(t *testing.T) {
|
|||
t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
n1 := Note{
|
||||
UUID: "n1-uuid",
|
||||
|
|
@ -391,7 +388,6 @@ func TestNoteUpdateUUID(t *testing.T) {
|
|||
func TestNoteExpunge(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
n1 := Note{
|
||||
UUID: "n1-uuid",
|
||||
|
|
@ -414,8 +410,8 @@ func TestNoteExpunge(t *testing.T) {
|
|||
Dirty: false,
|
||||
}
|
||||
|
||||
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty)
|
||||
MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty)
|
||||
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty)
|
||||
MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty)
|
||||
|
||||
// execute
|
||||
tx, err := db.Begin()
|
||||
|
|
@ -514,7 +510,6 @@ func TestBookInsert(t *testing.T) {
|
|||
func() {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
b := Book{
|
||||
UUID: tc.uuid,
|
||||
|
|
@ -595,7 +590,6 @@ func TestBookUpdate(t *testing.T) {
|
|||
func() {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
b1 := Book{
|
||||
UUID: "b1-uuid",
|
||||
|
|
@ -674,7 +668,6 @@ func TestBookUpdateUUID(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
b1 := Book{
|
||||
UUID: "b1-uuid",
|
||||
|
|
@ -725,7 +718,6 @@ func TestBookUpdateUUID(t *testing.T) {
|
|||
func TestBookExpunge(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
b1 := Book{
|
||||
UUID: "b1-uuid",
|
||||
|
|
@ -780,7 +772,6 @@ func TestBookExpunge(t *testing.T) {
|
|||
func TestNoteFTS(t *testing.T) {
|
||||
// set up
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
// execute - insert
|
||||
n := Note{
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@ func TestInsertSystem(t *testing.T) {
|
|||
t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
// execute
|
||||
tx, err := db.Begin()
|
||||
|
|
@ -96,7 +95,6 @@ func TestUpsertSystem(t *testing.T) {
|
|||
t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz")
|
||||
|
||||
|
|
@ -135,7 +133,6 @@ func TestGetSystem(t *testing.T) {
|
|||
t.Run(fmt.Sprintf("get string value"), func(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
// execute
|
||||
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "bar")
|
||||
|
|
@ -158,7 +155,6 @@ func TestGetSystem(t *testing.T) {
|
|||
t.Run(fmt.Sprintf("get int64 value"), func(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
// execute
|
||||
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", 1234)
|
||||
|
|
@ -199,7 +195,6 @@ func TestUpdateSystem(t *testing.T) {
|
|||
t.Run(fmt.Sprintf("update %s %s", tc.key, tc.val), func(t *testing.T) {
|
||||
// Setup
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "fuz")
|
||||
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz")
|
||||
|
|
@ -239,7 +234,6 @@ func TestGetActiveNote(t *testing.T) {
|
|||
t.Run("not deleted", func(t *testing.T) {
|
||||
// set up
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
n1UUID := "n1-uuid"
|
||||
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, false, true)
|
||||
|
|
@ -268,7 +262,6 @@ func TestGetActiveNote(t *testing.T) {
|
|||
t.Run("deleted", func(t *testing.T) {
|
||||
// set up
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
n1UUID := "n1-uuid"
|
||||
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, true, true)
|
||||
|
|
@ -292,7 +285,6 @@ func TestGetActiveNote(t *testing.T) {
|
|||
func TestUpdateNoteContent(t *testing.T) {
|
||||
// set up
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
uuid := "n1-uuid"
|
||||
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", uuid, "b1-uuid", "n1 content", 1542058875, 0, 1, false, false)
|
||||
|
|
@ -324,7 +316,6 @@ func TestUpdateNoteContent(t *testing.T) {
|
|||
func TestUpdateNoteBook(t *testing.T) {
|
||||
// set up
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
b1UUID := "b1-uuid"
|
||||
b2UUID := "b2-uuid"
|
||||
|
|
@ -361,7 +352,6 @@ func TestUpdateNoteBook(t *testing.T) {
|
|||
func TestUpdateBookName(t *testing.T) {
|
||||
// set up
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
b1UUID := "b1-uuid"
|
||||
MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 8, false, false)
|
||||
|
|
|
|||
|
|
@ -34,3 +34,7 @@ CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN
|
|||
INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body);
|
||||
INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body);
|
||||
END;
|
||||
|
||||
-- Migration version data.
|
||||
INSERT INTO system (key, value) VALUES ('schema', 14);
|
||||
INSERT INTO system (key, value) VALUES ('remote_schema', 1);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/dnote/dnote/pkg/cli/config"
|
||||
"github.com/dnote/dnote/pkg/cli/consts"
|
||||
"github.com/dnote/dnote/pkg/cli/context"
|
||||
"github.com/dnote/dnote/pkg/cli/database"
|
||||
"github.com/dnote/dnote/pkg/cli/infra"
|
||||
|
|
@ -118,7 +119,12 @@ func generateSchema(tmpDir string) (string, error) {
|
|||
return "", fmt.Errorf("extracting schema: %w", err)
|
||||
}
|
||||
|
||||
return schema, nil
|
||||
// Add INSERT statements for migration versions.
|
||||
systemData := "\n-- Migration version data.\n"
|
||||
systemData += fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s', %d);\n", consts.SystemSchema, len(migrate.LocalSequence))
|
||||
systemData += fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s', %d);\n", consts.SystemRemoteSchema, len(migrate.RemoteSequence))
|
||||
|
||||
return schema + systemData, nil
|
||||
}
|
||||
|
||||
// extractSchema extracts the complete schema by querying sqlite_master
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/dnote/dnote/pkg/assert"
|
||||
"github.com/dnote/dnote/pkg/cli/consts"
|
||||
)
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
|
|
@ -73,4 +74,11 @@ func TestRun(t *testing.T) {
|
|||
|
||||
// Verify schema does not contain sqlite internal tables
|
||||
assert.Equal(t, strings.Contains(schema, "sqlite_sequence"), false, "schema should not contain sqlite_sequence")
|
||||
|
||||
// Verify system key-value pairs for schema versions are present
|
||||
expectedSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemSchema)
|
||||
assert.Equal(t, strings.Contains(schema, expectedSchemaKey), true, "schema should contain schema version INSERT statement")
|
||||
|
||||
expectedRemoteSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemRemoteSchema)
|
||||
assert.Equal(t, strings.Contains(schema, expectedRemoteSchemaKey), true, "schema should contain remote_schema version INSERT statement")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +0,0 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSchemaEmbed(t *testing.T) {
|
||||
db := InitTestMemoryDB(t)
|
||||
defer db.Close()
|
||||
|
||||
// Try to insert a book to verify schema is loaded
|
||||
_, err := db.Exec("INSERT INTO books (uuid, label) VALUES (?, ?)", "test-uuid", "test-label")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to insert into books: %v", err)
|
||||
}
|
||||
|
||||
// Verify it was inserted
|
||||
var label string
|
||||
err = db.QueryRow("SELECT label FROM books WHERE uuid = ?", "test-uuid").Scan(&label)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to query books: %v", err)
|
||||
}
|
||||
if label != "test-label" {
|
||||
t.Fatalf("Expected label 'test-label', got '%s'", label)
|
||||
}
|
||||
t.Log("Schema embed test passed!")
|
||||
}
|
||||
|
|
@ -58,9 +58,7 @@ func MustExec(t *testing.T, message string, db *DB, query string, args ...interf
|
|||
|
||||
// InitTestMemoryDB initializes an in-memory test database with the default schema.
|
||||
func InitTestMemoryDB(t *testing.T) *DB {
|
||||
db := InitTestMemoryDBRaw(t, "")
|
||||
MarkMigrationComplete(t, db)
|
||||
return db
|
||||
return InitTestMemoryDBRaw(t, "")
|
||||
}
|
||||
|
||||
// InitTestFileDB initializes a file-based test database with the default schema.
|
||||
|
|
@ -81,8 +79,6 @@ func InitTestFileDBRaw(t *testing.T, dbPath string) *DB {
|
|||
t.Fatal(errors.Wrap(err, "running schema sql"))
|
||||
}
|
||||
|
||||
MarkMigrationComplete(t, db)
|
||||
|
||||
t.Cleanup(func() { db.Close() })
|
||||
return db
|
||||
}
|
||||
|
|
@ -125,16 +121,6 @@ func OpenTestDB(t *testing.T, dnoteDir string) *DB {
|
|||
return db
|
||||
}
|
||||
|
||||
// MarkMigrationComplete marks all migrations as complete in the database
|
||||
func MarkMigrationComplete(t *testing.T, db *DB) {
|
||||
if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", consts.SystemSchema, 14); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "inserting schema"))
|
||||
}
|
||||
if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", consts.SystemRemoteSchema, 1); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "inserting remote schema"))
|
||||
}
|
||||
}
|
||||
|
||||
// mustGenerateTestUUID generates a UUID for test databases and fails the test on error
|
||||
func mustGenerateTestUUID(t *testing.T) string {
|
||||
uuid, err := utils.GenerateUUID()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue