Compare commits
No commits in common. "master" and "copy-language-code" have entirely different histories.
master
...
copy-langu
@ -12,17 +12,17 @@ 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"
|
||||||
|
"git.defalsify.org/vise.git/lang"
|
||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
"git.grassecon.net/urdt/ussd/initializers"
|
||||||
"git.grassecon.net/urdt/ussd/internal/args"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||||
"git.grassecon.net/urdt/ussd/internal/http/at"
|
"git.grassecon.net/urdt/ussd/internal/http/at"
|
||||||
"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"
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/args"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -42,6 +42,7 @@ func main() {
|
|||||||
var connStr string
|
var connStr string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
var size uint
|
var size uint
|
||||||
|
var database string
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
var host string
|
var host string
|
||||||
var port uint
|
var port uint
|
||||||
@ -59,7 +60,7 @@ func main() {
|
|||||||
flag.Var(&langs, "language", "add symbol resolution for language")
|
flag.Var(&langs, "language", "add symbol resolution for language")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if connStr == "" {
|
if connStr != "" {
|
||||||
connStr = config.DbConn
|
connStr = config.DbConn
|
||||||
}
|
}
|
||||||
connData, err := storage.ToConnData(connStr)
|
connData, err := storage.ToConnData(connStr)
|
||||||
@ -71,6 +72,7 @@ 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)
|
ln, err := lang.LanguageFromCode(config.DefaultLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "default language set error: %v", err)
|
fmt.Fprintf(os.Stderr, "default language set error: %v", err)
|
||||||
|
@ -10,16 +10,16 @@ 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"
|
||||||
|
"git.defalsify.org/vise.git/lang"
|
||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
"git.grassecon.net/urdt/ussd/initializers"
|
||||||
"git.grassecon.net/urdt/ussd/internal/args"
|
|
||||||
"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"
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/args"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -52,6 +52,7 @@ func main() {
|
|||||||
var sessionId string
|
var sessionId string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
var size uint
|
var size uint
|
||||||
|
var database string
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
var host string
|
var host string
|
||||||
var port uint
|
var port uint
|
||||||
@ -70,7 +71,7 @@ func main() {
|
|||||||
flag.Var(&langs, "language", "add symbol resolution for language")
|
flag.Var(&langs, "language", "add symbol resolution for language")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if connStr == "" {
|
if connStr != "" {
|
||||||
connStr = config.DbConn
|
connStr = config.DbConn
|
||||||
}
|
}
|
||||||
connData, err := storage.ToConnData(connStr)
|
connData, err := storage.ToConnData(connStr)
|
||||||
@ -82,6 +83,7 @@ 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)
|
||||||
|
|
||||||
ln, err := lang.LanguageFromCode(config.DefaultLanguage)
|
ln, err := lang.LanguageFromCode(config.DefaultLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -115,6 +117,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
userdataStore, err := menuStorageService.GetUserdataDb(ctx)
|
userdataStore, err := menuStorageService.GetUserdataDb(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
|
@ -12,17 +12,17 @@ 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"
|
||||||
|
"git.defalsify.org/vise.git/lang"
|
||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
"git.grassecon.net/urdt/ussd/initializers"
|
||||||
"git.grassecon.net/urdt/ussd/internal/args"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||||
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
||||||
"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"
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/args"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -41,6 +41,7 @@ func main() {
|
|||||||
var connStr string
|
var connStr string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
var size uint
|
var size uint
|
||||||
|
var database string
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
var host string
|
var host string
|
||||||
var port uint
|
var port uint
|
||||||
@ -58,7 +59,7 @@ func main() {
|
|||||||
flag.Var(&langs, "language", "add symbol resolution for language")
|
flag.Var(&langs, "language", "add symbol resolution for language")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if connStr == "" {
|
if connStr != "" {
|
||||||
connStr = config.DbConn
|
connStr = config.DbConn
|
||||||
}
|
}
|
||||||
connData, err := storage.ToConnData(connStr)
|
connData, err := storage.ToConnData(connStr)
|
||||||
@ -70,6 +71,7 @@ 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)
|
||||||
|
|
||||||
ln, err := lang.LanguageFromCode(config.DefaultLanguage)
|
ln, err := lang.LanguageFromCode(config.DefaultLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -8,14 +8,14 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
|
|
||||||
"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"
|
||||||
|
"git.defalsify.org/vise.git/lang"
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
"git.grassecon.net/urdt/ussd/initializers"
|
||||||
"git.grassecon.net/urdt/ussd/internal/args"
|
|
||||||
"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/internal/args"
|
||||||
"git.grassecon.net/urdt/ussd/remote"
|
"git.grassecon.net/urdt/ussd/remote"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,6 +36,7 @@ func main() {
|
|||||||
var connStr string
|
var connStr string
|
||||||
var size uint
|
var size uint
|
||||||
var sessionId string
|
var sessionId string
|
||||||
|
var database string
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
var err error
|
var err error
|
||||||
@ -51,7 +52,7 @@ func main() {
|
|||||||
flag.Var(&langs, "language", "add symbol resolution for language")
|
flag.Var(&langs, "language", "add symbol resolution for language")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if connStr == "" {
|
if connStr != "" {
|
||||||
connStr = config.DbConn
|
connStr = config.DbConn
|
||||||
}
|
}
|
||||||
connData, err := storage.ToConnData(connStr)
|
connData, err := storage.ToConnData(connStr)
|
||||||
@ -68,6 +69,7 @@ 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)
|
||||||
|
|
||||||
ln, err := lang.LanguageFromCode(config.DefaultLanguage)
|
ln, err := lang.LanguageFromCode(config.DefaultLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -9,16 +9,14 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/logging"
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.grassecon.net/urdt/ussd/common"
|
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||||
testdataloader "github.com/peteole/testdata-loader"
|
"git.grassecon.net/urdt/ussd/initializers"
|
||||||
|
"git.grassecon.net/urdt/ussd/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla()
|
logg = logging.NewVanilla()
|
||||||
baseDir = testdataloader.GetBasePath()
|
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,6 +24,7 @@ func init() {
|
|||||||
initializers.LoadEnvVariables()
|
initializers.LoadEnvVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.LoadConfig()
|
config.LoadConfig()
|
||||||
|
|
||||||
@ -87,4 +86,5 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,21 +3,15 @@ package initializers
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadEnvVariables() {
|
func LoadEnvVariables() {
|
||||||
LoadEnvVariablesPath(".")
|
err := godotenv.Load()
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@ const (
|
|||||||
type ConnData struct {
|
type ConnData struct {
|
||||||
typ int
|
typ int
|
||||||
str string
|
str string
|
||||||
domain string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cd *ConnData) DbType() int {
|
func (cd *ConnData) DbType() int {
|
||||||
@ -26,38 +25,23 @@ func (cd *ConnData) String() string {
|
|||||||
return cd.str
|
return cd.str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cd *ConnData) Domain() string {
|
func probePostgres(s string) (string, bool) {
|
||||||
return cd.domain
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cd *ConnData) Path() string {
|
|
||||||
v, _ := url.Parse(cd.str)
|
|
||||||
v.RawQuery = ""
|
|
||||||
return v.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func probePostgres(s string) (string, string, bool) {
|
|
||||||
domain := "public"
|
|
||||||
v, err := url.Parse(s)
|
v, err := url.Parse(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", false
|
return "", false
|
||||||
}
|
}
|
||||||
if v.Scheme != "postgres" {
|
if v.Scheme != "postgres" {
|
||||||
return "", "", false
|
return "", false
|
||||||
}
|
}
|
||||||
vv := v.Query()
|
return s, true
|
||||||
if vv.Has("search_path") {
|
|
||||||
domain = vv.Get("search_path")
|
|
||||||
}
|
|
||||||
return s, domain, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func probeGdbm(s string) (string, string, bool) {
|
func probeGdbm(s string) (string, bool) {
|
||||||
if !path.IsAbs(s) {
|
if !path.IsAbs(s) {
|
||||||
return "", "", false
|
return "", false
|
||||||
}
|
}
|
||||||
s = path.Clean(s)
|
s = path.Clean(s)
|
||||||
return s, "", true
|
return s, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToConnData(connStr string) (ConnData, error) {
|
func ToConnData(connStr string) (ConnData, error) {
|
||||||
@ -67,15 +51,14 @@ func ToConnData(connStr string) (ConnData, error) {
|
|||||||
return o, nil
|
return o, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
v, domain, ok := probePostgres(connStr)
|
v, ok := probePostgres(connStr)
|
||||||
if ok {
|
if ok {
|
||||||
o.typ = DBTYPE_POSTGRES
|
o.typ = DBTYPE_POSTGRES
|
||||||
o.str = v
|
o.str = v
|
||||||
o.domain = domain
|
|
||||||
return o, nil
|
return o, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
v, _, ok = probeGdbm(connStr)
|
v, ok = probeGdbm(connStr)
|
||||||
if ok {
|
if ok {
|
||||||
o.typ = DBTYPE_GDBM
|
o.typ = DBTYPE_GDBM
|
||||||
o.str = v
|
o.str = v
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"git.defalsify.org/vise.git/persist"
|
"git.defalsify.org/vise.git/persist"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
gdbmstorage "git.grassecon.net/urdt/ussd/internal/storage/db/gdbm"
|
gdbmstorage "git.grassecon.net/urdt/ussd/internal/storage/db/gdbm"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -55,12 +54,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D
|
|||||||
connStr := ms.conn.String()
|
connStr := ms.conn.String()
|
||||||
dbTyp := ms.conn.DbType()
|
dbTyp := ms.conn.DbType()
|
||||||
if dbTyp == DBTYPE_POSTGRES {
|
if dbTyp == DBTYPE_POSTGRES {
|
||||||
// TODO: move to vise
|
newDb = postgres.NewPgDb()
|
||||||
err = ensureSchemaExists(ctx, ms.conn)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
newDb = postgres.NewPgDb().WithSchema(ms.conn.Domain())
|
|
||||||
} else if dbTyp == DBTYPE_GDBM {
|
} else if dbTyp == DBTYPE_GDBM {
|
||||||
err = ms.ensureDbDir()
|
err = ms.ensureDbDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,7 +65,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D
|
|||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("unsupported connection string: '%s'\n", ms.conn.String())
|
return nil, fmt.Errorf("unsupported connection string: '%s'\n", ms.conn.String())
|
||||||
}
|
}
|
||||||
logg.DebugCtxf(ctx, "connecting to db", "conn", connStr, "conndata", ms.conn)
|
logg.DebugCtxf(ctx, "connecting to db", "conn", connStr)
|
||||||
err = newDb.Connect(ctx, connStr)
|
err = newDb.Connect(ctx, connStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -102,23 +96,6 @@ func (ms *MenuStorageService) WithGettext(path string, lns []lang.Language) *Men
|
|||||||
return ms
|
return ms
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensureSchemaExists creates a new schema if it does not exist
|
|
||||||
func ensureSchemaExists(ctx context.Context, conn ConnData) error {
|
|
||||||
h, err := pgxpool.New(ctx, conn.Path())
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to connect to the database: %w", err)
|
|
||||||
}
|
|
||||||
defer h.Close()
|
|
||||||
|
|
||||||
query := fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s", conn.Domain())
|
|
||||||
_, err = h.Exec(ctx, query)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create schema: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
|
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
|
||||||
stateStore, err := ms.GetStateStore(ctx)
|
stateStore, err := ms.GetStateStore(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
@ -3,8 +3,6 @@ package testutil
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -13,90 +11,21 @@ import (
|
|||||||
"git.defalsify.org/vise.git/engine"
|
"git.defalsify.org/vise.git/engine"
|
||||||
"git.defalsify.org/vise.git/logging"
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
|
||||||
"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/internal/testutil/testservice"
|
"git.grassecon.net/urdt/ussd/internal/testutil/testservice"
|
||||||
"git.grassecon.net/urdt/ussd/internal/testutil/testtag"
|
"git.grassecon.net/urdt/ussd/internal/testutil/testtag"
|
||||||
"git.grassecon.net/urdt/ussd/remote"
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
testdataloader "github.com/peteole/testdata-loader"
|
testdataloader "github.com/peteole/testdata-loader"
|
||||||
|
"git.grassecon.net/urdt/ussd/remote"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla()
|
|
||||||
baseDir = testdataloader.GetBasePath()
|
baseDir = testdataloader.GetBasePath()
|
||||||
|
logg = logging.NewVanilla()
|
||||||
scriptDir = path.Join(baseDir, "services", "registration")
|
scriptDir = path.Join(baseDir, "services", "registration")
|
||||||
setDbType string
|
|
||||||
setConnStr string
|
|
||||||
setDbSchema string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
initializers.LoadEnvVariablesPath(baseDir)
|
|
||||||
config.LoadConfig()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetDatabase updates the database used by TestEngine
|
|
||||||
func SetDatabase(database, connStr, dbSchema string) {
|
|
||||||
setDbType = database
|
|
||||||
setConnStr = connStr
|
|
||||||
setDbSchema = dbSchema
|
|
||||||
}
|
|
||||||
|
|
||||||
// CleanDatabase removes all test data from the database
|
|
||||||
func CleanDatabase() {
|
|
||||||
if setDbType == "postgres" {
|
|
||||||
ctx := context.Background()
|
|
||||||
// Update the connection string with the new search path
|
|
||||||
updatedConnStr, err := updateSearchPath(setConnStr, setDbSchema)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to update search path: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dbConn, err := pgxpool.New(ctx, updatedConnStr)
|
|
||||||
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;", setDbSchema)
|
|
||||||
_, execErr := dbConn.Exec(ctx, query)
|
|
||||||
if execErr != nil {
|
|
||||||
log.Printf("Failed to cleanup table %s.kv_vise: %v", setDbSchema, execErr)
|
|
||||||
} else {
|
|
||||||
log.Printf("Successfully cleaned up table %s.kv_vise", setDbSchema)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
setConnStr, _ := filepath.Abs(setConnStr)
|
|
||||||
if err := os.RemoveAll(setConnStr); err != nil {
|
|
||||||
log.Fatalf("Failed to delete state store %s: %v", setConnStr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// updateSearchPath updates the search_path (schema) to be used in the connection
|
|
||||||
func updateSearchPath(connStr string, newSearchPath string) (string, error) {
|
|
||||||
u, err := url.Parse(connStr)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("invalid connection string: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the query parameters
|
|
||||||
q := u.Query()
|
|
||||||
|
|
||||||
// Update or add the search_path parameter
|
|
||||||
q.Set("search_path", newSearchPath)
|
|
||||||
|
|
||||||
// Rebuild the connection string with updated parameters
|
|
||||||
u.RawQuery = q.Encode()
|
|
||||||
|
|
||||||
return u.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
||||||
var err error
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
@ -110,27 +39,16 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
|||||||
FlagCount: uint32(128),
|
FlagCount: uint32(128),
|
||||||
}
|
}
|
||||||
|
|
||||||
if setDbType == "postgres" {
|
connStr, err := filepath.Abs(".test_state/state.gdbm")
|
||||||
setConnStr = config.DbConn
|
|
||||||
setConnStr, err = updateSearchPath(setConnStr, setDbSchema)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
setConnStr, err = filepath.Abs(setConnStr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "connstr err: %v", err)
|
fmt.Fprintf(os.Stderr, "connstr err: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
conn, err := storage.ToConnData(connStr)
|
||||||
|
|
||||||
conn, err := storage.ToConnData(setConnStr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "connstr parse err: %v", err)
|
fmt.Fprintf(os.Stderr, "connstr parse err: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceDir := scriptDir
|
resourceDir := scriptDir
|
||||||
menuStorageService := storage.NewMenuStorageService(conn, resourceDir)
|
menuStorageService := storage.NewMenuStorageService(conn, resourceDir)
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -22,9 +24,11 @@ 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 connStr = flag.String("conn", ".test_state", "connection string")
|
func testStore() string {
|
||||||
var dbSchema = flag.String("schema", "test", "Specify the database schema (default test)")
|
v, _ := filepath.Abs(".test_state/state.gdbm")
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func GenerateSessionId() string {
|
func GenerateSessionId() string {
|
||||||
uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g))
|
uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g))
|
||||||
@ -80,15 +84,12 @@ func extractSendAmount(response []byte) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
// Parse the flags
|
|
||||||
flag.Parse()
|
|
||||||
sessionID = GenerateSessionId()
|
sessionID = GenerateSessionId()
|
||||||
// set the db
|
defer func() {
|
||||||
testutil.SetDatabase(*database, *connStr, *dbSchema)
|
if err := os.RemoveAll(testStore()); err != nil {
|
||||||
|
log.Fatalf("Failed to delete state store %s: %v", testStore(), err)
|
||||||
// Cleanup the db after tests
|
}
|
||||||
defer testutil.CleanDatabase()
|
}()
|
||||||
|
|
||||||
m.Run()
|
m.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +126,7 @@ func TestAccountCreationSuccessful(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
<-eventChannel
|
<-eventChannel
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccountRegistrationRejectTerms(t *testing.T) {
|
func TestAccountRegistrationRejectTerms(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user