Rehabilitate tests

This commit is contained in:
lash 2025-01-08 22:27:19 +00:00 committed by konstantinmds
parent aa1b2bb108
commit 86c5efa3ad
9 changed files with 55 additions and 40 deletions

View File

@ -12,9 +12,9 @@ import (
"syscall" "syscall"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/lang"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
testdataloader "github.com/peteole/testdata-loader"
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/initializers"
@ -27,14 +27,13 @@ import (
var ( var (
logg = logging.NewVanilla().WithDomain("AfricasTalking").WithContextKey("at-session-id") logg = logging.NewVanilla().WithDomain("AfricasTalking").WithContextKey("at-session-id")
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join("services", "registration") scriptDir = path.Join("services", "registration")
build = "dev" build = "dev"
menuSeparator = ": " menuSeparator = ": "
) )
func init() { func init() {
initializers.LoadEnvVariables(baseDir) initializers.LoadEnvVariables()
} }
func main() { func main() {
@ -43,8 +42,6 @@ func main() {
var connStr string var connStr string
var resourceDir string var resourceDir string
var size uint var size uint
var database string
var dbSchema string
var engineDebug bool var engineDebug bool
var host string var host string
var port uint var port uint
@ -75,8 +72,13 @@ func main() {
logg.Infof("start command", "build", build, "conn", connData, "resourcedir", resourceDir, "outputsize", size) logg.Infof("start command", "build", build, "conn", connData, "resourcedir", resourceDir, "outputsize", size)
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "Database", database) ln, err := lang.LanguageFromCode(config.DefaultLanguage)
ctx = context.WithValue(ctx, "Schema", dbSchema) if err != nil {
fmt.Fprintf(os.Stderr, "default language set error: %v", err)
os.Exit(1)
}
ctx = context.WithValue(ctx, "Language", ln)
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
cfg := engine.Config{ cfg := engine.Config{

View File

@ -10,9 +10,9 @@ import (
"syscall" "syscall"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/lang"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
testdataloader "github.com/peteole/testdata-loader"
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/initializers"
@ -24,13 +24,12 @@ import (
var ( var (
logg = logging.NewVanilla() logg = logging.NewVanilla()
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join("services", "registration") scriptDir = path.Join("services", "registration")
menuSeparator = ": " menuSeparator = ": "
) )
func init() { func init() {
initializers.LoadEnvVariables(baseDir) initializers.LoadEnvVariables()
} }
type asyncRequestParser struct { type asyncRequestParser struct {
@ -53,8 +52,6 @@ func main() {
var sessionId string var sessionId string
var resourceDir string var resourceDir string
var size uint var size uint
var database string
var dbSchema string
var engineDebug bool var engineDebug bool
var host string var host string
var port uint var port uint
@ -86,8 +83,14 @@ func main() {
logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId)
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "Database", database)
ctx = context.WithValue(ctx, "Schema", dbSchema) ln, err := lang.LanguageFromCode(config.DefaultLanguage)
if err != nil {
fmt.Fprintf(os.Stderr, "default language set error: %v", err)
os.Exit(1)
}
ctx = context.WithValue(ctx, "Language", ln)
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
cfg := engine.Config{ cfg := engine.Config{

View File

@ -12,9 +12,9 @@ import (
"syscall" "syscall"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/lang"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
testdataloader "github.com/peteole/testdata-loader"
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/initializers"
@ -27,13 +27,12 @@ import (
var ( var (
logg = logging.NewVanilla() logg = logging.NewVanilla()
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join("services", "registration") scriptDir = path.Join("services", "registration")
menuSeparator = ": " menuSeparator = ": "
) )
func init() { func init() {
initializers.LoadEnvVariables(baseDir) initializers.LoadEnvVariables()
} }
func main() { func main() {
@ -42,8 +41,6 @@ func main() {
var connStr string var connStr string
var resourceDir string var resourceDir string
var size uint var size uint
var database string
var dbSchema string
var engineDebug bool var engineDebug bool
var host string var host string
var port uint var port uint
@ -74,8 +71,14 @@ func main() {
logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size) logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size)
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "Database", database)
ctx = context.WithValue(ctx, "Schema", dbSchema) ln, err := lang.LanguageFromCode(config.DefaultLanguage)
if err != nil {
fmt.Fprintf(os.Stderr, "default language set error: %v", err)
os.Exit(1)
}
ctx = context.WithValue(ctx, "Language", ln)
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
cfg := engine.Config{ cfg := engine.Config{

View File

@ -16,18 +16,16 @@ import (
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
"git.grassecon.net/urdt/ussd/remote" "git.grassecon.net/urdt/ussd/remote"
testdataloader "github.com/peteole/testdata-loader"
) )
var ( var (
logg = logging.NewVanilla() logg = logging.NewVanilla()
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join("services", "registration") scriptDir = path.Join("services", "registration")
menuSeparator = ": " menuSeparator = ": "
) )
func init() { func init() {
initializers.LoadEnvVariables(baseDir) initializers.LoadEnvVariables()
} }
// TODO: external script automatically generate language handler list from select language vise code OR consider dynamic menu generation script possibility // TODO: external script automatically generate language handler list from select language vise code OR consider dynamic menu generation script possibility
@ -37,8 +35,6 @@ func main() {
var connStr string var connStr string
var size uint var size uint
var sessionId string var sessionId string
var database string
var dbSchema string
var engineDebug bool var engineDebug bool
var resourceDir string var resourceDir string
var err error var err error
@ -73,8 +69,14 @@ func main() {
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "SessionId", sessionId)
ctx = context.WithValue(ctx, "Database", database)
ctx = context.WithValue(ctx, "Schema", dbSchema) ln, err := lang.LanguageFromCode(config.DefaultLanguage)
if err != nil {
fmt.Fprintf(os.Stderr, "default language set error: %v", err)
os.Exit(1)
}
ctx = context.WithValue(ctx, "Language", ln)
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
cfg := engine.Config{ cfg := engine.Config{

View File

@ -23,7 +23,7 @@ var (
) )
func init() { func init() {
initializers.LoadEnvVariables(baseDir) initializers.LoadEnvVariables()
} }
func main() { func main() {

View File

@ -9,9 +9,13 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
func LoadEnvVariables(baseDir string) { func LoadEnvVariables() {
envDir := path.Join(baseDir, ".env") LoadEnvVariablesPath(".")
err := godotenv.Load(envDir) }
func LoadEnvVariablesPath(dir string) {
fp := path.Join(dir, ".env")
err := godotenv.Load(fp)
if err != nil { if err != nil {
log.Fatal("Error loading .env file", err) log.Fatal("Error loading .env file", err)
} }

View File

@ -73,11 +73,12 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D
} }
if database == "postgres" { connStr := ms.conn.String()
connStr := BuildConnStr() dbTyp := ms.conn.DbType()
if dbTyp == DBTYPE_POSTGRES {
// Ensure the schema exists newDb = postgres.NewPgDb()
err = ensureSchemaExists(ctx, connStr, schema) } else if dbTyp == DBTYPE_GDBM {
err = ms.ensureDbDir()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to ensure schema exists: %w", err) return nil, fmt.Errorf("failed to ensure schema exists: %w", err)
} }

View File

@ -23,13 +23,14 @@ import (
var ( var (
baseDir = testdataloader.GetBasePath() baseDir = testdataloader.GetBasePath()
logg = logging.NewVanilla() logg = logging.NewVanilla()
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "services", "registration") scriptDir = path.Join(baseDir, "services", "registration")
selectedDatabase = "" selectedDatabase = ""
selectedDbSchema = "" selectedDbSchema = ""
) )
func init() { func init() {
initializers.LoadEnvVariables(baseDir) initializers.LoadEnvVariablesPath(baseDir)
} }
// SetDatabase updates the database used by TestEngine // SetDatabase updates the database used by TestEngine

View File

@ -12,7 +12,6 @@ import (
"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"
@ -106,7 +105,7 @@ func TestMain(m *testing.M) {
defer func() { defer func() {
if *database == "postgres" { if *database == "postgres" {
ctx := context.Background() ctx := context.Background()
connStr := storage.BuildConnStr() connStr := "postgres://" //storage.BuildConnStr()
dbConn, err := pgxpool.New(ctx, connStr) dbConn, err := pgxpool.New(ctx, connStr)
if err != nil { if err != nil {
log.Fatalf("Failed to connect to database for cleanup: %v", err) log.Fatalf("Failed to connect to database for cleanup: %v", err)