mirror of
https://github.com/aykhans/slash-e.git
synced 2026-09-23 14:26:49 +00:00
chore: update store tests
This commit is contained in:
@@ -3,16 +3,20 @@ package teststore
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
|
"github.com/yourselfhosted/slash/server/common"
|
||||||
"github.com/yourselfhosted/slash/server/profile"
|
"github.com/yourselfhosted/slash/server/profile"
|
||||||
"github.com/yourselfhosted/slash/store"
|
"github.com/yourselfhosted/slash/store"
|
||||||
"github.com/yourselfhosted/slash/store/db"
|
"github.com/yourselfhosted/slash/store/db"
|
||||||
"github.com/yourselfhosted/slash/test"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewTestingStore(ctx context.Context, t *testing.T) *store.Store {
|
func NewTestingStore(ctx context.Context, t *testing.T) *store.Store {
|
||||||
profile := test.GetTestingProfile(t)
|
profile := getTestingProfile(t)
|
||||||
dbDriver, err := db.NewDBDriver(profile)
|
dbDriver, err := db.NewDBDriver(profile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to create db driver, error: %+v\n", err)
|
fmt.Printf("failed to create db driver, error: %+v\n", err)
|
||||||
@@ -41,3 +45,48 @@ func resetTestingDB(ctx context.Context, profile *profile.Profile, dbDriver stor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getUnusedPort() int {
|
||||||
|
// Get a random unused port
|
||||||
|
listener, err := net.Listen("tcp", "localhost:0")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
|
||||||
|
// Get the port number
|
||||||
|
port := listener.Addr().(*net.TCPAddr).Port
|
||||||
|
return port
|
||||||
|
}
|
||||||
|
|
||||||
|
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 := "prod"
|
||||||
|
port := getUnusedPort()
|
||||||
|
driver := getDriverFromEnv()
|
||||||
|
dsn := os.Getenv("DSN")
|
||||||
|
if driver == "sqlite" {
|
||||||
|
dsn = fmt.Sprintf("%s/slash_%s.db", dir, mode)
|
||||||
|
}
|
||||||
|
return &profile.Profile{
|
||||||
|
Mode: mode,
|
||||||
|
Port: port,
|
||||||
|
Data: dir,
|
||||||
|
DSN: dsn,
|
||||||
|
Driver: driver,
|
||||||
|
Version: common.GetCurrentVersion(mode),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDriverFromEnv() string {
|
||||||
|
driver := os.Getenv("DRIVER")
|
||||||
|
if driver == "" {
|
||||||
|
driver = "sqlite"
|
||||||
|
}
|
||||||
|
return driver
|
||||||
|
}
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
|
|
||||||
"github.com/yourselfhosted/slash/server/common"
|
|
||||||
"github.com/yourselfhosted/slash/server/profile"
|
|
||||||
)
|
|
||||||
|
|
||||||
func getUnusedPort() int {
|
|
||||||
// Get a random unused port
|
|
||||||
listener, err := net.Listen("tcp", "localhost:0")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
|
|
||||||
// Get the port number
|
|
||||||
port := listener.Addr().(*net.TCPAddr).Port
|
|
||||||
return port
|
|
||||||
}
|
|
||||||
|
|
||||||
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 := "prod"
|
|
||||||
port := getUnusedPort()
|
|
||||||
driver := getDriverFromEnv()
|
|
||||||
dsn := os.Getenv("DSN")
|
|
||||||
if driver == "sqlite" {
|
|
||||||
dsn = fmt.Sprintf("%s/slash_%s.db", dir, mode)
|
|
||||||
}
|
|
||||||
return &profile.Profile{
|
|
||||||
Mode: mode,
|
|
||||||
Port: port,
|
|
||||||
Data: dir,
|
|
||||||
DSN: dsn,
|
|
||||||
Driver: driver,
|
|
||||||
Version: common.GetCurrentVersion(mode),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDriverFromEnv() string {
|
|
||||||
driver := os.Getenv("DRIVER")
|
|
||||||
if driver == "" {
|
|
||||||
driver = "sqlite"
|
|
||||||
}
|
|
||||||
return driver
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user