Skip to content

Commit feb952f

Browse files
committed
chore: update store tests
1 parent cb1413b commit feb952f

File tree

9 files changed

+51
-60
lines changed

9 files changed

+51
-60
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/store/store.go renamed to store/test/store.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package teststore
33
import (
44
"context"
55
"fmt"
6+
"net"
7+
"os"
68
"testing"
79

10+
"github.com/joho/godotenv"
11+
12+
"github.com/yourselfhosted/slash/server/common"
813
"github.com/yourselfhosted/slash/server/profile"
914
"github.com/yourselfhosted/slash/store"
1015
"github.com/yourselfhosted/slash/store/db"
11-
"github.com/yourselfhosted/slash/test"
1216
)
1317

1418
func NewTestingStore(ctx context.Context, t *testing.T) *store.Store {
15-
profile := test.GetTestingProfile(t)
19+
profile := getTestingProfile(t)
1620
dbDriver, err := db.NewDBDriver(profile)
1721
if err != nil {
1822
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
4145
}
4246
}
4347
}
48+
49+
func getUnusedPort() int {
50+
// Get a random unused port
51+
listener, err := net.Listen("tcp", "localhost:0")
52+
if err != nil {
53+
panic(err)
54+
}
55+
defer listener.Close()
56+
57+
// Get the port number
58+
port := listener.Addr().(*net.TCPAddr).Port
59+
return port
60+
}
61+
62+
func getTestingProfile(t *testing.T) *profile.Profile {
63+
if err := godotenv.Load(".env"); err != nil {
64+
t.Log("failed to load .env file, but it's ok")
65+
}
66+
67+
// Get a temporary directory for the test data.
68+
dir := t.TempDir()
69+
mode := "prod"
70+
port := getUnusedPort()
71+
driver := getDriverFromEnv()
72+
dsn := os.Getenv("DSN")
73+
if driver == "sqlite" {
74+
dsn = fmt.Sprintf("%s/slash_%s.db", dir, mode)
75+
}
76+
return &profile.Profile{
77+
Mode: mode,
78+
Port: port,
79+
Data: dir,
80+
DSN: dsn,
81+
Driver: driver,
82+
Version: common.GetCurrentVersion(mode),
83+
}
84+
}
85+
86+
func getDriverFromEnv() string {
87+
driver := os.Getenv("DRIVER")
88+
if driver == "" {
89+
driver = "sqlite"
90+
}
91+
return driver
92+
}
File renamed without changes.
File renamed without changes.

test/test.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)