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
|
|
|
// mysql driver.
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
// sqlite driver.
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
|
2023-04-01 22:47:19 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2023-09-29 09:15:54 +08:00
|
|
|
"github.com/usememos/memos/store/mysql"
|
2023-09-26 19:15:18 +08:00
|
|
|
"github.com/usememos/memos/store/sqlite"
|
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-09-29 09:15:54 +08:00
|
|
|
var driver store.Driver
|
|
|
|
var err error
|
|
|
|
switch profile.Driver {
|
|
|
|
case "sqlite":
|
|
|
|
driver, err = sqlite.NewDriver(profile)
|
|
|
|
case "mysql":
|
|
|
|
driver, err = mysql.NewDriver(profile)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown db driver: %s", profile.Driver))
|
|
|
|
}
|
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
|
|
|
}
|
2023-09-27 11:56:20 +08:00
|
|
|
if err := driver.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-09-27 09:27:31 +08:00
|
|
|
store := store.New(driver, profile)
|
2023-04-01 22:47:19 +08:00
|
|
|
return store
|
|
|
|
}
|