2025-01-07 16:30:45 +00:00
|
|
|
package query_test
|
2025-01-03 10:35:42 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-01-03 10:35:42 +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-03 10:35:42 +00:00
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-01-03 10:35:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2025-01-06 13:30:20 +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-03 10:35:42 +00:00
|
|
|
|
|
|
|
|
config := "{\"path\":\"example_path\"}"
|
|
|
|
|
query := query.Query{
|
2025-01-06 13:30:20 +00:00
|
|
|
ID: uuid.New(),
|
2025-01-29 11:52:37 +00:00
|
|
|
Type: resultprocessor.TypeJsonExtractor,
|
2025-01-06 13:30:20 +00:00
|
|
|
ActiveVersion: int32(1),
|
|
|
|
|
LatestVersion: int32(1),
|
2025-01-20 13:31:48 +00:00
|
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
2025-01-03 10:35:42 +00:00
|
|
|
uuid.New(),
|
|
|
|
|
},
|
2025-01-20 13:31:48 +00:00
|
|
|
Config: &config,
|
2025-01-03 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
dbReqIDs := *query.RequiredQueryIDs
|
2025-01-03 10:35:42 +00:00
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetQuery :one").WithArgs(query.ID).WillReturnRows(
|
2025-01-06 13:30:20 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.ActiveVersion, query.LatestVersion, []byte(config), dbReqIDs),
|
2025-01-03 10:35:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
returnQuery, err := svc.Get(ctx, query.ID)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-03 10:35:42 +00:00
|
|
|
|
|
|
|
|
assert.EqualExportedValues(t, query, *returnQuery)
|
|
|
|
|
}
|
2025-01-13 15:02:43 +00:00
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
func TestGetWithVersion(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
2025-01-13 15:02:43 +00:00
|
|
|
|
2025-01-29 11:52:37 +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-13 15:02:43 +00:00
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
config := "{\"path\":\"example_path\"}"
|
|
|
|
|
query := query.Query{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
Type: resultprocessor.TypeJsonExtractor,
|
2025-01-20 13:31:48 +00:00
|
|
|
ActiveVersion: int32(1),
|
2025-01-29 11:52:37 +00:00
|
|
|
LatestVersion: int32(3),
|
|
|
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
|
|
|
uuid.New(),
|
2025-01-15 12:19:49 +00:00
|
|
|
},
|
2025-01-29 11:52:37 +00:00
|
|
|
Config: &config,
|
2025-01-15 12:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
version := int32(2)
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
dbReqIDs := *query.RequiredQueryIDs
|
2025-01-29 11:52:37 +00:00
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&query.ID, &version).WillReturnRows(
|
2025-01-29 11:52:37 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.ActiveVersion, query.LatestVersion, []byte(config), dbReqIDs),
|
2025-01-29 11:52:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
returnQuery, err := svc.GetWithVersion(ctx, query.ID, version)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-29 11:52:37 +00:00
|
|
|
|
|
|
|
|
assert.EqualExportedValues(t, query, *returnQuery)
|
2025-01-15 12:19:49 +00:00
|
|
|
}
|