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
|
.env
|
||||||
|
query.sql
|
||||||
|
*.db
|
||||||
.logs/
|
.logs/
|
||||||
tmp/
|
tmp/
|
||||||
projectreshoot
|
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 (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "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
|
// SetupTestDB initializes a test SQLite database with mock data
|
||||||
// Make sure to call DeleteTestDB when finished to cleanup
|
// Make sure to call DeleteTestDB when finished to cleanup
|
||||||
func SetupTestDB() (*sql.DB, error) {
|
func SetupTestDB() (*sql.DB, error) {
|
||||||
@@ -16,22 +37,34 @@ func SetupTestDB() (*sql.DB, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "sql.Open")
|
return nil, errors.Wrap(err, "sql.Open")
|
||||||
}
|
}
|
||||||
// Create the test database
|
// Setup the test database
|
||||||
_, err = conn.Exec(`
|
schemaPath, err := findSQLFile("schema.sql")
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
if err != nil {
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
return nil, errors.Wrap(err, "findSchema")
|
||||||
username TEXT NOT NULL,
|
}
|
||||||
password_hash TEXT,
|
|
||||||
created_at INTEGER DEFAULT (unixepoch())
|
|
||||||
);
|
|
||||||
INSERT INTO users VALUES(1,'testuser','hashedpassword',1738995274);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS jwtblacklist (
|
sqlBytes, err := os.ReadFile(schemaPath)
|
||||||
jti TEXT PRIMARY KEY CHECK(jti GLOB '[0-9a-fA-F-]*'),
|
if err != nil {
|
||||||
exp INTEGER NOT NULL
|
return nil, errors.Wrap(err, "os.ReadFile")
|
||||||
) STRICT;
|
}
|
||||||
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "conn.Exec")
|
return nil, errors.Wrap(err, "conn.Exec")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user