65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
|
|
package collector_test
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"queryorchestration/internal/database"
|
||
|
|
"queryorchestration/internal/database/repository"
|
||
|
|
"queryorchestration/internal/job/collector"
|
||
|
|
"queryorchestration/internal/query"
|
||
|
|
"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)
|
||
|
|
}
|
||
|
|
queries := repository.New(pool)
|
||
|
|
db := &database.Connection{
|
||
|
|
Queries: queries,
|
||
|
|
Pool: pool,
|
||
|
|
}
|
||
|
|
|
||
|
|
svc := collector.New(db, &collector.Services{
|
||
|
|
Query: query.New(db),
|
||
|
|
})
|
||
|
|
|
||
|
|
id := uuid.New()
|
||
|
|
minCleanV := int32(2)
|
||
|
|
minTextV := int32(4)
|
||
|
|
create := collector.CreateParams{
|
||
|
|
JobID: uuid.New(),
|
||
|
|
MinCleanVersion: &minCleanV,
|
||
|
|
MinTextVersion: &minTextV,
|
||
|
|
Fields: map[string]uuid.UUID{
|
||
|
|
"example_key": uuid.New(),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{create.Fields["example_key"]})).WillReturnRows(
|
||
|
|
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))
|
||
|
|
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(id), "example_key", database.MustToDBUUID(create.Fields["example_key"]), int32(1)).
|
||
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
||
|
|
pool.ExpectCommit()
|
||
|
|
|
||
|
|
aid, err := svc.Create(ctx, &create)
|
||
|
|
assert.Nil(t, err)
|
||
|
|
assert.Equal(t, id, aid)
|
||
|
|
}
|