2025-01-23 14:56:20 +00:00
|
|
|
package collector_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/job/collector"
|
2025-01-31 13:43:55 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-01-23 14:56:20 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
|
|
|
}
|
2025-01-31 13:43:55 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
svc := collector.New(cfg, &collector.Services{})
|
2025-01-23 14:56:20 +00:00
|
|
|
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
minCleanV := int32(2)
|
|
|
|
|
minTextV := int32(4)
|
|
|
|
|
create := collector.CreateParams{
|
|
|
|
|
JobID: uuid.New(),
|
|
|
|
|
MinCleanVersion: &minCleanV,
|
|
|
|
|
MinTextVersion: &minTextV,
|
2025-01-24 14:52:56 +00:00
|
|
|
Fields: &map[string]uuid.UUID{
|
2025-01-23 14:56:20 +00:00
|
|
|
"example_key": uuid.New(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 14:52:56 +00:00
|
|
|
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{(*create.Fields)["example_key"]})).WillReturnRows(
|
2025-01-23 14:56:20 +00:00
|
|
|
pgxmock.NewRows([]string{"all_exist"}).
|
|
|
|
|
AddRow(true),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
|
|
|
|
pool.ExpectQuery("name: CreateCollector :one").WithArgs(database.MustToDBUUID(create.JobID)).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id"}).
|
|
|
|
|
AddRow(database.MustToDBUUID(id)),
|
|
|
|
|
)
|
|
|
|
|
pool.ExpectExec("name: AddCollectorCodeVersion :exec").WithArgs(database.MustToDBUUID(id), int32(1), *create.MinCleanVersion, *create.MinTextVersion).
|
|
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
2025-01-24 14:52:56 +00:00
|
|
|
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(id), "example_key", database.MustToDBUUID((*create.Fields)["example_key"]), int32(1)).
|
2025-01-23 14:56:20 +00:00
|
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
|
pool.ExpectCommit()
|
|
|
|
|
|
|
|
|
|
aid, err := svc.Create(ctx, &create)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, id, aid)
|
|
|
|
|
}
|