test: add test data directories (testMusic and testCharacters)
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestSetupDB initializes the test database using existing functions
|
||||
// It creates the database if it doesn't exist and runs migrations
|
||||
func TestSetupDB(t *testing.T) {
|
||||
host := os.Getenv("DB_HOST")
|
||||
port := os.Getenv("DB_PORT")
|
||||
user := os.Getenv("DB_USERNAME")
|
||||
password := os.Getenv("DB_PASSWORD")
|
||||
dbname := os.Getenv("DB_NAME")
|
||||
|
||||
if host == "" || port == "" || user == "" || password == "" || dbname == "" {
|
||||
t.Skip("Test database environment variables not set")
|
||||
}
|
||||
|
||||
// Use existing function to create database if it doesn't exist and run migrations
|
||||
Migrate_db(host, port, user, password, dbname)
|
||||
InitDB(host, port, user, password, dbname)
|
||||
}
|
||||
|
||||
// TestTearDownDB closes the test database connection
|
||||
func TestTearDownDB(t *testing.T) {
|
||||
CloseDb()
|
||||
}
|
||||
|
||||
// TestClearDatabase clears all data from the test database
|
||||
// Useful for running tests with a clean slate
|
||||
func TestClearDatabase(t *testing.T) {
|
||||
if Dbpool == nil {
|
||||
t.Skip("Database not initialized")
|
||||
}
|
||||
|
||||
// Clear all tables in reverse order to respect foreign keys
|
||||
// Note: This assumes the tables exist and have the expected structure
|
||||
tables := []string{
|
||||
"song_list",
|
||||
"song",
|
||||
"game",
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
_, err := Dbpool.Exec(Ctx, "TRUNCATE TABLE "+table+" CASCADE")
|
||||
if err != nil {
|
||||
t.Logf("Failed to truncate table %s: %v", table, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Reset sequences
|
||||
_, err := Dbpool.Exec(Ctx, "SELECT setval('game_id_seq', 1, false)")
|
||||
if err != nil {
|
||||
t.Logf("Failed to reset game_id_seq: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user