2023-04-03 09:53:36 +08:00
|
|
|
package teststore
|
2023-04-01 22:47:19 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2023-09-29 09:15:54 +08:00
|
|
|
// sqlite driver.
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2023-04-01 22:47:19 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2023-10-05 23:11:29 +08:00
|
|
|
"github.com/usememos/memos/store/db"
|
2023-04-01 22:47:19 +08:00
|
|
|
"github.com/usememos/memos/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewTestingStore(ctx context.Context, t *testing.T) *store.Store {
|
|
|
|
profile := test.GetTestingProfile(t)
|
2023-10-05 23:11:29 +08:00
|
|
|
dbDriver, err := db.NewDBDriver(profile)
|
2023-09-27 11:56:20 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to create db driver, error: %+v\n", err)
|
2023-04-01 22:47:19 +08:00
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
resetTestingDB(ctx, profile, dbDriver)
|
2023-10-05 23:11:29 +08:00
|
|
|
if err := dbDriver.Migrate(ctx); err != nil {
|
2023-08-26 07:33:45 +08:00
|
|
|
fmt.Printf("failed to migrate db, error: %+v\n", err)
|
|
|
|
}
|
2023-04-01 22:47:19 +08:00
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
store := store.New(dbDriver, profile)
|
2023-04-01 22:47:19 +08:00
|
|
|
return store
|
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
|
|
|
|
func resetTestingDB(ctx context.Context, profile *profile.Profile, dbDriver store.Driver) {
|
|
|
|
if profile.Driver == "postgres" {
|
|
|
|
_, err := dbDriver.GetDB().ExecContext(ctx, `DROP SCHEMA public CASCADE; CREATE SCHEMA public;`)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to reset testing db, error: %+v\n", err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|