Files
query-orchestration/internal/query/list_test.go
T

89 lines
2.5 KiB
Go
Raw Normal View History

2025-01-07 16:30:45 +00:00
package query_test
2025-01-06 14:40:43 +00:00
import (
"context"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/query"
resultprocessor "queryorchestration/internal/query/result/processor"
"queryorchestration/internal/serviceconfig"
2025-01-06 14:40:43 +00:00
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
2025-01-06 14:40:43 +00:00
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/require"
2025-01-06 14:40:43 +00:00
"github.com/stretchr/testify/assert"
)
func TestList(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := query.New(cfg)
config := "{\"path\":\"example_path\"}"
q := &query.Query{
ID: uuid.New(),
Type: resultprocessor.TypeJsonExtractor,
ActiveVersion: int32(1),
LatestVersion: int32(1),
2025-01-20 13:31:48 +00:00
RequiredQueryIDs: &[]uuid.UUID{
uuid.New(),
},
2025-01-20 13:31:48 +00:00
Config: &config,
}
2025-01-20 13:31:48 +00:00
dbReqIDs := database.MustToDBUUIDArray(*q.RequiredQueryIDs)
pool.ExpectQuery("name: ListQueries :many").WithArgs().WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(database.MustToDBUUID(q.ID), repository.QuerytypeJsonExtractor, q.ActiveVersion, q.LatestVersion, []byte(config), dbReqIDs),
)
resList, err := svc.List(ctx)
assert.NoError(t, err)
assert.EqualExportedValues(t, []*query.Query{q}, resList)
}
func TestListById(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
svc := query.New(cfg)
2025-01-06 14:40:43 +00:00
config := "{\"path\":\"example_path\"}"
2025-01-06 15:31:39 +00:00
q := &query.Query{
2025-01-06 14:40:43 +00:00
ID: uuid.New(),
Type: resultprocessor.TypeJsonExtractor,
2025-01-06 14:40:43 +00:00
ActiveVersion: int32(1),
LatestVersion: int32(1),
2025-01-20 13:31:48 +00:00
RequiredQueryIDs: &[]uuid.UUID{
2025-01-06 14:40:43 +00:00
uuid.New(),
},
2025-01-20 13:31:48 +00:00
Config: &config,
2025-01-06 14:40:43 +00:00
}
2025-01-20 13:31:48 +00:00
dbReqIDs := database.MustToDBUUIDArray(*q.RequiredQueryIDs)
2025-01-06 14:40:43 +00:00
pool.ExpectQuery("name: ListQueriesById :many").WithArgs([]pgtype.UUID{database.MustToDBUUID(q.ID)}).WillReturnRows(
2025-01-06 14:40:43 +00:00
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(database.MustToDBUUID(q.ID), repository.QuerytypeJsonExtractor, q.ActiveVersion, q.LatestVersion, []byte(config), dbReqIDs),
)
resList, err := svc.ListById(ctx, []uuid.UUID{q.ID})
assert.NoError(t, err)
2025-01-06 14:40:43 +00:00
2025-01-06 15:31:39 +00:00
assert.EqualExportedValues(t, []*query.Query{q}, resList)
2025-01-06 14:40:43 +00:00
}