mirror of
https://github.com/dnote/dnote
synced 2026-03-14 14:35:50 +01:00
Simplify CLI test db teardown
This commit is contained in:
parent
fea4215c98
commit
5feed29df7
12 changed files with 71 additions and 167 deletions
2
Makefile
2
Makefile
|
|
@ -80,7 +80,7 @@ generate-cli-schema:
|
|||
@echo "==> generating CLI database schema"
|
||||
@mkdir -p pkg/cli/database
|
||||
@touch pkg/cli/database/schema.sql
|
||||
@go run -tags fts5 pkg/cli/database/cmd/generate-schema.go
|
||||
@go run -tags fts5 ./pkg/cli/database/schema
|
||||
.PHONY: generate-cli-schema
|
||||
|
||||
build-cli: generate-cli-schema
|
||||
|
|
|
|||
|
|
@ -36,16 +36,6 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// getTestPaths creates unique test paths for parallel test execution
|
||||
func getTestPaths(t *testing.T) context.Paths {
|
||||
testDir := t.TempDir()
|
||||
return context.Paths{
|
||||
Home: testDir,
|
||||
Cache: testDir,
|
||||
Config: testDir,
|
||||
Data: testDir,
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessFragments(t *testing.T) {
|
||||
fragments := []client.SyncFragment{
|
||||
|
|
@ -1848,8 +1838,7 @@ func TestSaveServerState(t *testing.T) {
|
|||
// are updated accordingly based on the server response.
|
||||
func TestSendBooks(t *testing.T) {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
testutils.Login(t, &ctx)
|
||||
|
||||
db := ctx.DB
|
||||
|
|
@ -2081,9 +2070,8 @@ func TestSendBooks_isBehind(t *testing.T) {
|
|||
for idx, tc := range testCases {
|
||||
func() {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
ctx := context.InitTestCtx(t)
|
||||
ctx.APIEndpoint = ts.URL
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
testutils.Login(t, &ctx)
|
||||
|
||||
db := ctx.DB
|
||||
|
|
@ -2129,9 +2117,8 @@ func TestSendBooks_isBehind(t *testing.T) {
|
|||
for idx, tc := range testCases {
|
||||
func() {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
ctx := context.InitTestCtx(t)
|
||||
ctx.APIEndpoint = ts.URL
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
testutils.Login(t, &ctx)
|
||||
|
||||
db := ctx.DB
|
||||
|
|
@ -2177,9 +2164,8 @@ func TestSendBooks_isBehind(t *testing.T) {
|
|||
for idx, tc := range testCases {
|
||||
func() {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
ctx := context.InitTestCtx(t)
|
||||
ctx.APIEndpoint = ts.URL
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
testutils.Login(t, &ctx)
|
||||
|
||||
db := ctx.DB
|
||||
|
|
@ -2212,8 +2198,7 @@ func TestSendBooks_isBehind(t *testing.T) {
|
|||
// uuid from the incoming data.
|
||||
func TestSendNotes(t *testing.T) {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
testutils.Login(t, &ctx)
|
||||
|
||||
db := ctx.DB
|
||||
|
|
@ -2365,8 +2350,7 @@ func TestSendNotes(t *testing.T) {
|
|||
|
||||
func TestSendNotes_addedOn(t *testing.T) {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
testutils.Login(t, &ctx)
|
||||
|
||||
db := ctx.DB
|
||||
|
|
@ -2497,8 +2481,7 @@ func TestSendNotes_isBehind(t *testing.T) {
|
|||
for idx, tc := range testCases {
|
||||
func() {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
testutils.Login(t, &ctx)
|
||||
ctx.APIEndpoint = ts.URL
|
||||
|
||||
|
|
@ -2546,8 +2529,7 @@ func TestSendNotes_isBehind(t *testing.T) {
|
|||
for idx, tc := range testCases {
|
||||
func() {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
testutils.Login(t, &ctx)
|
||||
ctx.APIEndpoint = ts.URL
|
||||
|
||||
|
|
@ -2595,8 +2577,7 @@ func TestSendNotes_isBehind(t *testing.T) {
|
|||
for idx, tc := range testCases {
|
||||
func() {
|
||||
// set up
|
||||
ctx := context.InitTestCtx(t, getTestPaths(t))
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
testutils.Login(t, &ctx)
|
||||
ctx.APIEndpoint = ts.URL
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,17 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// getDefaultTestPaths creates default test paths with all paths pointing to a temp directory
|
||||
func getDefaultTestPaths(t *testing.T) Paths {
|
||||
tmpDir := t.TempDir()
|
||||
return Paths{
|
||||
Home: tmpDir,
|
||||
Cache: tmpDir,
|
||||
Config: tmpDir,
|
||||
Data: tmpDir,
|
||||
}
|
||||
}
|
||||
|
||||
// createTestDirectories creates test directories for the given paths
|
||||
func createTestDirectories(t *testing.T, paths Paths) {
|
||||
if paths.Config != "" {
|
||||
|
|
@ -52,7 +63,9 @@ func createTestDirectories(t *testing.T, paths Paths) {
|
|||
}
|
||||
|
||||
// InitTestCtx initializes a test context with an in-memory database
|
||||
func InitTestCtx(t *testing.T, paths Paths) DnoteCtx {
|
||||
// and a temporary directory for all paths
|
||||
func InitTestCtx(t *testing.T) DnoteCtx {
|
||||
paths := getDefaultTestPaths(t)
|
||||
db := database.InitTestMemoryDB(t)
|
||||
createTestDirectories(t, paths)
|
||||
|
||||
|
|
@ -63,9 +76,11 @@ func InitTestCtx(t *testing.T, paths Paths) DnoteCtx {
|
|||
}
|
||||
}
|
||||
|
||||
// InitTestCtxWithDB initializes a test context with the provided database.
|
||||
// InitTestCtxWithDB initializes a test context with the provided database
|
||||
// and a temporary directory for all paths.
|
||||
// Used when you need full control over database initialization (e.g. migration tests).
|
||||
func InitTestCtxWithDB(t *testing.T, paths Paths, db *database.DB) DnoteCtx {
|
||||
func InitTestCtxWithDB(t *testing.T, db *database.DB) DnoteCtx {
|
||||
paths := getDefaultTestPaths(t)
|
||||
createTestDirectories(t, paths)
|
||||
|
||||
return DnoteCtx{
|
||||
|
|
@ -76,9 +91,9 @@ func InitTestCtxWithDB(t *testing.T, paths Paths, db *database.DB) DnoteCtx {
|
|||
}
|
||||
|
||||
// InitTestCtxWithFileDB initializes a test context with a file-based database
|
||||
// at the expected XDG path. This is used for e2e tests that spawn CLI processes
|
||||
// which need to access the database file.
|
||||
func InitTestCtxWithFileDB(t *testing.T, paths Paths) DnoteCtx {
|
||||
// at the expected path.
|
||||
func InitTestCtxWithFileDB(t *testing.T) DnoteCtx {
|
||||
paths := getDefaultTestPaths(t)
|
||||
createTestDirectories(t, paths)
|
||||
|
||||
dbPath := filepath.Join(paths.Data, consts.DnoteDirName, consts.DnoteDBFileName)
|
||||
|
|
@ -92,6 +107,7 @@ func InitTestCtxWithFileDB(t *testing.T, paths Paths) DnoteCtx {
|
|||
}
|
||||
|
||||
database.MarkMigrationComplete(t, db)
|
||||
t.Cleanup(func() { db.Close() })
|
||||
|
||||
return DnoteCtx{
|
||||
DB: db,
|
||||
|
|
@ -99,18 +115,3 @@ func InitTestCtxWithFileDB(t *testing.T, paths Paths) DnoteCtx {
|
|||
Clock: clock.NewMock(), // Use a mock clock to test times
|
||||
}
|
||||
}
|
||||
|
||||
// TeardownTestCtx cleans up the test context
|
||||
func TeardownTestCtx(t *testing.T, ctx DnoteCtx) {
|
||||
database.TeardownTestDB(t, ctx.DB)
|
||||
|
||||
if err := os.RemoveAll(ctx.Paths.Data); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "removing test data directory"))
|
||||
}
|
||||
if err := os.RemoveAll(ctx.Paths.Config); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "removing test config directory"))
|
||||
}
|
||||
if err := os.RemoveAll(ctx.Paths.Cache); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "removing test cache directory"))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// generate-schema creates a schema.sql file
|
||||
// Command schema generates the CLI database schema.sql file.
|
||||
package main
|
||||
|
||||
import (
|
||||
|
|
@ -22,7 +22,6 @@ import (
|
|||
"database/sql"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
|
@ -67,8 +66,8 @@ func InitTestMemoryDB(t *testing.T) *DB {
|
|||
// InitTestFileDB initializes a file-based test database with the default schema.
|
||||
func InitTestFileDB(t *testing.T) (*DB, string) {
|
||||
dbPath := filepath.Join(t.TempDir(), "dnote.db")
|
||||
|
||||
return InitTestFileDBRaw(t, dbPath), dbPath
|
||||
db := InitTestFileDBRaw(t, dbPath)
|
||||
return db, dbPath
|
||||
}
|
||||
|
||||
// InitTestFileDBRaw initializes a file-based test database at the specified path with the default schema.
|
||||
|
|
@ -84,6 +83,7 @@ func InitTestFileDBRaw(t *testing.T, dbPath string) *DB {
|
|||
|
||||
MarkMigrationComplete(t, db)
|
||||
|
||||
t.Cleanup(func() { db.Close() })
|
||||
return db
|
||||
}
|
||||
|
||||
|
|
@ -109,20 +109,10 @@ func InitTestMemoryDBRaw(t *testing.T, schemaPath string) *DB {
|
|||
t.Fatal(errors.Wrap(err, "running schema sql"))
|
||||
}
|
||||
|
||||
t.Cleanup(func() { db.Close() })
|
||||
return db
|
||||
}
|
||||
|
||||
// TeardownTestDB closes the test database and removes the its file
|
||||
func TeardownTestDB(t *testing.T, db *DB) {
|
||||
if err := db.Close(); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "closing database"))
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(db.Filepath); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "removing database file"))
|
||||
}
|
||||
}
|
||||
|
||||
// OpenTestDB opens the database connection to a test database
|
||||
// without initializing any schema
|
||||
func OpenTestDB(t *testing.T, dnoteDir string) *DB {
|
||||
|
|
|
|||
|
|
@ -146,7 +146,6 @@ func TestAddNote(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db, dbPath := database.InitTestFileDB(t)
|
||||
defer database.TeardownTestDB(t, db)
|
||||
testutils.Setup3(t, db)
|
||||
|
||||
// Execute
|
||||
|
|
@ -189,7 +188,6 @@ func TestEditNote(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db, dbPath := database.InitTestFileDB(t)
|
||||
defer database.TeardownTestDB(t, db)
|
||||
testutils.Setup4(t, db)
|
||||
|
||||
// Execute
|
||||
|
|
@ -224,7 +222,6 @@ func TestEditNote(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db, dbPath := database.InitTestFileDB(t)
|
||||
defer database.TeardownTestDB(t, db)
|
||||
testutils.Setup5(t, db)
|
||||
|
||||
// Execute
|
||||
|
|
@ -260,7 +257,6 @@ func TestEditNote(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db, dbPath := database.InitTestFileDB(t)
|
||||
defer database.TeardownTestDB(t, db)
|
||||
testutils.Setup5(t, db)
|
||||
|
||||
// Execute
|
||||
|
|
@ -298,7 +294,6 @@ func TestEditBook(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db, dbPath := database.InitTestFileDB(t)
|
||||
defer database.TeardownTestDB(t, db)
|
||||
testutils.Setup1(t, db)
|
||||
|
||||
// Execute
|
||||
|
|
@ -359,7 +354,6 @@ func TestRemoveNote(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db, dbPath := database.InitTestFileDB(t)
|
||||
defer database.TeardownTestDB(t, db)
|
||||
testutils.Setup2(t, db)
|
||||
|
||||
// Execute
|
||||
|
|
@ -448,7 +442,6 @@ func TestRemoveBook(t *testing.T) {
|
|||
|
||||
// Setup
|
||||
db, dbPath := database.InitTestFileDB(t)
|
||||
defer database.TeardownTestDB(t, db)
|
||||
testutils.Setup2(t, db)
|
||||
|
||||
// Execute
|
||||
|
|
|
|||
|
|
@ -360,7 +360,6 @@ func TestMigrateToV8(t *testing.T) {
|
|||
}
|
||||
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql")
|
||||
defer database.TeardownTestDB(t, db)
|
||||
|
||||
ctx := context.DnoteCtx{
|
||||
Paths: context.Paths{
|
||||
|
|
|
|||
|
|
@ -38,18 +38,8 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func getTestPaths(t *testing.T) context.Paths {
|
||||
tmpDir := t.TempDir()
|
||||
return context.Paths{
|
||||
Home: tmpDir,
|
||||
Cache: tmpDir,
|
||||
Config: tmpDir,
|
||||
Data: tmpDir,
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecute_bump_schema(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
testCases := []struct {
|
||||
schemaKey string
|
||||
}{
|
||||
|
|
@ -65,8 +55,7 @@ func TestExecute_bump_schema(t *testing.T) {
|
|||
func() {
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 8)
|
||||
|
||||
|
|
@ -102,7 +91,6 @@ func TestExecute_bump_schema(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRun_nonfresh(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
testCases := []struct {
|
||||
mode int
|
||||
schemaKey string
|
||||
|
|
@ -121,8 +109,7 @@ func TestRun_nonfresh(t *testing.T) {
|
|||
func() {
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 2)
|
||||
database.MustExec(t, "creating a temporary table for testing", db,
|
||||
"CREATE TABLE migrate_run_test ( name string )")
|
||||
|
|
@ -181,7 +168,6 @@ func TestRun_nonfresh(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRun_fresh(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
testCases := []struct {
|
||||
mode int
|
||||
schemaKey string
|
||||
|
|
@ -200,8 +186,7 @@ func TestRun_fresh(t *testing.T) {
|
|||
func() {
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
database.MustExec(t, "creating a temporary table for testing", db,
|
||||
"CREATE TABLE migrate_run_test ( name string )")
|
||||
|
|
@ -254,7 +239,6 @@ func TestRun_fresh(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRun_up_to_date(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
testCases := []struct {
|
||||
mode int
|
||||
schemaKey string
|
||||
|
|
@ -273,8 +257,7 @@ func TestRun_up_to_date(t *testing.T) {
|
|||
func() {
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
database.MustExec(t, "creating a temporary table for testing", db,
|
||||
"CREATE TABLE migrate_run_test ( name string )")
|
||||
|
|
@ -324,11 +307,9 @@ func TestRun_up_to_date(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration1(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"})
|
||||
a1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting action", db,
|
||||
|
|
@ -401,11 +382,9 @@ func TestLocalMigration1(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration2(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
c1 := "note 1 - v1"
|
||||
c2 := "note 1 - v2"
|
||||
css := "css"
|
||||
|
|
@ -487,11 +466,9 @@ func TestLocalMigration2(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration3(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
data := testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: "note-1-uuid", BookName: "js", Content: "note 1", Public: false})
|
||||
a1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting action", db,
|
||||
|
|
@ -561,11 +538,9 @@ func TestLocalMigration3(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration4(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css")
|
||||
|
|
@ -604,11 +579,9 @@ func TestLocalMigration4(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration5(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-5-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css")
|
||||
|
|
@ -665,11 +638,9 @@ func TestLocalMigration5(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration6(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-5-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"})
|
||||
a1UUID := testutils.MustGenerateUUID(t)
|
||||
|
|
@ -697,11 +668,9 @@ func TestLocalMigration6(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration7_trash(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting trash book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "trash")
|
||||
|
|
@ -729,11 +698,9 @@ func TestLocalMigration7_trash(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration7_conflicts(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts")
|
||||
|
|
@ -761,11 +728,9 @@ func TestLocalMigration7_conflicts(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration7_conflicts_dup(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts")
|
||||
|
|
@ -798,11 +763,9 @@ func TestLocalMigration7_conflicts_dup(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration8(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-8-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1")
|
||||
|
|
@ -863,11 +826,9 @@ func TestLocalMigration8(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration9(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-9-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1")
|
||||
|
|
@ -908,11 +869,9 @@ func TestLocalMigration9(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration10(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-10-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting book ", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "123")
|
||||
|
|
@ -979,11 +938,9 @@ func TestLocalMigration10(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration11(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-11-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "foo")
|
||||
|
|
@ -1058,11 +1015,9 @@ func TestLocalMigration11(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration12(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-12-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
data := []byte("editor: vim")
|
||||
path := fmt.Sprintf("%s/%s/dnoterc", ctx.Paths.Config, consts.DnoteDirName)
|
||||
|
|
@ -1096,11 +1051,9 @@ func TestLocalMigration12(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration13(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-12-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
data := []byte("editor: vim\napiEndpoint: https://test.com/api")
|
||||
|
||||
|
|
@ -1139,11 +1092,9 @@ func TestLocalMigration13(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocalMigration14(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-14-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
|
||||
b1UUID := testutils.MustGenerateUUID(t)
|
||||
database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1")
|
||||
|
|
@ -1188,11 +1139,9 @@ func TestLocalMigration14(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRemoteMigration1(t *testing.T) {
|
||||
paths := getTestPaths(t)
|
||||
// set up
|
||||
db := database.InitTestMemoryDBRaw(t, "./fixtures/remote-1-pre-schema.sql")
|
||||
ctx := context.InitTestCtxWithDB(t, paths, db)
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtxWithDB(t, db)
|
||||
testutils.Login(t, &ctx)
|
||||
|
||||
JSBookUUID := "existing-js-book-uuid"
|
||||
|
|
|
|||
|
|
@ -30,12 +30,7 @@ import (
|
|||
|
||||
func TestGetTmpContentPath(t *testing.T) {
|
||||
t.Run("no collision", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
ctx := context.InitTestCtx(t, context.Paths{
|
||||
Data: tmpDir,
|
||||
Cache: tmpDir,
|
||||
})
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
|
||||
res, err := GetTmpContentPath(ctx)
|
||||
if err != nil {
|
||||
|
|
@ -48,12 +43,7 @@ func TestGetTmpContentPath(t *testing.T) {
|
|||
|
||||
t.Run("one existing session", func(t *testing.T) {
|
||||
// set up
|
||||
tmpDir := t.TempDir()
|
||||
ctx := context.InitTestCtx(t, context.Paths{
|
||||
Data: tmpDir,
|
||||
Cache: tmpDir,
|
||||
})
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
|
||||
p := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md")
|
||||
if _, err := os.Create(p); err != nil {
|
||||
|
|
@ -73,12 +63,7 @@ func TestGetTmpContentPath(t *testing.T) {
|
|||
|
||||
t.Run("two existing sessions", func(t *testing.T) {
|
||||
// set up
|
||||
tmpDir := t.TempDir()
|
||||
ctx := context.InitTestCtx(t, context.Paths{
|
||||
Data: tmpDir,
|
||||
Cache: tmpDir,
|
||||
})
|
||||
defer context.TeardownTestCtx(t, ctx)
|
||||
ctx := context.InitTestCtx(t)
|
||||
|
||||
p1 := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md")
|
||||
if _, err := os.Create(p1); err != nil {
|
||||
|
|
|
|||
|
|
@ -20,11 +20,10 @@
|
|||
package clock
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//TODO: use mutex to avoid race
|
||||
|
||||
// Clock is an interface to the standard library time.
|
||||
// It is used to implement a real or a mock clock. The latter is used in tests.
|
||||
type Clock interface {
|
||||
|
|
@ -39,16 +38,21 @@ func (c *clock) Now() time.Time {
|
|||
|
||||
// Mock is a mock instance of clock
|
||||
type Mock struct {
|
||||
mu sync.RWMutex
|
||||
currentTime time.Time
|
||||
}
|
||||
|
||||
// SetNow sets the current time for the mock clock
|
||||
func (c *Mock) SetNow(t time.Time) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.currentTime = t
|
||||
}
|
||||
|
||||
// Now returns the current time
|
||||
func (c *Mock) Now() time.Time {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.currentTime
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,8 +33,6 @@ import (
|
|||
|
||||
func TestCreateNote(t *testing.T) {
|
||||
serverTime := time.Date(2017, time.March, 14, 21, 15, 0, 0, time.UTC)
|
||||
mockClock := clock.NewMock()
|
||||
mockClock.SetNow(serverTime)
|
||||
|
||||
ts1 := time.Date(2018, time.November, 12, 10, 11, 0, 0, time.UTC).UnixNano()
|
||||
ts2 := time.Date(2018, time.November, 15, 0, 1, 10, 0, time.UTC).UnixNano()
|
||||
|
|
@ -75,6 +73,10 @@ func TestCreateNote(t *testing.T) {
|
|||
|
||||
for idx, tc := range testCases {
|
||||
func() {
|
||||
// Create a new clock for each test case to avoid race conditions in parallel tests
|
||||
mockClock := clock.NewMock()
|
||||
mockClock.SetNow(serverTime)
|
||||
|
||||
db := testutils.InitMemoryDB(t)
|
||||
|
||||
user := testutils.SetupUserData(db, "user@test.com", "password123")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue