Update authentication, reauth, logout to use new transactions

This commit is contained in:
2025-02-17 18:58:34 +11:00
parent 417daf0028
commit 2c61cec55c
17 changed files with 306 additions and 121 deletions

View File

@@ -1,10 +1,12 @@
package tests
import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"projectreshoot/db"
"github.com/pkg/errors"
@@ -32,11 +34,16 @@ func findSQLFile(filename string) (string, error) {
// SetupTestDB initializes a test SQLite database with mock data
// Make sure to call DeleteTestDB when finished to cleanup
func SetupTestDB() (*sql.DB, error) {
conn, err := sql.Open("sqlite3", "file:.projectreshoot-test-database.db")
func SetupTestDB(ctx context.Context) (*db.SafeConn, error) {
dbfile, err := sql.Open("sqlite3", "file:.projectreshoot-test-database.db")
if err != nil {
return nil, errors.Wrap(err, "sql.Open")
}
conn := db.MakeSafe(dbfile)
tx, err := conn.Begin(ctx)
if err != nil {
return nil, errors.Wrap(err, "conn.Begin")
}
// Setup the test database
schemaPath, err := findSQLFile("schema.sql")
if err != nil {
@@ -49,9 +56,10 @@ func SetupTestDB() (*sql.DB, error) {
}
schemaSQL := string(sqlBytes)
_, err = conn.Exec(schemaSQL)
_, err = tx.Exec(ctx, schemaSQL)
if err != nil {
return nil, errors.Wrap(err, "conn.Exec")
tx.Rollback()
return nil, errors.Wrap(err, "tx.Exec")
}
// Load the test data
dataPath, err := findSQLFile("testdata.sql")
@@ -64,10 +72,12 @@ func SetupTestDB() (*sql.DB, error) {
}
dataSQL := string(sqlBytes)
_, err = conn.Exec(dataSQL)
_, err = tx.Exec(ctx, dataSQL)
if err != nil {
return nil, errors.Wrap(err, "conn.Exec")
tx.Rollback()
return nil, errors.Wrap(err, "tx.Exec")
}
tx.Commit()
return conn, nil
}