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,170 @@
|
||||
package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
"queryorchestration/internal/collector"
|
||||
collectorset "queryorchestration/internal/collector/set"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/clientsync"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"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"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSetCollector(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ClientSyncConfig
|
||||
}{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
|
||||
current := collector.Collector{
|
||||
ClientID: uuid.New(),
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 4,
|
||||
}
|
||||
|
||||
av := int32(2)
|
||||
cv := int64(1)
|
||||
body := queryapi.CollectorSet{
|
||||
ActiveVersion: &av,
|
||||
MinimumCleanerVersion: &cv,
|
||||
Fields: &[]queryapi.CollectorField{
|
||||
{
|
||||
Name: "a",
|
||||
QueryId: uuid.New(),
|
||||
},
|
||||
},
|
||||
}
|
||||
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)
|
||||
|
||||
cons := queryapi.NewControllers(validator.New(), &queryapi.Services{
|
||||
CollectorSet: collectorset.New(cfg, &collectorset.Services{
|
||||
Collector: collector.New(cfg),
|
||||
}),
|
||||
})
|
||||
|
||||
id := "clientid"
|
||||
|
||||
ctx.Set("id", id)
|
||||
|
||||
pool.ExpectQuery("name: GetClientByExternalId :one").WithArgs(id).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "externalId", "name", "can_sync"}).
|
||||
AddRow(database.MustToDBUUID(current.ClientID), id, "name", true),
|
||||
)
|
||||
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(database.MustToDBUUID(current.ClientID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(database.MustToDBUUID(current.ClientID), current.MinCleanVersion, current.MinTextVersion, current.ActiveVersion, current.LatestVersion, []byte("")),
|
||||
)
|
||||
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs([]pgtype.UUID{database.MustToDBUUID((*body.Fields)[0].QueryId)}).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"exist"}).
|
||||
AddRow(true),
|
||||
)
|
||||
pool.ExpectBeginTx(pgx.TxOptions{})
|
||||
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(database.MustToDBUUID(current.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(int32(5)),
|
||||
)
|
||||
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), int32(2)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), int32(5), *body.MinimumCleanerVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(current.ClientID), "a", database.MustToDBUUID((*body.Fields)[0].QueryId), int32(5)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", current.ClientID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
err = cons.SetCollectorByClientId(ctx, id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Empty(t, rec.Body.String())
|
||||
}
|
||||
|
||||
func TestGetCollectorByclientId(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
ctx := e.NewContext(req, rec)
|
||||
|
||||
svc := collector.New(cfg)
|
||||
cons := queryapi.NewControllers(validator.New(), &queryapi.Services{
|
||||
Collector: svc,
|
||||
})
|
||||
|
||||
coll := collector.Collector{
|
||||
ClientID: uuid.New(),
|
||||
MinCleanVersion: 1,
|
||||
MinTextVersion: 2,
|
||||
}
|
||||
|
||||
id := "clientid"
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByClientExternalID :one").WithArgs(id).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(database.MustToDBUUID(coll.ClientID), coll.MinCleanVersion, coll.MinTextVersion, int32(1), int32(2), []byte("")),
|
||||
)
|
||||
|
||||
err = cons.GetCollectorByClientId(ctx, id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.Collector
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualExportedValues(t, queryapi.Collector{
|
||||
ClientId: id,
|
||||
MinimumCleanerVersion: coll.MinCleanVersion,
|
||||
MinimumTextVersion: coll.MinTextVersion,
|
||||
ActiveVersion: int32(1),
|
||||
LatestVersion: int32(2),
|
||||
Fields: []queryapi.CollectorField{},
|
||||
}, res)
|
||||
}
|
||||
Reference in New Issue
Block a user