2025-01-07 16:30:45 +00:00
|
|
|
package query_test
|
2025-01-03 10:35:42 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/query"
|
2025-01-17 12:00:32 +00:00
|
|
|
queryprocessor "queryorchestration/internal/query/processor"
|
2025-01-03 10:35:42 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2025-01-06 13:30:20 +00:00
|
|
|
pool, err := pgxmock.NewPool()
|
2025-01-03 10:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
|
|
|
}
|
2025-01-06 13:30:20 +00:00
|
|
|
queries := repository.New(pool)
|
|
|
|
|
db := &database.Connection{
|
|
|
|
|
Queries: queries,
|
|
|
|
|
Pool: pool,
|
|
|
|
|
}
|
|
|
|
|
svc := query.New(db)
|
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(),
|
|
|
|
|
Type: queryprocessor.TypeJsonExtractor,
|
|
|
|
|
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-01-20 13:31:48 +00:00
|
|
|
dbReqIDs := database.MustToDBUUIDArray(*query.RequiredQueryIDs)
|
2025-01-03 10:35:42 +00:00
|
|
|
|
2025-01-06 13:30:20 +00:00
|
|
|
pool.ExpectQuery("name: GetQuery :one").WithArgs(database.MustToDBUUID(query.ID)).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(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)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
assert.EqualExportedValues(t, query, *returnQuery)
|
|
|
|
|
}
|
2025-01-13 15:02:43 +00:00
|
|
|
|
|
|
|
|
func TestFullActiveQueryEmpty(t *testing.T) {
|
|
|
|
|
dbQuery := &repository.Fullactivequery{
|
|
|
|
|
ID: database.MustToDBUUID(uuid.New()),
|
|
|
|
|
Type: repository.QuerytypeContextFull,
|
|
|
|
|
Activeversion: int32(1),
|
|
|
|
|
Latestversion: int32(2),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := query.ParseFullActiveQuery(dbQuery)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.EqualExportedValues(t, query.Query{
|
2025-01-20 13:31:48 +00:00
|
|
|
ID: database.MustToUUID(dbQuery.ID),
|
|
|
|
|
Type: queryprocessor.TypeContextFull,
|
|
|
|
|
ActiveVersion: int32(1),
|
|
|
|
|
LatestVersion: int32(2),
|
2025-01-13 15:02:43 +00:00
|
|
|
}, *out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFullActiveQueryWithNullUUID(t *testing.T) {
|
|
|
|
|
dbQuery := &repository.Fullactivequery{
|
|
|
|
|
ID: database.MustToDBUUID(uuid.New()),
|
|
|
|
|
Type: repository.QuerytypeContextFull,
|
|
|
|
|
Activeversion: int32(1),
|
|
|
|
|
Latestversion: int32(2),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := query.ParseFullActiveQuery(dbQuery)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.EqualExportedValues(t, query.Query{
|
2025-01-20 13:31:48 +00:00
|
|
|
ID: database.MustToUUID(dbQuery.ID),
|
|
|
|
|
Type: queryprocessor.TypeContextFull,
|
|
|
|
|
ActiveVersion: int32(1),
|
|
|
|
|
LatestVersion: int32(2),
|
2025-01-13 15:02:43 +00:00
|
|
|
}, *out)
|
|
|
|
|
}
|
2025-01-15 12:19:49 +00:00
|
|
|
|
|
|
|
|
func TestFullActiveQueryArray(t *testing.T) {
|
|
|
|
|
dbQueries := []*repository.Fullactivequery{
|
|
|
|
|
{
|
|
|
|
|
ID: database.MustToDBUUID(uuid.New()),
|
|
|
|
|
Type: repository.QuerytypeContextFull,
|
|
|
|
|
Activeversion: int32(1),
|
|
|
|
|
Latestversion: int32(2),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := query.ParseFullActiveQueryArray(dbQueries)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.EqualExportedValues(t, []*query.Query{
|
|
|
|
|
{
|
2025-01-20 13:31:48 +00:00
|
|
|
ID: database.MustToUUID(dbQueries[0].ID),
|
|
|
|
|
Type: queryprocessor.TypeContextFull,
|
|
|
|
|
ActiveVersion: int32(1),
|
|
|
|
|
LatestVersion: int32(2),
|
2025-01-15 12:19:49 +00:00
|
|
|
},
|
|
|
|
|
}, out)
|
|
|
|
|
}
|