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

87 lines
2.5 KiB
Go
Raw Normal View History

2025-01-15 19:45:51 +00:00
package queryservice_test
import (
"encoding/json"
2025-01-15 19:45:51 +00:00
"net/http"
"net/http/httptest"
queryservice "queryorchestration/api/queryService"
"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"
"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()
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())
}
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)
svc := collector.New(db)
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
JobCollector: svc,
})
2025-01-15 19:45:51 +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
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)
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
}