refactor: changed file structure

This commit is contained in:
2025-03-05 20:18:28 +11:00
parent 5c1089e0ce
commit 1d9af44d0a
137 changed files with 4986 additions and 581 deletions

18
pkg/tests/config.go Normal file
View File

@@ -0,0 +1,18 @@
package tests
import (
"os"
"projectreshoot/pkg/config"
"github.com/pkg/errors"
)
func TestConfig() (*config.Config, error) {
os.Setenv("SECRET_KEY", ".")
os.Setenv("TMDB_API_TOKEN", ".")
cfg, err := config.GetConfig(map[string]string{})
if err != nil {
return nil, errors.Wrap(err, "config.GetConfig")
}
return cfg, nil
}

116
pkg/tests/database.go Normal file
View File

@@ -0,0 +1,116 @@
package tests
import (
"context"
"database/sql"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/pressly/goose/v3"
_ "modernc.org/sqlite"
)
func findMigrations() (*fs.FS, error) {
dir, err := os.Getwd()
if err != nil {
return nil, err
}
for {
if _, err := os.Stat(filepath.Join(dir, "Makefile")); err == nil {
migrationsdir := os.DirFS(filepath.Join(dir, "cmd", "migrate", "migrations"))
return &migrationsdir, nil
}
parent := filepath.Dir(dir)
if parent == dir { // Reached root
return nil, errors.New("Unable to locate migrations directory")
}
dir = parent
}
}
func findTestData() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
for {
if _, err := os.Stat(filepath.Join(dir, "Makefile")); err == nil {
return filepath.Join(dir, "pkg", "tests", "testdata.sql"), nil
}
parent := filepath.Dir(dir)
if parent == dir { // Reached root
return "", errors.New("Unable to locate test data")
}
dir = parent
}
}
func migrateTestDB(wconn *sql.DB, version int64) error {
migrations, err := findMigrations()
if err != nil {
return errors.Wrap(err, "findMigrations")
}
provider, err := goose.NewProvider(goose.DialectSQLite3, wconn, *migrations)
if err != nil {
return errors.Wrap(err, "goose.NewProvider")
}
ctx := context.Background()
if _, err := provider.UpTo(ctx, version); err != nil {
return errors.Wrap(err, "provider.UpTo")
}
return nil
}
func loadTestData(wconn *sql.DB) error {
dataPath, err := findTestData()
if err != nil {
return errors.Wrap(err, "findSchema")
}
sqlBytes, err := os.ReadFile(dataPath)
if err != nil {
return errors.Wrap(err, "os.ReadFile")
}
dataSQL := string(sqlBytes)
_, err = wconn.Exec(dataSQL)
if err != nil {
return errors.Wrap(err, "tx.Exec")
}
return nil
}
// Returns two db connection handles. First is a readwrite connection, second
// is a read only connection
func SetupTestDB(version int64) (*sql.DB, *sql.DB, error) {
opts := "_journal_mode=WAL&_synchronous=NORMAL&_txlock=IMMEDIATE"
file := fmt.Sprintf("file::memory:?cache=shared&%s", opts)
wconn, err := sql.Open("sqlite", file)
if err != nil {
return nil, nil, errors.Wrap(err, "sql.Open")
}
err = migrateTestDB(wconn, version)
if err != nil {
return nil, nil, errors.Wrap(err, "migrateTestDB")
}
err = loadTestData(wconn)
if err != nil {
return nil, nil, errors.Wrap(err, "loadTestData")
}
opts = "_synchronous=NORMAL&mode=ro"
file = fmt.Sprintf("file::memory:?cache=shared&%s", opts)
rconn, err := sql.Open("sqlite", file)
if err != nil {
return nil, nil, errors.Wrap(err, "sql.Open")
}
return wconn, rconn, nil
}

33
pkg/tests/logger.go Normal file
View File

@@ -0,0 +1,33 @@
package tests
import (
"testing"
"github.com/rs/zerolog"
)
type TLogWriter struct {
t *testing.T
}
// Write implements the io.Writer interface for TLogWriter.
func (w *TLogWriter) Write(p []byte) (n int, err error) {
w.t.Logf("%s", p)
return len(p), nil
}
// Return a fake logger to satisfy functions that expect one
func NilLogger() *zerolog.Logger {
logger := zerolog.New(nil)
return &logger
}
// Return a logger that makes use of the T.Log method to enable debugging tests
func DebugLogger(t *testing.T) *zerolog.Logger {
logger := zerolog.New(GetTLogWriter(t))
return &logger
}
func GetTLogWriter(t *testing.T) *TLogWriter {
return &TLogWriter{t: t}
}

3
pkg/tests/testdata.sql Normal file
View File

@@ -0,0 +1,3 @@
INSERT INTO users VALUES(1,'testuser','hashedpassword',1738995274, 'bio');
INSERT INTO jwtblacklist VALUES('0a6b338e-930a-43fe-8f70-1a6daed256fa', 33299675344);
INSERT INTO jwtblacklist VALUES('b7fa51dc-8532-42e1-8756-5d25bfb2003a', 33299675344);