2025-01-07 16:30:45 +00:00
|
|
|
package query_test
|
2025-01-06 14:40:43 +00:00
|
|
|
|
|
|
|
|
import (
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-01-06 14:40:43 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/query"
|
2025-01-29 11:52:37 +00:00
|
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
2025-01-31 13:43:55 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-01-06 14:40:43 +00:00
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2025-01-06 14:40:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-01-06 14:40:43 +00:00
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-31 13:43:55 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-02-11 15:22:59 +00:00
|
|
|
svc := query.New(cfg)
|
2025-01-15 12:19:49 +00:00
|
|
|
|
|
|
|
|
config := "{\"path\":\"example_path\"}"
|
|
|
|
|
q := &query.Query{
|
|
|
|
|
ID: uuid.New(),
|
2025-01-29 11:52:37 +00:00
|
|
|
Type: resultprocessor.TypeJsonExtractor,
|
2025-01-15 12:19:49 +00:00
|
|
|
ActiveVersion: int32(1),
|
|
|
|
|
LatestVersion: int32(1),
|
2025-01-20 13:31:48 +00:00
|
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
2025-01-15 12:19:49 +00:00
|
|
|
uuid.New(),
|
|
|
|
|
},
|
2025-01-20 13:31:48 +00:00
|
|
|
Config: &config,
|
2025-01-15 12:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
dbReqIDs := *q.RequiredQueryIDs
|
2025-01-15 12:19:49 +00:00
|
|
|
|
|
|
|
|
pool.ExpectQuery("name: ListQueries :many").WithArgs().WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(q.ID, repository.QuerytypeJsonExtractor, q.ActiveVersion, q.LatestVersion, []byte(config), dbReqIDs),
|
2025-01-15 12:19:49 +00:00
|
|
|
)
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
resList, err := svc.List(ctx)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-15 12:19:49 +00:00
|
|
|
|
|
|
|
|
assert.EqualExportedValues(t, []*query.Query{q}, resList)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
func TestListById(t *testing.T) {
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-01-15 12:19:49 +00:00
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-31 13:43:55 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-02-11 15:22:59 +00:00
|
|
|
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(),
|
2025-01-29 11:52:37 +00:00
|
|
|
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-03-20 11:06:41 +00:00
|
|
|
dbReqIDs := *q.RequiredQueryIDs
|
2025-01-06 14:40:43 +00:00
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: ListQueriesById :many").WithArgs([]uuid.UUID{q.ID}).WillReturnRows(
|
2025-01-06 14:40:43 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(q.ID, repository.QuerytypeJsonExtractor, q.ActiveVersion, q.LatestVersion, []byte(config), dbReqIDs),
|
2025-01-06 14:40:43 +00:00
|
|
|
)
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
resList, err := svc.ListById(ctx, []uuid.UUID{q.ID})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.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
|
|
|
}
|