2025-03-17 18:14:15 +00:00
|
|
|
package queryapi_test
|
2025-01-21 18:24:14 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2025-03-05 12:05:46 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
queryapi "queryorchestration/api/queryAPI"
|
2025-01-21 18:24:14 +00:00
|
|
|
"queryorchestration/internal/client"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
2025-01-31 13:43:55 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-01-21 18:24:14 +00:00
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-01-21 18:24:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCreateClient(t *testing.T) {
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-31 13:43:55 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-01-21 18:24:14 +00:00
|
|
|
|
2025-03-18 13:06:42 +00:00
|
|
|
cons := queryapi.NewControllers(&queryapi.Services{
|
2025-03-10 13:00:57 +00:00
|
|
|
Client: client.New(cfg),
|
2025-01-21 18:24:14 +00:00
|
|
|
})
|
|
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
body := queryapi.ClientCreate{
|
2025-01-21 18:24:14 +00:00
|
|
|
Name: "example_name",
|
2025-03-11 16:31:06 +00:00
|
|
|
Id: "external_id",
|
2025-01-21 18:24:14 +00:00
|
|
|
}
|
|
|
|
|
bodyBytes, err := json.Marshal(body)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-21 18:24:14 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2025-03-11 16:31:06 +00:00
|
|
|
pool.ExpectQuery("name: CreateClient :one").WithArgs(body.Id, body.Name).WillReturnRows(
|
2025-01-21 18:24:14 +00:00
|
|
|
pgxmock.NewRows([]string{"id"}).
|
2025-03-11 16:31:06 +00:00
|
|
|
AddRow(database.MustToDBUUID(uuid.New())),
|
2025-01-21 18:24:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
err = cons.CreateClient(ctx)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-21 18:24:14 +00:00
|
|
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
2025-03-11 16:31:06 +00:00
|
|
|
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", body.Id), rec.Body.String())
|
2025-01-21 18:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetClient(t *testing.T) {
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-31 13:43:55 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-01-21 18:24:14 +00:00
|
|
|
|
2025-03-18 13:06:42 +00:00
|
|
|
cons := queryapi.NewControllers(&queryapi.Services{
|
2025-03-10 13:00:57 +00:00
|
|
|
Client: client.New(cfg),
|
2025-01-21 18:24:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
|
|
2025-03-11 16:31:06 +00:00
|
|
|
id := "client_id"
|
|
|
|
|
clientUid := uuid.New()
|
2025-01-21 18:24:14 +00:00
|
|
|
|
|
|
|
|
ctx.Set("id", id)
|
|
|
|
|
|
2025-03-11 16:31:06 +00:00
|
|
|
pool.ExpectQuery("name: GetClientByExternalId :one").WithArgs(id).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "externalId", "name", "canSync"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(clientUid), "client_id", "client_name", true),
|
2025-01-21 18:24:14 +00:00
|
|
|
)
|
|
|
|
|
|
2025-01-24 14:52:56 +00:00
|
|
|
err = cons.GetClient(ctx, id)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-21 18:24:14 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
var res queryapi.DocClient
|
2025-01-21 18:24:14 +00:00
|
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-03-17 18:14:15 +00:00
|
|
|
assert.EqualExportedValues(t, queryapi.DocClient{
|
2025-01-24 14:52:56 +00:00
|
|
|
Id: id,
|
2025-03-11 16:31:06 +00:00
|
|
|
Uid: clientUid,
|
2025-01-21 18:24:14 +00:00
|
|
|
Name: "client_name",
|
|
|
|
|
CanSync: true,
|
|
|
|
|
}, res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUpdateClient(t *testing.T) {
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-31 13:43:55 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-01-21 18:24:14 +00:00
|
|
|
|
2025-03-18 13:06:42 +00:00
|
|
|
cons := queryapi.NewControllers(&queryapi.Services{
|
2025-03-10 13:00:57 +00:00
|
|
|
Client: client.New(cfg),
|
2025-01-21 18:24:14 +00:00
|
|
|
})
|
|
|
|
|
|
2025-01-29 16:26:11 +00:00
|
|
|
cs := false
|
2025-03-17 18:14:15 +00:00
|
|
|
body := queryapi.ClientUpdate{
|
2025-01-29 16:26:11 +00:00
|
|
|
CanSync: &cs,
|
|
|
|
|
}
|
2025-01-21 18:24:14 +00:00
|
|
|
bodyBytes, err := json.Marshal(body)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-21 18:24:14 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2025-03-11 16:31:06 +00:00
|
|
|
id := "clientid"
|
2025-01-21 18:24:14 +00:00
|
|
|
|
|
|
|
|
ctx.Set("id", id)
|
|
|
|
|
|
2025-03-11 16:31:06 +00:00
|
|
|
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),
|
2025-01-21 18:24:14 +00:00
|
|
|
)
|
2025-01-29 16:26:11 +00:00
|
|
|
pool.ExpectBegin()
|
2025-03-11 16:31:06 +00:00
|
|
|
pool.ExpectExec("name: AddClientCanSync :exec").WithArgs(*body.CanSync, database.MustToDBUUID(intid)).
|
2025-01-21 18:24:14 +00:00
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
2025-01-29 16:26:11 +00:00
|
|
|
pool.ExpectCommit()
|
2025-01-21 18:24:14 +00:00
|
|
|
|
2025-01-24 14:52:56 +00:00
|
|
|
err = cons.UpdateClient(ctx, id)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-21 18:24:14 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
|
assert.Empty(t, rec.Body.String())
|
|
|
|
|
}
|