cleanup the generated test data for the schema
This commit is contained in:
parent
a37f6e6da3
commit
ea9cab930e
@ -4,15 +4,18 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||||
"git.grassecon.net/urdt/ussd/internal/testutil"
|
"git.grassecon.net/urdt/ussd/internal/testutil"
|
||||||
"git.grassecon.net/urdt/ussd/internal/testutil/driver"
|
"git.grassecon.net/urdt/ussd/internal/testutil/driver"
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -25,6 +28,7 @@ var (
|
|||||||
|
|
||||||
var groupTestFile = flag.String("test-file", "group_test.json", "The test file to use for running the group tests")
|
var groupTestFile = flag.String("test-file", "group_test.json", "The test file to use for running the group tests")
|
||||||
var database = flag.String("db", "gdbm", "Specify the database (gdbm or postgres)")
|
var database = flag.String("db", "gdbm", "Specify the database (gdbm or postgres)")
|
||||||
|
var dbSchema = flag.String("schema", "test", "Specify the database schema (default test)")
|
||||||
|
|
||||||
func GenerateSessionId() string {
|
func GenerateSessionId() string {
|
||||||
uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g))
|
uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g))
|
||||||
@ -91,7 +95,29 @@ func TestMain(m *testing.M) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Set the selected database
|
// Set the selected database
|
||||||
testutil.SetDatabase(*database)
|
testutil.SetDatabase(*database, *dbSchema)
|
||||||
|
|
||||||
|
// Cleanup the schema table after tests
|
||||||
|
defer func() {
|
||||||
|
if *database == "postgres" {
|
||||||
|
ctx := context.Background()
|
||||||
|
connStr := storage.BuildConnStr()
|
||||||
|
dbConn, err := pgxpool.New(ctx, connStr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to connect to database for cleanup: %v", err)
|
||||||
|
}
|
||||||
|
defer dbConn.Close()
|
||||||
|
|
||||||
|
query := fmt.Sprintf("DELETE FROM %s.kv_vise;", *dbSchema)
|
||||||
|
_, execErr := dbConn.Exec(ctx, query)
|
||||||
|
if execErr != nil {
|
||||||
|
log.Printf("Failed to cleanup table %s.kv_vise: %v", *dbSchema, execErr)
|
||||||
|
} else {
|
||||||
|
log.Printf("Successfully cleaned up table %s.kv_vise", *dbSchema)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
m.Run()
|
m.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user