2025-01-15 19:45:51 +00:00
|
|
|
package queryservice_test
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-21 12:28:46 +00:00
|
|
|
"encoding/json"
|
2025-01-15 19:45:51 +00:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
queryservice "queryorchestration/api/queryService"
|
2025-01-21 12:28:46 +00:00
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/job/collector"
|
2025-01-15 19:45:51 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2025-01-21 12:28:46 +00:00
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
2025-01-15 19:45:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestUpdateJobCollector(t *testing.T) {
|
|
|
|
|
e := echo.New()
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
|
|
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{})
|
|
|
|
|
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
|
2025-01-21 12:28:46 +00:00
|
|
|
err := cons.UpdateJobCollectorByJobId(ctx, id.String())
|
2025-01-15 19:45:51 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
|
assert.NotEmpty(t, rec.Body.String())
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-21 12:28:46 +00:00
|
|
|
func TestGetJobCollectorByJobId(t *testing.T) {
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
|
|
|
}
|
|
|
|
|
queries := repository.New(pool)
|
|
|
|
|
db := &database.Connection{
|
|
|
|
|
Queries: queries,
|
|
|
|
|
Pool: pool,
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 19:45:51 +00:00
|
|
|
e := echo.New()
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
ctx := e.NewContext(req, rec)
|
|
|
|
|
|
2025-01-21 12:28:46 +00:00
|
|
|
svc := collector.New(db)
|
|
|
|
|
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
|
|
|
|
|
JobCollector: svc,
|
|
|
|
|
})
|
2025-01-15 19:45:51 +00:00
|
|
|
|
2025-01-21 12:28:46 +00:00
|
|
|
coll := collector.Collector{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
JobID: uuid.New(),
|
|
|
|
|
MinCleanVersion: int32(1),
|
|
|
|
|
MinTextVersion: int32(2),
|
|
|
|
|
}
|
2025-01-15 19:45:51 +00:00
|
|
|
|
2025-01-21 12:28:46 +00:00
|
|
|
pool.ExpectQuery("name: GetCollectorByJobID :one").WithArgs(database.MustToDBUUID(coll.JobID)).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(coll.ID), database.MustToDBUUID(coll.JobID), coll.MinCleanVersion, coll.MinTextVersion, int32(1), int32(2), []byte("")),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
err = cons.GetJobCollectorByJobId(ctx, coll.JobID.String())
|
2025-01-15 19:45:51 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2025-01-21 12:28:46 +00:00
|
|
|
|
|
|
|
|
var res queryservice.JobCollector
|
|
|
|
|
err = json.Unmarshal(rec.Body.Bytes(), &res)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.EqualExportedValues(t, queryservice.JobCollector{
|
|
|
|
|
JobId: coll.JobID.String(),
|
|
|
|
|
MinimumCleanerVersion: coll.MinCleanVersion,
|
|
|
|
|
MinimumTextVersion: coll.MinTextVersion,
|
|
|
|
|
ActiveVersion: int32(1),
|
|
|
|
|
LatestVersion: int32(2),
|
|
|
|
|
Fields: []queryservice.JobCollectorField{},
|
|
|
|
|
}, res)
|
2025-01-15 19:45:51 +00:00
|
|
|
}
|