Added schema.sql and testdata.sql, moved sql out of tests package
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,6 @@
|
||||
.env
|
||||
query.sql
|
||||
*.db
|
||||
.logs/
|
||||
tmp/
|
||||
projectreshoot
|
||||
|
||||
18
schema.sql
Normal file
18
schema.sql
Normal file
@@ -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;
|
||||
1
testdata.sql
Normal file
1
testdata.sql
Normal file
@@ -0,0 +1 @@
|
||||
INSERT INTO users VALUES(1,'testuser','hashedpassword',1738995274);
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user