stardb/testing/testing.go

48 lines
1.1 KiB
Go
Raw Permalink Normal View History

2026-03-07 19:27:44 +08:00
package testing
import (
"testing"
"b612.me/stardb"
_ "modernc.org/sqlite"
)
// setupTestDB creates a test database with sample data
// This function is only available when building with -tags=testing
func setupTestDB(t *testing.T) *stardb.StarDB {
db := &stardb.StarDB{}
err := db.Open("sqlite", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
// Create test table
_, err = db.Exec(`
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL,
age INTEGER,
balance REAL,
active BOOLEAN,
created_at DATETIME
)
`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
// Insert test data
_, err = db.Exec(`
INSERT INTO users (name, email, age, balance, active, created_at) VALUES
('Alice', 'alice@example.com', 25, 100.50, 1, '2024-01-01 10:00:00'),
('Bob', 'bob@example.com', 30, 200.75, 1, '2024-01-02 11:00:00'),
('Charlie', 'charlie@example.com', 35, 300.25, 0, '2024-01-03 12:00:00')
`)
if err != nil {
t.Fatalf("Failed to insert test data: %v", err)
}
return db
}