7001ca854c
Queuing Changes and Cfg Testing * staarting * staarting * startedpush * note * save * mocking * removederrs * fixtests * cleanuperrs * newenvsetup * preppingtests * queue * mmovetocfgpassunittests * sortoutconfig * passinginteg * deps * fixtests
288 lines
9.1 KiB
Go
288 lines
9.1 KiB
Go
package queryservice_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
queryservice "queryorchestration/api/queryService"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/document"
|
|
documentclean "queryorchestration/internal/document/clean"
|
|
documenttext "queryorchestration/internal/document/text"
|
|
"queryorchestration/internal/job/collector"
|
|
"queryorchestration/internal/query"
|
|
"queryorchestration/internal/query/result"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCreateQuery(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Query: query.New(cfg, &query.Services{}),
|
|
})
|
|
|
|
body := queryservice.QueryCreate{
|
|
Type: queryservice.CONTEXTFULL,
|
|
}
|
|
bodyBytes, err := json.Marshal(body)
|
|
assert.NoError(t, err)
|
|
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
rec := httptest.NewRecorder()
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
id := uuid.New()
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
pool.ExpectQuery("-- name: CreateQuery :one").WithArgs(repository.QuerytypeContextFull).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(database.MustToDBUUID(id)),
|
|
)
|
|
pool.ExpectCommit()
|
|
|
|
err = cons.CreateQuery(ctx)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
|
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", id), rec.Body.String())
|
|
}
|
|
|
|
func TestListQueries(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Query: query.New(cfg, &query.Services{}),
|
|
})
|
|
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
rec := httptest.NewRecorder()
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
id := uuid.New()
|
|
|
|
pool.ExpectQuery("name: ListQueries :many").WithArgs().WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
|
AddRow(database.MustToDBUUID(id), repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []pgtype.UUID{}),
|
|
)
|
|
|
|
err = cons.ListQueries(ctx)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
var res queryservice.ListQueries
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, res.Queries)
|
|
assert.ElementsMatch(t, res.Queries, []queryservice.Query{
|
|
{
|
|
Id: id,
|
|
Type: queryservice.CONTEXTFULL,
|
|
ActiveVersion: 1,
|
|
LatestVersion: 2,
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestGetQuery(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Query: query.New(cfg, &query.Services{}),
|
|
})
|
|
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
rec := httptest.NewRecorder()
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
id := uuid.New()
|
|
|
|
ctx.Set("id", id)
|
|
|
|
pool.ExpectQuery("-- name: GetQuery :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
|
AddRow(database.MustToDBUUID(id), repository.QuerytypeContextFull, int32(1), int32(2), nil, []pgtype.UUID{}),
|
|
)
|
|
|
|
err = cons.GetQuery(ctx, id)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
var res queryservice.Query
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
|
assert.NoError(t, err)
|
|
assert.EqualExportedValues(t, queryservice.Query{
|
|
Id: id,
|
|
Type: queryservice.CONTEXTFULL,
|
|
ActiveVersion: 1,
|
|
LatestVersion: 2,
|
|
}, res)
|
|
}
|
|
|
|
func TestUpdateQuery(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Query: query.New(cfg, &query.Services{}),
|
|
})
|
|
|
|
av := int32(2)
|
|
body := queryservice.QueryUpdate{
|
|
ActiveVersion: &av,
|
|
}
|
|
bodyBytes, err := json.Marshal(body)
|
|
assert.NoError(t, err)
|
|
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
rec := httptest.NewRecorder()
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
id := uuid.New()
|
|
|
|
ctx.Set("id", id)
|
|
|
|
pool.ExpectQuery("-- name: GetQuery :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
|
AddRow(database.MustToDBUUID(id), repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []pgtype.UUID{}),
|
|
)
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
pool.ExpectExec("name: UpdateQuery :exec").WithArgs(av, int32(3), database.MustToDBUUID(id)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
err = cons.UpdateQuery(ctx, id)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Empty(t, rec.Body.String())
|
|
}
|
|
|
|
func TestTestQuery(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
docsvc := document.New(cfg)
|
|
col := collector.New(cfg, &collector.Services{
|
|
Clean: documentclean.New(),
|
|
Text: documenttext.New(),
|
|
})
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Collector: col,
|
|
Query: query.New(cfg, &query.Services{
|
|
Document: docsvc,
|
|
Collector: col,
|
|
Result: result.New(cfg),
|
|
}),
|
|
})
|
|
|
|
coll := collector.Collector{
|
|
ID: uuid.New(),
|
|
JobID: uuid.New(),
|
|
}
|
|
doc := document.Document{
|
|
ID: uuid.New(),
|
|
JobID: coll.JobID,
|
|
Hash: "example_hash",
|
|
Location: "example_location",
|
|
}
|
|
params := &query.Test{
|
|
QueryID: uuid.New(),
|
|
DocumentID: doc.ID,
|
|
QueryVersion: int32(3),
|
|
}
|
|
body := queryservice.QueryTestRequest{
|
|
DocumentId: params.DocumentID,
|
|
QueryVersion: params.QueryVersion,
|
|
}
|
|
bodyBytes, err := json.Marshal(body)
|
|
assert.NoError(t, err)
|
|
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
rec := httptest.NewRecorder()
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
reqID := database.MustToDBUUID(uuid.New())
|
|
pool.ExpectQuery("name: GetDocument :one").WithArgs(database.MustToDBUUID(doc.ID)).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "jobId", "hash", "location"}).
|
|
AddRow(database.MustToDBUUID(doc.ID), database.MustToDBUUID(doc.JobID), doc.Hash, doc.Location),
|
|
)
|
|
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(database.MustToDBUUID(doc.JobID)).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
|
AddRow(database.MustToDBUUID(coll.ID), database.MustToDBUUID(doc.JobID), coll.MinCleanVersion, coll.MinTextVersion, int32(1), int32(2), []byte("")),
|
|
)
|
|
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(database.MustToDBUUID(params.QueryID), params.QueryVersion).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
|
AddRow(database.MustToDBUUID(params.QueryID), repository.QuerytypeJsonExtractor, int32(1), params.QueryVersion+1, []byte("{\"path\":\"oldkey\"}"), []pgtype.UUID{reqID}),
|
|
)
|
|
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(database.MustToDBUUID(params.QueryID), params.QueryVersion, database.MustToDBUUID(params.DocumentID), coll.MinCleanVersion, coll.MinTextVersion).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"queryId", "value", "type"}).
|
|
AddRow(reqID, "{\"mykey\":\"example_value\",\"oldkey\":\"old_value\"}", repository.QuerytypeContextFull),
|
|
)
|
|
pool.ExpectQuery("name: GetQueryConfig :one").WithArgs(database.MustToDBUUID(params.QueryID), params.QueryVersion).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "config"}).
|
|
AddRow(pgtype.UUID{}, []byte("{\"path\":\"oldkey\"}")),
|
|
)
|
|
|
|
err = cons.TestQuery(ctx, params.QueryID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
var res queryservice.QueryTestResponse
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
|
assert.NoError(t, err)
|
|
assert.EqualExportedValues(t, queryservice.QueryTestResponse{
|
|
Value: "old_value",
|
|
}, res)
|
|
}
|