Merged in feature/serviceconfig-integration (pull request #38)
DRAFT PR : WIP working through ideas for integration * movearound * attempttwo * openapi * further sanding * fix * start on tests * runthroughsingleconfig * somechanges * reflectissue * removeerrs * mostlyremovepanic * removeenv * noncfgtests * go * repo * fix service config test * add PWD to all * test fix * fix lint * todo for later * passingunittests * alltests * testlogger * testloggername * clean
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMustGetURIInvalidPort(t *testing.T) {
|
||||
t.Setenv("DB_USER", "user")
|
||||
t.Setenv("DB_PASS", "pass")
|
||||
t.Setenv("DB_HOST", "host")
|
||||
t.Setenv("DB_PORT", "port")
|
||||
t.Setenv("DB_NAME", "name")
|
||||
|
||||
assert.Panics(t, func() { mustGetURI() })
|
||||
}
|
||||
|
||||
func TestMustGetURINoSSL(t *testing.T) {
|
||||
t.Setenv("DB_USER", "user")
|
||||
t.Setenv("DB_PASS", "pass")
|
||||
t.Setenv("DB_HOST", "host")
|
||||
t.Setenv("DB_PORT", "5432")
|
||||
t.Setenv("DB_NAME", "name")
|
||||
t.Setenv("DB_NOSSL", "1")
|
||||
|
||||
connStr := mustGetURI()
|
||||
assert.Equal(t, "postgres://user:pass@host:5432/name?sslmode=disable", connStr.String())
|
||||
}
|
||||
|
||||
func TestMustGetURINoEnv(t *testing.T) {
|
||||
t.Setenv("DB_USER", "")
|
||||
t.Setenv("DB_PASS", "")
|
||||
t.Setenv("DB_HOST", "")
|
||||
t.Setenv("DB_PORT", "")
|
||||
t.Setenv("DB_NAME", "")
|
||||
|
||||
assert.Panics(t, func() { mustGetURI() })
|
||||
}
|
||||
|
||||
func TestGetPoolConfig(t *testing.T) {
|
||||
t.Setenv("DB_USER", "user")
|
||||
t.Setenv("DB_PASS", "pass")
|
||||
t.Setenv("DB_HOST", "host")
|
||||
t.Setenv("DB_PORT", "5432")
|
||||
t.Setenv("DB_NAME", "name")
|
||||
|
||||
config, err := getPoolConfig()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, config.ConnString(), mustGetURI().String())
|
||||
}
|
||||
|
||||
func TestMustGetPoolConfig(t *testing.T) {
|
||||
t.Setenv("DB_USER", "user")
|
||||
t.Setenv("DB_PASS", "pass")
|
||||
t.Setenv("DB_HOST", "host")
|
||||
t.Setenv("DB_PORT", "5432")
|
||||
t.Setenv("DB_NAME", "name")
|
||||
|
||||
config := mustGetPoolConfig()
|
||||
assert.Equal(t, config.ConnString(), mustGetURI().String())
|
||||
}
|
||||
|
||||
func TestMustGetPoolConfigInvalidPort(t *testing.T) {
|
||||
t.Setenv("DB_USER", "user")
|
||||
t.Setenv("DB_PASS", "pass")
|
||||
t.Setenv("DB_HOST", "host")
|
||||
t.Setenv("DB_PORT", "invalid_port")
|
||||
t.Setenv("DB_NAME", "name")
|
||||
|
||||
assert.Panics(t, func() { mustGetPoolConfig() })
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/server/env"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
user string
|
||||
password string
|
||||
host string
|
||||
port nat.Port
|
||||
name string
|
||||
disableSSL bool
|
||||
}
|
||||
|
||||
func mustGetConfig() *config {
|
||||
user := env.GetPanic("DB_USER")
|
||||
pass := env.GetPanic("DB_PASS")
|
||||
host := env.GetPanic("DB_HOST")
|
||||
port := env.GetPanic("DB_PORT")
|
||||
natPort, err := nat.NewPort("tcp", port)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to create port: %v", err)
|
||||
}
|
||||
name := env.GetPanic("DB_NAME")
|
||||
|
||||
disableSSL, _ := env.Get("DB_NOSSL")
|
||||
|
||||
return &config{
|
||||
user: user,
|
||||
password: pass,
|
||||
host: host,
|
||||
port: natPort,
|
||||
name: name,
|
||||
disableSSL: disableSSL == "1",
|
||||
}
|
||||
}
|
||||
|
||||
func mustGetURI() *url.URL {
|
||||
driver := "postgres"
|
||||
conf := mustGetConfig()
|
||||
opts := ""
|
||||
|
||||
if conf.disableSSL {
|
||||
opts += "sslmode=disable"
|
||||
}
|
||||
|
||||
connStr := fmt.Sprintf("%s://%s:%s@%s:%d/%s?%s", driver, conf.user, conf.password, conf.host, conf.port.Int(), conf.name, opts)
|
||||
|
||||
uri, err := url.Parse(connStr)
|
||||
if err != nil {
|
||||
log.Panicf("Unable to parse URI: %s", err)
|
||||
}
|
||||
|
||||
return uri
|
||||
}
|
||||
|
||||
func getPoolConfig() (*pgxpool.Config, error) {
|
||||
connStr := mustGetURI()
|
||||
|
||||
config, err := pgxpool.ParseConfig(connStr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
enumTypes := []string{
|
||||
"querytype",
|
||||
}
|
||||
|
||||
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
||||
for _, enumName := range enumTypes {
|
||||
dt, err := conn.LoadType(ctx, enumName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load suitable enum type: %w", err)
|
||||
}
|
||||
conn.TypeMap().RegisterType(dt)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func mustGetPoolConfig() *pgxpool.Config {
|
||||
config, err := getPoolConfig()
|
||||
if err != nil {
|
||||
log.Panicf("Unable to create database config: %v\n", err)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func GetDBPool(ctx context.Context) *pgxpool.Pool {
|
||||
config := mustGetPoolConfig()
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, config)
|
||||
if err != nil {
|
||||
log.Panicf("Unable to create database pool: %v\n", err)
|
||||
}
|
||||
|
||||
return pool
|
||||
}
|
||||
|
||||
func ExecuteTransaction(ctx context.Context, db *Connection, executeQueries func(context.Context, *repository.Queries) error) error {
|
||||
tx, err := db.Pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = tx.Rollback(ctx)
|
||||
}()
|
||||
|
||||
qtx := db.Queries.WithTx(tx)
|
||||
|
||||
err = executeQueries(ctx, qtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package database_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDBConn(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
}
|
||||
ctx := context.Background()
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../.."),
|
||||
},
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
pool := database.GetDBPool(ctx)
|
||||
assert.NotNil(t, pool)
|
||||
}
|
||||
|
||||
func TestExecuteTransaction(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open pgxmock database: %v", err)
|
||||
}
|
||||
queries := repository.New(pool)
|
||||
db := &database.Connection{
|
||||
Queries: queries,
|
||||
Pool: pool,
|
||||
}
|
||||
|
||||
clientID := database.MustToDBUUID(uuid.New())
|
||||
clientName := "example_client"
|
||||
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectQuery("name: CreateClient :one").WithArgs(clientName).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(clientID),
|
||||
)
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = database.ExecuteTransaction(ctx, db, func(ctx context.Context, q *repository.Queries) error {
|
||||
id, err := q.CreateClient(ctx, "example_client")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, clientID, id)
|
||||
|
||||
return nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func createDB() {
|
||||
driver := "postgres"
|
||||
conf := mustGetConfig()
|
||||
|
||||
opts := ""
|
||||
if conf.disableSSL {
|
||||
opts += "sslmode=disable"
|
||||
}
|
||||
|
||||
connStr := fmt.Sprintf("%s://%s:%s@%s:%d/?%s", driver, conf.user, conf.password, conf.host, conf.port.Int(), opts)
|
||||
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
log.Panicf("Error opening database: %v", err)
|
||||
}
|
||||
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
log.Panicf("Error pinging database: %v", err)
|
||||
}
|
||||
|
||||
rs, err := db.Query(fmt.Sprintf("SELECT 'CREATE DATABASE %s' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '%s')", conf.name, conf.name))
|
||||
if err != nil {
|
||||
log.Panicf("Error creating database: %v", err)
|
||||
}
|
||||
|
||||
if rs.Next() {
|
||||
log.Printf("Database created: %s", conf.name)
|
||||
} else {
|
||||
log.Printf("Database already exists: %s", conf.name)
|
||||
}
|
||||
}
|
||||
|
||||
type MigrationConfig struct {
|
||||
BasePath string
|
||||
}
|
||||
|
||||
func RunMigrations(ctx context.Context, config *MigrationConfig) {
|
||||
createDB()
|
||||
|
||||
connStr := mustGetURI()
|
||||
|
||||
basePath := config.BasePath
|
||||
if basePath == "" {
|
||||
basePath = os.Getenv("PWD")
|
||||
}
|
||||
|
||||
migPath := "file://" + path.Join(basePath, "database/migrations")
|
||||
m, err := migrate.New(
|
||||
migPath,
|
||||
connStr.String(),
|
||||
)
|
||||
if err != nil {
|
||||
log.Panicf("failed to create migrate instance: %v", err)
|
||||
}
|
||||
|
||||
err = m.Up()
|
||||
if err == migrate.ErrNoChange {
|
||||
log.Println("No changes required")
|
||||
} else if err != nil {
|
||||
log.Panicf("failed to apply migrations: %v", err)
|
||||
} else {
|
||||
log.Println("Migrations applied successfully!")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func createDB(cfg serviceconfig.ConfigProvider) error {
|
||||
connStr := fmt.Sprintf("%s?%s", cfg.GetDBBaseURI(), cfg.GetDBOptsString())
|
||||
|
||||
db, err := sql.Open(cfg.GetDBDriver(), connStr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening database: %v", err)
|
||||
}
|
||||
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error pinging database: %v", err)
|
||||
}
|
||||
|
||||
rs, err := db.Query(fmt.Sprintf("SELECT 'CREATE DATABASE %s' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '%s')", cfg.GetDBName(), cfg.GetDBName()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating database: %v", err)
|
||||
}
|
||||
|
||||
if !rs.Next() {
|
||||
slog.Info("database created", "name", cfg.GetDBName())
|
||||
} else {
|
||||
slog.Info("database already exists", "name", cfg.GetDBName())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Run(ctx context.Context, cfg serviceconfig.ConfigProvider) error {
|
||||
err := createDB(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
migPath := "file://" + path.Join(cfg.GetBasePath(), "database/migrations")
|
||||
m, err := migrate.New(
|
||||
migPath,
|
||||
cfg.GetDBURI(),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create migrate instance: %v", err)
|
||||
}
|
||||
|
||||
err = m.Up()
|
||||
if err == migrate.ErrNoChange {
|
||||
slog.Info("No migration changes required")
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("failed to apply migrations: %v", err)
|
||||
} else {
|
||||
slog.Info("Migrations applied successfully!")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+20
-12
@@ -1,10 +1,11 @@
|
||||
package database_test
|
||||
package migrations_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/migrations"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
@@ -17,12 +18,18 @@ func TestRunMigrations(t *testing.T) {
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{})
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
database.RunMigrations(ctx, &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../.."),
|
||||
})
|
||||
err = migrations.Run(ctx, cfg)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestRunMigrationsNoDB(t *testing.T) {
|
||||
@@ -33,11 +40,12 @@ func TestRunMigrationsNoDB(t *testing.T) {
|
||||
t.Setenv("DB_HOST", "invalid_host")
|
||||
t.Setenv("DB_PORT", "5432")
|
||||
t.Setenv("DB_NAME", "invalid_name")
|
||||
t.Setenv("DB_NOSSL", "1")
|
||||
t.Setenv("DB_NOSSL", "true")
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Panics(t, func() {
|
||||
database.RunMigrations(ctx, &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../.."),
|
||||
})
|
||||
})
|
||||
err = migrations.Run(ctx, cfg)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCreateDB(t *testing.T) {
|
||||
t.Setenv("DB_HOST", "invalid_value")
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = createDB(cfg)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCreateDB(t *testing.T) {
|
||||
t.Setenv("DB_HOST", "")
|
||||
|
||||
assert.Panics(t, func() { createDB() })
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"queryorchestration/internal/database/repository"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type Pool interface {
|
||||
Begin(ctx context.Context) (pgx.Tx, error)
|
||||
}
|
||||
|
||||
type Connection struct {
|
||||
Pool Pool
|
||||
Queries *repository.Queries
|
||||
}
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
@@ -14,13 +14,19 @@ import (
|
||||
|
||||
func TestClient(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
id, err := queries.CreateClient(ctx, "example_client")
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
@@ -16,13 +17,19 @@ import (
|
||||
|
||||
func TestCollector(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
contextId, err := queries.CreateQuery(ctx, repository.QuerytypeContextFull)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
@@ -14,13 +14,19 @@ import (
|
||||
|
||||
func TestDocument(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
clientId, err := queries.CreateClient(ctx, "example_client")
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
@@ -14,13 +14,18 @@ import (
|
||||
|
||||
func TestJob(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
clientId, err := queries.CreateClient(ctx, "example_client")
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
@@ -16,14 +17,18 @@ import (
|
||||
|
||||
func TestQueries(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
contextQueryID, err := queries.CreateQuery(ctx, repository.Querytype(repository.QuerytypeContextFull))
|
||||
assert.Nil(t, err)
|
||||
@@ -162,13 +167,18 @@ func TestQueries(t *testing.T) {
|
||||
func TestQueryDependencyTree(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
contextQueryID, err := queries.CreateQuery(ctx, repository.Querytype(repository.QuerytypeContextFull))
|
||||
assert.Nil(t, err)
|
||||
@@ -230,13 +240,18 @@ func TestQueryDependencyTree(t *testing.T) {
|
||||
func TestQueriesList(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: "../../..",
|
||||
}})
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
contextQueryID, err := queries.CreateQuery(ctx, repository.Querytype(repository.QuerytypeContextFull))
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
@@ -16,13 +16,18 @@ import (
|
||||
func TestResults(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
jsonQueryID, err := queries.CreateQuery(ctx, repository.Querytype(repository.QuerytypeJsonExtractor))
|
||||
assert.Nil(t, err)
|
||||
@@ -72,13 +77,18 @@ func TestResults(t *testing.T) {
|
||||
func TestResultValues(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
jsonQueryID, err := queries.CreateQuery(ctx, repository.Querytype(repository.QuerytypeJsonExtractor))
|
||||
assert.Nil(t, err)
|
||||
@@ -167,13 +177,18 @@ func TestResultValues(t *testing.T) {
|
||||
func TestUnsyncedQueries(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../../.."),
|
||||
}})
|
||||
t.Setenv("BASE_PATH", path.Join(os.Getenv("PWD"), "../../.."))
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
err := serviceconfig.InitializeConfig(cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Cfg: cfg,
|
||||
RunMigrations: true,
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
queries := repository.New(db.Pool)
|
||||
queries := cfg.DBQueries
|
||||
|
||||
clientId, err := queries.CreateClient(ctx, "example_client")
|
||||
assert.Nil(t, err)
|
||||
|
||||
Reference in New Issue
Block a user