3d434eedb8
Job Status Get and DB tidy up * initalquery * tests * shorttests * testing queries * job * solvedthequery * updatingdb * fixingtests * repotests * shorttests * docker * testspassed
193 lines
5.7 KiB
Go
193 lines
5.7 KiB
Go
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"
|
|
cleanversion "queryorchestration/internal/document/clean/version"
|
|
textversion "queryorchestration/internal/document/text/version"
|
|
"queryorchestration/internal/job"
|
|
"queryorchestration/internal/job/collector"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"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"
|
|
)
|
|
|
|
func TestCreateJob(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Job: job.New(cfg, &job.Services{
|
|
Collector: collector.New(cfg, &collector.Services{
|
|
CleanVersion: cleanversion.New(cfg),
|
|
TextVersion: textversion.New(cfg),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
body := queryservice.JobCreate{
|
|
ClientId: uuid.New(),
|
|
}
|
|
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 := uuid.New()
|
|
|
|
pool.ExpectQuery("name: CreateJob :one").WithArgs(database.MustToDBUUID(body.ClientId)).WillReturnRows(
|
|
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()
|
|
|
|
err = cons.CreateJob(ctx)
|
|
assert.NoError(t, err)
|
|
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()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Job: job.New(cfg, &job.Services{
|
|
Client: client.New(cfg),
|
|
}),
|
|
})
|
|
|
|
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),
|
|
)
|
|
pool.ExpectQuery("name: IsJobSynced :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
|
pgxmock.NewRows([]string{"issynced"}).
|
|
AddRow(true),
|
|
)
|
|
|
|
err = cons.GetJob(ctx, j.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
var res queryservice.Job
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
|
assert.NoError(t, err)
|
|
assert.EqualExportedValues(t, queryservice.Job{
|
|
Id: j.ID,
|
|
ClientId: j.ClientID,
|
|
CanSync: j.CanSync,
|
|
Status: queryservice.INSYNC,
|
|
}, res)
|
|
}
|
|
|
|
func TestUpdateJob(t *testing.T) {
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
Job: job.New(cfg, &job.Services{
|
|
Client: client.New(cfg),
|
|
}),
|
|
})
|
|
|
|
j := job.Job{
|
|
ID: uuid.New(),
|
|
ClientID: uuid.New(),
|
|
CanSync: false,
|
|
}
|
|
|
|
ucs := !j.CanSync
|
|
body := queryservice.JobUpdate{
|
|
CanSync: &ucs,
|
|
}
|
|
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: 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),
|
|
)
|
|
pool.ExpectBegin()
|
|
pool.ExpectExec("name: AddJobCanSync :exec").WithArgs(*body.CanSync, database.MustToDBUUID(j.ID)).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
err = cons.UpdateJob(ctx, j.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Empty(t, rec.Body.String())
|
|
}
|