Files
query-orchestration/api/queryAPI/documents_test.go
T

122 lines
3.1 KiB
Go
Raw Normal View History

package queryapi_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
queryapi "queryorchestration/api/queryAPI"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/serviceconfig/queue/clientsync"
queuemock "queryorchestration/mocks/queue"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListDocumentsByClientId(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
clientId := "clientid"
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
cons := queryapi.NewControllers(&queryapi.Services{
Document: document.New(cfg),
})
ctx.Set("id", clientId)
doc := queryapi.ListDocuments{
{
Id: uuid.New(),
Hash: "hash",
},
}
pool.ExpectQuery("name: ListDocumentsByClientExternalId :many").WithArgs(clientId).
WillReturnRows(
pgxmock.NewRows([]string{"id", "hash"}).
AddRow(database.MustToDBUUID(doc[0].Id), "hash"),
)
err = cons.ListDocumentsByClientId(ctx, clientId)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.ListDocuments
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.NoError(t, err)
assert.EqualExportedValues(t, doc, res)
}
func TestGetDocument(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
docId := uuid.New()
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
cons := queryapi.NewControllers(&queryapi.Services{
Document: document.New(cfg),
})
ctx.Set("id", docId)
doc := queryapi.Document{
Id: docId,
ClientId: "externalID",
Hash: "example",
Fields: map[string]interface{}{
"json": "hello",
},
}
pool.ExpectQuery("name: GetDocumentExternal :one").WithArgs(database.MustToDBUUID(docId)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "hash", "fields"}).
AddRow(database.MustToDBUUID(doc.Id), "externalID", "example", []byte(`{"json": "hello"}`)),
)
err = cons.GetDocument(ctx, docId)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.Document
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.NoError(t, err)
assert.EqualExportedValues(t, doc, res)
}