Files
query-orchestration/api/queryService/client_test.go
T

172 lines
4.8 KiB
Go
Raw Normal View History

2025-01-21 18:24:14 +00:00
package queryservice_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
2025-03-05 12:05:46 +00:00
"strings"
"testing"
2025-01-21 18:24:14 +00:00
queryservice "queryorchestration/api/queryService"
"queryorchestration/internal/client"
"queryorchestration/internal/collector"
2025-01-21 18:24:14 +00:00
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
cleanversion "queryorchestration/internal/document/clean/version"
textversion "queryorchestration/internal/document/text/version"
"queryorchestration/internal/serviceconfig"
2025-01-21 18:24:14 +00:00
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
2025-01-21 18:24:14 +00:00
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2025-01-21 18:24:14 +00:00
)
func TestCreateClient(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
2025-01-21 18:24:14 +00:00
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Client: client.New(cfg, &client.Services{
Collector: collector.New(cfg, &collector.Services{
TextVersion: textversion.New(cfg),
CleanVersion: cleanversion.New(cfg),
}),
}),
2025-01-21 18:24:14 +00:00
})
body := queryservice.ClientCreate{
Name: "example_name",
}
bodyBytes, err := json.Marshal(body)
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)
id := uuid.New()
pool.ExpectQuery("name: CreateClient :one").WithArgs(body.Name).WillReturnRows(
2025-01-21 18:24:14 +00:00
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(id)),
)
pool.ExpectBeginTx(pgx.TxOptions{})
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
pgxmock.NewRows([]string{"version"}).
AddRow(int32(1)),
)
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(database.MustToDBUUID(id), int32(1)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
2025-01-21 18:24:14 +00:00
err = cons.CreateClient(ctx)
assert.NoError(t, err)
2025-01-21 18:24:14 +00:00
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", 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)
2025-01-21 18:24:14 +00:00
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Client: client.New(cfg, &client.Services{
Collector: collector.New(cfg, &collector.Services{
TextVersion: textversion.New(cfg),
CleanVersion: cleanversion.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)
id := uuid.New()
ctx.Set("id", id)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
2025-01-21 18:24:14 +00:00
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(id), "client_name", true),
)
err = cons.GetClient(ctx, id)
assert.NoError(t, err)
2025-01-21 18:24:14 +00:00
assert.Equal(t, http.StatusOK, rec.Code)
var res queryservice.DocClient
2025-01-21 18:24:14 +00:00
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.NoError(t, err)
assert.EqualExportedValues(t, queryservice.DocClient{
Id: id,
2025-01-21 18:24:14 +00:00
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)
2025-01-21 18:24:14 +00:00
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Client: client.New(cfg, &client.Services{
Collector: collector.New(cfg, &collector.Services{
TextVersion: textversion.New(cfg),
CleanVersion: cleanversion.New(cfg),
}),
}),
2025-01-21 18:24:14 +00:00
})
cs := false
body := queryservice.ClientUpdate{
CanSync: &cs,
}
2025-01-21 18:24:14 +00:00
bodyBytes, err := json.Marshal(body)
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)
id := uuid.New()
ctx.Set("id", id)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
2025-01-21 18:24:14 +00:00
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(id), "client_name", true),
)
pool.ExpectBegin()
pool.ExpectExec("name: AddClientCanSync :exec").WithArgs(*body.CanSync, database.MustToDBUUID(id)).
2025-01-21 18:24:14 +00:00
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
2025-01-21 18:24:14 +00:00
err = cons.UpdateClient(ctx, id)
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())
}