chore: update store testing

This commit is contained in:
Steven
2024-01-10 10:08:53 +08:00
parent 4bc2a0ff42
commit 38cd5fabee
17 changed files with 106 additions and 125 deletions

View File

@ -11,8 +11,6 @@ import (
"time"
"github.com/pkg/errors"
// sqlite driver.
_ "modernc.org/sqlite"
"github.com/yourselfhosted/slash/api/auth"
"github.com/yourselfhosted/slash/server"

View File

@ -5,9 +5,7 @@ import (
"fmt"
"testing"
// sqlite driver.
_ "modernc.org/sqlite"
"github.com/yourselfhosted/slash/server/profile"
"github.com/yourselfhosted/slash/store"
"github.com/yourselfhosted/slash/store/db"
test "github.com/yourselfhosted/slash/test"
@ -19,15 +17,29 @@ func NewTestingStore(ctx context.Context, t *testing.T) *store.Store {
if err != nil {
fmt.Printf("failed to create db driver, error: %+v\n", err)
}
resetTestingDB(ctx, profile, dbDriver)
if err := dbDriver.Migrate(ctx); err != nil {
fmt.Printf("failed to migrate db, error: %+v\n", err)
}
if profile.Driver == "postgres" {
if err := dbDriver.Seed(ctx); err != nil {
fmt.Printf("failed to seed db, error: %+v\n", err)
}
}
store := store.New(dbDriver, profile)
return store
}
func resetTestingDB(ctx context.Context, profile *profile.Profile, dbDriver store.Driver) {
if profile.Driver == "postgres" {
_, err := dbDriver.GetDB().ExecContext(ctx, `
DROP TABLE IF EXISTS migration_history CASCADE;
DROP TABLE IF EXISTS workspace_setting CASCADE;
DROP TABLE IF EXISTS "user" CASCADE;
DROP TABLE IF EXISTS user_setting CASCADE;
DROP TABLE IF EXISTS shortcut CASCADE;
DROP TABLE IF EXISTS activity CASCADE;
DROP TABLE IF EXISTS collection CASCADE;
DROP TABLE IF EXISTS memo CASCADE;`)
if err != nil {
fmt.Printf("failed to reset testing db, error: %+v\n", err)
panic(err)
}
}
}

View File

@ -6,6 +6,8 @@ import (
"os"
"testing"
"github.com/joho/godotenv"
"github.com/yourselfhosted/slash/server/profile"
"github.com/yourselfhosted/slash/server/version"
)
@ -24,6 +26,10 @@ func getUnusedPort() int {
}
func GetTestingProfile(t *testing.T) *profile.Profile {
if err := godotenv.Load(".env"); err != nil {
t.Log("failed to load .env file, but it's ok")
}
// Get a temporary directory for the test data.
dir := t.TempDir()
mode := "dev"