223 lines
6.0 KiB
Go
223 lines
6.0 KiB
Go
package document_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
contextfull "queryorchestration/internal/contextFull"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
jsonextractor "queryorchestration/internal/jsonExtractor"
|
|
queryprocessor "queryorchestration/internal/queryProcessor"
|
|
"queryorchestration/internal/result"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestJSONProcess(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,
|
|
}
|
|
|
|
extractor := jsonextractor.NewExtractor(db)
|
|
|
|
query := &queryprocessor.Query{
|
|
ID: uuid.New(),
|
|
Type: queryprocessor.TypeJsonExtractor,
|
|
RequiredQueryIDs: []uuid.UUID{},
|
|
Version: int32(1),
|
|
}
|
|
entryValue := "value"
|
|
|
|
jsonString := fmt.Sprintf("{\"key\": \"%s\"}", entryValue)
|
|
values := []result.Value{
|
|
contextfull.NewResult(jsonString),
|
|
}
|
|
|
|
config := "{\"path\":\"key\"}"
|
|
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte(config)),
|
|
)
|
|
|
|
value, err := extractor.Process(ctx, query, values)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, entryValue, value)
|
|
|
|
entryValue = ""
|
|
jsonString = fmt.Sprintf("{\"key\": \"%s\"}", entryValue)
|
|
values = []result.Value{
|
|
contextfull.NewResult(jsonString),
|
|
}
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte(config)),
|
|
)
|
|
|
|
value, err = extractor.Process(ctx, query, values)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, entryValue, value)
|
|
|
|
entryValue = "1"
|
|
jsonString = fmt.Sprintf("{\"key\": %s", entryValue)
|
|
values = []result.Value{
|
|
contextfull.NewResult(jsonString),
|
|
}
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte(config)),
|
|
)
|
|
|
|
value, err = extractor.Process(ctx, query, values)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, entryValue, value)
|
|
}
|
|
|
|
func TestJSONProcessJSON(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,
|
|
}
|
|
|
|
extractor := jsonextractor.NewExtractor(db)
|
|
|
|
query := &queryprocessor.Query{
|
|
ID: uuid.New(),
|
|
Type: queryprocessor.TypeJsonExtractor,
|
|
RequiredQueryIDs: []uuid.UUID{},
|
|
Version: int32(1),
|
|
}
|
|
entryValue := "value"
|
|
|
|
jsonString := fmt.Sprintf("{\"key\": \"%s\"}", entryValue)
|
|
values := []result.Value{
|
|
contextfull.NewResult(jsonString),
|
|
}
|
|
|
|
config := "{\"path\":\"invalid_key\"}"
|
|
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte(config)),
|
|
)
|
|
|
|
value, err := extractor.Process(ctx, query, values)
|
|
assert.EqualError(t, err, "JSON path does not exist: invalid_key")
|
|
assert.Empty(t, value)
|
|
|
|
config = ""
|
|
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte(config)),
|
|
)
|
|
|
|
value, err = extractor.Process(ctx, query, values)
|
|
assert.EqualError(t, err, "unexpected end of JSON input")
|
|
assert.Empty(t, value)
|
|
|
|
config = "{\"path\":\"\"}"
|
|
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte(config)),
|
|
)
|
|
|
|
value, err = extractor.Process(ctx, query, values)
|
|
assert.EqualError(t, err, "JSON path does not exist: ")
|
|
assert.Empty(t, value)
|
|
|
|
config = "{\"path\":}"
|
|
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte(config)),
|
|
)
|
|
|
|
value, err = extractor.Process(ctx, query, values)
|
|
assert.EqualError(t, err, "invalid character '}' looking for beginning of value")
|
|
assert.Empty(t, value)
|
|
|
|
config = "{\"path\":\"key\"}"
|
|
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(query.ID), query.Version).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}),
|
|
)
|
|
|
|
value, err = extractor.Process(ctx, query, values)
|
|
assert.EqualError(t, err, "no rows in result set")
|
|
assert.Empty(t, value)
|
|
}
|
|
|
|
func TestJSONProcessResults(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,
|
|
}
|
|
|
|
extractor := jsonextractor.NewExtractor(db)
|
|
|
|
query := &queryprocessor.Query{
|
|
ID: uuid.New(),
|
|
Type: queryprocessor.TypeJsonExtractor,
|
|
RequiredQueryIDs: []uuid.UUID{},
|
|
Version: int32(1),
|
|
}
|
|
|
|
results := []result.Value{}
|
|
value, err := extractor.Process(ctx, query, results)
|
|
assert.EqualError(t, err, "JSON Extraction requires 1 result")
|
|
assert.Empty(t, value)
|
|
|
|
results = []result.Value{
|
|
contextfull.NewResult(""),
|
|
contextfull.NewResult(""),
|
|
}
|
|
value, err = extractor.Process(ctx, query, results)
|
|
assert.EqualError(t, err, "JSON Extraction requires 1 result")
|
|
assert.Empty(t, value)
|
|
|
|
results = []result.Value{
|
|
contextfull.NewResult(""),
|
|
contextfull.NewResult(""),
|
|
contextfull.NewResult(""),
|
|
}
|
|
value, err = extractor.Process(ctx, query, results)
|
|
assert.EqualError(t, err, "JSON Extraction requires 1 result")
|
|
assert.Empty(t, value)
|
|
}
|