2025-01-24 14:52:56 +00:00
|
|
|
package queryservice_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
queryservice "queryorchestration/api/queryService"
|
|
|
|
|
"queryorchestration/internal/client"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
2025-02-12 19:00:25 +00:00
|
|
|
cleanversion "queryorchestration/internal/document/clean/version"
|
|
|
|
|
textversion "queryorchestration/internal/document/text/version"
|
2025-01-24 14:52:56 +00:00
|
|
|
"queryorchestration/internal/job"
|
|
|
|
|
"queryorchestration/internal/job/collector"
|
2025-01-31 13:43:55 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-01-24 14:52:56 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
|
"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-24 14:52:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCreateJob(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-24 14:52:56 +00:00
|
|
|
|
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
2025-01-31 13:43:55 +00:00
|
|
|
Job: job.New(cfg, &job.Services{
|
|
|
|
|
Collector: collector.New(cfg, &collector.Services{
|
2025-02-12 19:00:25 +00:00
|
|
|
CleanVersion: cleanversion.New(cfg),
|
|
|
|
|
TextVersion: textversion.New(cfg),
|
2025-01-24 14:52:56 +00:00
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
body := queryservice.JobCreate{
|
|
|
|
|
ClientId: uuid.New(),
|
|
|
|
|
}
|
|
|
|
|
bodyBytes, err := json.Marshal(body)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-24 14:52:56 +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()
|
|
|
|
|
|
2025-02-07 12:12:51 +00:00
|
|
|
pool.ExpectQuery("name: CreateJob :one").WithArgs(database.MustToDBUUID(body.ClientId)).WillReturnRows(
|
2025-01-24 14:52:56 +00:00
|
|
|
pgxmock.NewRows([]string{"id"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(id)),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
2025-02-20 19:02:44 +00:00
|
|
|
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
|
2025-02-17 14:30:37 +00:00
|
|
|
pgxmock.NewRows([]string{"version"}).
|
|
|
|
|
AddRow(int32(1)),
|
|
|
|
|
)
|
2025-02-20 19:02:44 +00:00
|
|
|
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(database.MustToDBUUID(id), int32(1)).
|
2025-02-17 11:44:51 +00:00
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
2025-01-24 14:52:56 +00:00
|
|
|
pool.ExpectCommit()
|
|
|
|
|
|
|
|
|
|
err = cons.CreateJob(ctx)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-24 14:52:56 +00:00
|
|
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
|
|
|
|
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", id), rec.Body.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetJob(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-24 14:52:56 +00:00
|
|
|
|
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
2025-01-31 13:43:55 +00:00
|
|
|
Job: job.New(cfg, &job.Services{
|
|
|
|
|
Client: client.New(cfg),
|
2025-01-24 14:52:56 +00:00
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
|
|
j := job.Job{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
ClientID: uuid.New(),
|
|
|
|
|
CanSync: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
|
|
|
|
|
)
|
2025-02-20 19:02:44 +00:00
|
|
|
pool.ExpectQuery("name: IsJobSynced :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"issynced"}).
|
|
|
|
|
AddRow(true),
|
|
|
|
|
)
|
2025-01-24 14:52:56 +00:00
|
|
|
|
|
|
|
|
err = cons.GetJob(ctx, j.ID)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-24 14:52:56 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
|
|
|
|
|
|
var res queryservice.Job
|
|
|
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-24 14:52:56 +00:00
|
|
|
assert.EqualExportedValues(t, queryservice.Job{
|
|
|
|
|
Id: j.ID,
|
|
|
|
|
ClientId: j.ClientID,
|
|
|
|
|
CanSync: j.CanSync,
|
2025-02-20 19:02:44 +00:00
|
|
|
Status: queryservice.INSYNC,
|
2025-01-24 14:52:56 +00:00
|
|
|
}, res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUpdateJob(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-24 14:52:56 +00:00
|
|
|
|
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
2025-01-31 13:43:55 +00:00
|
|
|
Job: job.New(cfg, &job.Services{
|
|
|
|
|
Client: client.New(cfg),
|
2025-01-24 14:52:56 +00:00
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
j := job.Job{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
ClientID: uuid.New(),
|
|
|
|
|
CanSync: false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ucs := !j.CanSync
|
|
|
|
|
body := queryservice.JobUpdate{
|
|
|
|
|
CanSync: &ucs,
|
|
|
|
|
}
|
|
|
|
|
bodyBytes, err := json.Marshal(body)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-24 14:52:56 +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)
|
|
|
|
|
|
|
|
|
|
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
|
|
|
|
|
)
|
2025-01-29 16:26:11 +00:00
|
|
|
pool.ExpectBegin()
|
|
|
|
|
pool.ExpectExec("name: AddJobCanSync :exec").WithArgs(*body.CanSync, database.MustToDBUUID(j.ID)).
|
2025-01-24 14:52:56 +00:00
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
2025-01-29 16:26:11 +00:00
|
|
|
pool.ExpectCommit()
|
2025-01-24 14:52:56 +00:00
|
|
|
|
|
|
|
|
err = cons.UpdateJob(ctx, j.ID)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-24 14:52:56 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
|
assert.Empty(t, rec.Body.String())
|
|
|
|
|
}
|