Merged in feature/docresult (pull request #105)
Document Result Endpoint * testing * cleantesting * progress * query * lint * readme * dockerclient * api * passtest * tests * test
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
"queryorchestration/internal/collector"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/query"
|
||||
"queryorchestration/internal/query/result"
|
||||
querytest "queryorchestration/internal/query/test"
|
||||
queryupdate "queryorchestration/internal/query/update"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/queryversionsync"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||||
"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"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestCreateQuery(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
cons := queryapi.NewControllers(validator.New(), &queryapi.Services{
|
||||
Query: query.New(cfg),
|
||||
})
|
||||
|
||||
body := queryapi.QueryCreate{
|
||||
Type: queryapi.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.ExpectQuery("name: AddLatestQueryVersion :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(int32(1)),
|
||||
)
|
||||
pool.ExpectExec("name: AddActiveQueryVersion :exec").WithArgs(database.MustToDBUUID(id), int32(1)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
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()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
cons := queryapi.NewControllers(validator.New(), &queryapi.Services{
|
||||
Query: query.New(cfg),
|
||||
})
|
||||
|
||||
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 queryapi.ListQueries
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, res.Queries)
|
||||
assert.ElementsMatch(t, res.Queries, []queryapi.Query{
|
||||
{
|
||||
Id: id,
|
||||
Type: queryapi.CONTEXTFULL,
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 2,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetQuery(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
cons := queryapi.NewControllers(validator.New(), &queryapi.Services{
|
||||
Query: query.New(cfg),
|
||||
})
|
||||
|
||||
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 queryapi.Query
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.Query{
|
||||
Id: id,
|
||||
Type: queryapi.CONTEXTFULL,
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 2,
|
||||
}, res)
|
||||
}
|
||||
|
||||
func TestUpdateQuery(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &struct {
|
||||
serviceconfig.BaseConfig
|
||||
queryversionsync.QueryVersionSyncConfig
|
||||
}{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.QueryVersionSyncURL = "here"
|
||||
|
||||
cons := queryapi.NewControllers(validator.New(), &queryapi.Services{
|
||||
QueryUpdate: queryupdate.New(cfg, &queryupdate.Services{
|
||||
Query: query.New(cfg),
|
||||
}),
|
||||
})
|
||||
|
||||
av := int32(2)
|
||||
body := queryapi.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: AddActiveQueryVersion :exec").WithArgs(database.MustToDBUUID(id), av).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.QueryVersionSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", id.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
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()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
docsvc := document.New(cfg)
|
||||
col := collector.New(cfg)
|
||||
que := query.New(cfg)
|
||||
cons := queryapi.NewControllers(validator.New(), &queryapi.Services{
|
||||
Collector: col,
|
||||
Query: que,
|
||||
QueryTest: querytest.New(cfg, &querytest.Services{
|
||||
Document: docsvc,
|
||||
Collector: col,
|
||||
Result: result.New(cfg, &result.Services{
|
||||
Query: que,
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
doc := document.DocumentSummary{
|
||||
ID: uuid.New(),
|
||||
}
|
||||
params := &result.Process{
|
||||
QueryID: uuid.New(),
|
||||
DocumentID: doc.ID,
|
||||
QueryVersion: int32(3),
|
||||
}
|
||||
body := queryapi.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: GetQueryWithVersion :one").WithArgs(database.MustToDBUUID(params.QueryID), ¶ms.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}),
|
||||
)
|
||||
strVal := "{\"mykey\":\"example_value\",\"oldkey\":\"old_value\"}"
|
||||
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(database.MustToDBUUID(params.QueryID), ¶ms.QueryVersion, database.MustToDBUUID(params.DocumentID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(database.MustToDBUUID(uuid.New()), reqID, repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(database.MustToDBUUID(params.QueryID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte("{\"path\":\"oldkey\"}")),
|
||||
)
|
||||
|
||||
err = cons.TestQuery(ctx, params.QueryID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.QueryTestResponse
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.QueryTestResponse{
|
||||
Value: "old_value",
|
||||
}, res)
|
||||
}
|
||||
Reference in New Issue
Block a user