From 62111df6c513573e2b26f0de605ddc66679f0a08 Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Thu, 13 Feb 2025 20:30:53 +1100 Subject: [PATCH] Added schema.sql and testdata.sql, moved sql out of tests package --- .gitignore | 2 ++ schema.sql | 18 ++++++++++++++ testdata.sql | 1 + tests/database.go | 61 ++++++++++++++++++++++++++++++++++++----------- 4 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 schema.sql create mode 100644 testdata.sql diff --git a/.gitignore b/.gitignore index ec61d27..37839ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .env +query.sql +*.db .logs/ tmp/ projectreshoot diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..3e55fc5 --- /dev/null +++ b/schema.sql @@ -0,0 +1,18 @@ +PRAGMA foreign_keys=ON; +BEGIN TRANSACTION; +CREATE TABLE IF NOT EXISTS jwtblacklist ( +jti TEXT PRIMARY KEY CHECK(jti GLOB '[0-9a-fA-F-]*'), +exp INTEGER NOT NULL +) STRICT; +CREATE TABLE IF NOT EXISTS "users" ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT NOT NULL UNIQUE, + password_hash TEXT, + created_at INTEGER DEFAULT (unixepoch()) +) STRICT; +CREATE TRIGGER cleanup_expired_tokens +AFTER INSERT ON jwtblacklist +BEGIN +DELETE FROM jwtblacklist WHERE exp < strftime('%s', 'now'); +END; +COMMIT; diff --git a/testdata.sql b/testdata.sql new file mode 100644 index 0000000..6b05c97 --- /dev/null +++ b/testdata.sql @@ -0,0 +1 @@ +INSERT INTO users VALUES(1,'testuser','hashedpassword',1738995274); diff --git a/tests/database.go b/tests/database.go index 49b1b98..7c606c5 100644 --- a/tests/database.go +++ b/tests/database.go @@ -2,13 +2,34 @@ package tests import ( "database/sql" + "fmt" "os" + "path/filepath" "github.com/pkg/errors" _ "github.com/mattn/go-sqlite3" ) +func findSQLFile(filename string) (string, error) { + dir, err := os.Getwd() + if err != nil { + return "", err + } + + for { + if _, err := os.Stat(filepath.Join(dir, filename)); err == nil { + return filepath.Join(dir, filename), nil + } + + parent := filepath.Dir(dir) + if parent == dir { // Reached root + return "", errors.New(fmt.Sprintf("Unable to locate %s", filename)) + } + dir = parent + } +} + // SetupTestDB initializes a test SQLite database with mock data // Make sure to call DeleteTestDB when finished to cleanup func SetupTestDB() (*sql.DB, error) { @@ -16,22 +37,34 @@ func SetupTestDB() (*sql.DB, error) { if err != nil { return nil, errors.Wrap(err, "sql.Open") } - // Create the test database - _, err = conn.Exec(` -CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT NOT NULL, - password_hash TEXT, - created_at INTEGER DEFAULT (unixepoch()) -); -INSERT INTO users VALUES(1,'testuser','hashedpassword',1738995274); + // Setup the test database + schemaPath, err := findSQLFile("schema.sql") + if err != nil { + return nil, errors.Wrap(err, "findSchema") + } -CREATE TABLE IF NOT EXISTS jwtblacklist ( - jti TEXT PRIMARY KEY CHECK(jti GLOB '[0-9a-fA-F-]*'), - exp INTEGER NOT NULL -) STRICT; + sqlBytes, err := os.ReadFile(schemaPath) + if err != nil { + return nil, errors.Wrap(err, "os.ReadFile") + } + schemaSQL := string(sqlBytes) - `) + _, err = conn.Exec(schemaSQL) + if err != nil { + return nil, errors.Wrap(err, "conn.Exec") + } + // Load the test data + dataPath, err := findSQLFile("testdata.sql") + if err != nil { + return nil, errors.Wrap(err, "findSchema") + } + sqlBytes, err = os.ReadFile(dataPath) + if err != nil { + return nil, errors.Wrap(err, "os.ReadFile") + } + dataSQL := string(sqlBytes) + + _, err = conn.Exec(dataSQL) if err != nil { return nil, errors.Wrap(err, "conn.Exec") }