53ea7d34e6
Start adding Textract + UUID changes * base * startclient * ts * short * tests
150 lines
3.8 KiB
Go
150 lines
3.8 KiB
Go
package queryapi_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
queryapi "queryorchestration/api/queryAPI"
|
|
"queryorchestration/internal/client"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/serviceconfig"
|
|
|
|
"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 TestCreateClient(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(&queryapi.Services{
|
|
Client: client.New(cfg),
|
|
})
|
|
|
|
body := queryapi.ClientCreate{
|
|
Name: "example_name",
|
|
Id: "external_id",
|
|
}
|
|
bodyBytes, err := json.Marshal(body)
|
|
require.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)
|
|
|
|
pool.ExpectQuery("name: CreateClient :one").WithArgs(body.Id, body.Name).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(uuid.New()),
|
|
)
|
|
|
|
err = cons.CreateClient(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
|
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", body.Id), rec.Body.String())
|
|
}
|
|
|
|
func TestGetClient(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(&queryapi.Services{
|
|
Client: client.New(cfg),
|
|
})
|
|
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
rec := httptest.NewRecorder()
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
id := "client_id"
|
|
clientUid := uuid.New()
|
|
|
|
ctx.Set("id", id)
|
|
|
|
pool.ExpectQuery("name: GetClientByExternalId :one").WithArgs(id).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "externalId", "name", "canSync"}).
|
|
AddRow(clientUid, "client_id", "client_name", true),
|
|
)
|
|
|
|
err = cons.GetClient(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
var res queryapi.DocClient
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, queryapi.DocClient{
|
|
Id: id,
|
|
Uid: clientUid,
|
|
Name: "client_name",
|
|
CanSync: true,
|
|
}, res)
|
|
}
|
|
|
|
func TestUpdateClient(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(&queryapi.Services{
|
|
Client: client.New(cfg),
|
|
})
|
|
|
|
cs := false
|
|
body := queryapi.ClientUpdate{
|
|
CanSync: &cs,
|
|
}
|
|
bodyBytes, err := json.Marshal(body)
|
|
require.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 := "clientid"
|
|
|
|
ctx.Set("id", id)
|
|
|
|
intid := uuid.New()
|
|
|
|
pool.ExpectQuery("name: GetClientByExternalId :one").WithArgs(id).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "externalId", "name", "canSync"}).
|
|
AddRow(intid, "clientid", "client_name", true),
|
|
)
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(intid).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "externalId", "name", "canSync"}).
|
|
AddRow(intid, "clientid", "client_name", true),
|
|
)
|
|
pool.ExpectBegin()
|
|
pool.ExpectExec("name: AddClientCanSync :exec").WithArgs(*body.CanSync, intid).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
err = cons.UpdateClient(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Empty(t, rec.Body.String())
|
|
}
|