127 lines
3.2 KiB
Go
127 lines
3.2 KiB
Go
|
|
package collector
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"queryorchestration/internal/database"
|
||
|
|
"queryorchestration/internal/database/repository"
|
||
|
|
"queryorchestration/internal/query"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/jackc/pgx/v5"
|
||
|
|
"github.com/jackc/pgx/v5/pgtype"
|
||
|
|
"github.com/pashagolub/pgxmock/v3"
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestGetCreateParams(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 := Service{
|
||
|
|
db: db,
|
||
|
|
svc: &Services{
|
||
|
|
Query: query.New(db),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
minCleanV := int32(2)
|
||
|
|
minTextV := int32(4)
|
||
|
|
params := 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{params.Fields["example_key"]})).WillReturnRows(
|
||
|
|
pgxmock.NewRows([]string{"all_exist"}).
|
||
|
|
AddRow(true),
|
||
|
|
)
|
||
|
|
|
||
|
|
dbparams, err := svc.getCreateParams(ctx, ¶ms)
|
||
|
|
assert.Nil(t, err)
|
||
|
|
assert.EqualExportedValues(t, &dbCreateParams{
|
||
|
|
JobID: database.MustToDBUUID(params.JobID),
|
||
|
|
MinCleanVersion: &minCleanV,
|
||
|
|
MinTextVersion: &minTextV,
|
||
|
|
Fields: map[string]pgtype.UUID{
|
||
|
|
"example_key": database.MustToDBUUID(params.Fields["example_key"]),
|
||
|
|
},
|
||
|
|
}, dbparams)
|
||
|
|
|
||
|
|
params.Fields["second_key"] = params.Fields["example_key"]
|
||
|
|
assert.Len(t, params.Fields, 2)
|
||
|
|
_, err = svc.getCreateParams(ctx, ¶ms)
|
||
|
|
assert.Error(t, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSubmitCreate(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 := Service{
|
||
|
|
db: db,
|
||
|
|
svc: &Services{
|
||
|
|
Query: query.New(db),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
minCleanV := int32(2)
|
||
|
|
minTextV := int32(4)
|
||
|
|
id := uuid.New()
|
||
|
|
params := dbCreateParams{
|
||
|
|
JobID: database.MustToDBUUID(uuid.New()),
|
||
|
|
MinCleanVersion: &minCleanV,
|
||
|
|
MinTextVersion: &minTextV,
|
||
|
|
Fields: map[string]pgtype.UUID{
|
||
|
|
"example_key": database.MustToDBUUID(uuid.New()),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
pool.ExpectBeginTx(pgx.TxOptions{})
|
||
|
|
pool.ExpectQuery("name: CreateCollector :one").WithArgs(params.JobID).WillReturnRows(
|
||
|
|
pgxmock.NewRows([]string{"id"}).
|
||
|
|
AddRow(database.MustToDBUUID(id)),
|
||
|
|
)
|
||
|
|
pool.ExpectExec("name: AddCollectorCodeVersion :exec").WithArgs(database.MustToDBUUID(id), int32(1), *params.MinCleanVersion, *params.MinTextVersion).
|
||
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
||
|
|
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(id), "example_key", params.Fields["example_key"], int32(1)).
|
||
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
||
|
|
pool.ExpectCommit()
|
||
|
|
|
||
|
|
aid, err := svc.submitCreate(ctx, ¶ms)
|
||
|
|
assert.Nil(t, err)
|
||
|
|
assert.Equal(t, id, aid)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFields(t *testing.T) {
|
||
|
|
f := fields{}
|
||
|
|
assert.Nil(t, f.GetRequiredQueryIDs())
|
||
|
|
|
||
|
|
values := []uuid.UUID{uuid.New()}
|
||
|
|
f.SetRequiredQueryIDs(&values)
|
||
|
|
assert.NotNil(t, f.values)
|
||
|
|
assert.ElementsMatch(t, *f.values, *f.GetRequiredQueryIDs())
|
||
|
|
}
|