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,151 @@
|
||||
package queryapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
queryapi "queryorchestration/api/queryAPI"
|
||||
"queryorchestration/internal/client"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"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(validator.New(), &queryapi.Services{
|
||||
Client: client.New(cfg),
|
||||
})
|
||||
|
||||
body := queryapi.ClientCreate{
|
||||
Name: "example_name",
|
||||
Id: "external_id",
|
||||
}
|
||||
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)
|
||||
|
||||
pool.ExpectQuery("name: CreateClient :one").WithArgs(body.Id, body.Name).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}).
|
||||
AddRow(database.MustToDBUUID(uuid.New())),
|
||||
)
|
||||
|
||||
err = cons.CreateClient(ctx)
|
||||
assert.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(validator.New(), &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(database.MustToDBUUID(clientUid), "client_id", "client_name", true),
|
||||
)
|
||||
|
||||
err = cons.GetClient(ctx, id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var res queryapi.DocClient
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
||||
assert.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(validator.New(), &queryapi.Services{
|
||||
Client: client.New(cfg),
|
||||
})
|
||||
|
||||
cs := false
|
||||
body := queryapi.ClientUpdate{
|
||||
CanSync: &cs,
|
||||
}
|
||||
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 := "clientid"
|
||||
|
||||
ctx.Set("id", id)
|
||||
|
||||
intid := uuid.New()
|
||||
|
||||
pool.ExpectQuery("name: GetClientByExternalId :one").WithArgs(id).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "externalId", "name", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(intid), "clientid", "client_name", true),
|
||||
)
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(intid)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "externalId", "name", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(intid), "clientid", "client_name", true),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
pool.ExpectExec("name: AddClientCanSync :exec").WithArgs(*body.CanSync, database.MustToDBUUID(intid)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = cons.UpdateClient(ctx, id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Empty(t, rec.Body.String())
|
||||
}
|
||||
Reference in New Issue
Block a user