@@ -1,154 +0,0 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/validation"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type CreateParams struct {
|
||||
ClientID uuid.UUID
|
||||
MinCleanVersion *int32
|
||||
MinTextVersion *int32
|
||||
Fields *map[string]uuid.UUID
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, params *CreateParams) error {
|
||||
dbparams, err := s.getCreateParams(ctx, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.submitCreate(ctx, dbparams)
|
||||
}
|
||||
|
||||
type dbCreateParams struct {
|
||||
ClientID pgtype.UUID
|
||||
MinCleanVersion *int32
|
||||
MinTextVersion *int32
|
||||
Fields *map[string]pgtype.UUID
|
||||
}
|
||||
|
||||
func (s *Service) getCreateParams(ctx context.Context, params *CreateParams) (*dbCreateParams, error) {
|
||||
minClean := params.MinCleanVersion
|
||||
if minClean != nil {
|
||||
err := s.svc.CleanVersion.IsValidVersion(*minClean)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
minText := params.MinTextVersion
|
||||
if minText != nil {
|
||||
err := s.svc.TextVersion.IsValidVersion(*minText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
fields, err := s.NormalizeFieldsToDB(ctx, params.Fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dbCreateParams{
|
||||
ClientID: database.MustToDBUUID(params.ClientID),
|
||||
MinCleanVersion: minClean,
|
||||
MinTextVersion: minText,
|
||||
Fields: fields,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) NormalizeFieldsToDB(ctx context.Context, ofields *map[string]uuid.UUID) (*map[string]pgtype.UUID, error) {
|
||||
if ofields == nil || *ofields == nil {
|
||||
return nil, nil
|
||||
} else if len(*ofields) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
dbm := map[string]pgtype.UUID{}
|
||||
for name, id := range *ofields {
|
||||
dbm[name] = database.MustToDBUUID(id)
|
||||
}
|
||||
|
||||
dbids := []pgtype.UUID{}
|
||||
for _, id := range dbm {
|
||||
dbids = append(dbids, id)
|
||||
}
|
||||
|
||||
dedup := validation.DeduplicateArray(dbids)
|
||||
|
||||
if len(dedup) != len(dbids) {
|
||||
return nil, errors.New("duplicate output fields")
|
||||
}
|
||||
|
||||
exist, err := s.cfg.GetDBQueries().AllQueriesExist(ctx, dbids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !exist {
|
||||
return nil, errors.New("not all required ids are present")
|
||||
}
|
||||
|
||||
return &dbm, nil
|
||||
}
|
||||
|
||||
func (s *Service) submitCreate(ctx context.Context, params *dbCreateParams) error {
|
||||
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, qtx *repository.Queries) error {
|
||||
version, err := qtx.AddLatestCollectorVersion(ctx, params.ClientID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = qtx.SetActiveCollectorVersion(ctx, &repository.SetActiveCollectorVersionParams{
|
||||
Clientid: params.ClientID,
|
||||
Versionid: version,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if params.MinCleanVersion != nil {
|
||||
err = qtx.SetCollectorCleanVersion(ctx, &repository.SetCollectorCleanVersionParams{
|
||||
Clientid: params.ClientID,
|
||||
Versionid: *params.MinCleanVersion,
|
||||
Addedversion: version,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if params.MinTextVersion != nil {
|
||||
err = qtx.SetCollectorTextVersion(ctx, &repository.SetCollectorTextVersionParams{
|
||||
Clientid: params.ClientID,
|
||||
Versionid: *params.MinTextVersion,
|
||||
Addedversion: version,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if params.Fields != nil {
|
||||
for key, field := range *params.Fields {
|
||||
err = qtx.AddCollectorQuery(ctx, &repository.AddCollectorQueryParams{
|
||||
Clientid: params.ClientID,
|
||||
Name: key,
|
||||
Queryid: field,
|
||||
Addedversion: version,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package collector_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"queryorchestration/internal/collector"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"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()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := collector.New(cfg, &collector.Services{})
|
||||
|
||||
minCleanV := int32(1)
|
||||
minTextV := int32(1)
|
||||
create := collector.CreateParams{
|
||||
ClientID: 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: AddLatestCollectorVersion :one").WithArgs(database.MustToDBUUID(create.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(int32(1)),
|
||||
)
|
||||
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(database.MustToDBUUID(create.ClientID), int32(1)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(database.MustToDBUUID(create.ClientID), int32(1), *create.MinCleanVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorTextVersion :exec").WithArgs(database.MustToDBUUID(create.ClientID), int32(1), *create.MinTextVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(create.ClientID), "example_key", database.MustToDBUUID((*create.Fields)["example_key"]), int32(1)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = svc.Create(ctx, &create)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"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"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetCreateParams(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{},
|
||||
}
|
||||
|
||||
minCleanV := int32(1)
|
||||
minTextV := int32(1)
|
||||
params := CreateParams{
|
||||
ClientID: 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.NoError(t, err)
|
||||
assert.EqualExportedValues(t, &dbCreateParams{
|
||||
ClientID: database.MustToDBUUID(params.ClientID),
|
||||
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()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{},
|
||||
}
|
||||
|
||||
minCleanV := int32(2)
|
||||
minTextV := int32(4)
|
||||
params := dbCreateParams{
|
||||
ClientID: 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: AddLatestCollectorVersion :one").WithArgs(params.ClientID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(int32(1)),
|
||||
)
|
||||
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(params.ClientID, int32(1)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(params.ClientID, int32(1), *params.MinCleanVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorTextVersion :exec").WithArgs(params.ClientID, int32(1), *params.MinTextVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(params.ClientID, "example_key", (*params.Fields)["example_key"], int32(1)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = svc.submitCreate(ctx, ¶ms)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestNormalizeFieldsToDB(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{},
|
||||
}
|
||||
|
||||
fields := map[string]uuid.UUID{
|
||||
"example_key": uuid.New(),
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{fields["example_key"]})).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"all_exist"}).
|
||||
AddRow(true),
|
||||
)
|
||||
|
||||
dbparams, err := svc.NormalizeFieldsToDB(ctx, &fields)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualExportedValues(t, &map[string]pgtype.UUID{
|
||||
"example_key": database.MustToDBUUID(fields["example_key"]),
|
||||
}, dbparams)
|
||||
|
||||
fields["second_key"] = fields["example_key"]
|
||||
|
||||
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{fields["example_key"]})).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"all_exist"}).
|
||||
AddRow(true),
|
||||
)
|
||||
|
||||
_, err = svc.NormalizeFieldsToDB(ctx, &fields)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
@@ -26,7 +26,7 @@ func TestGetByClientID(t *testing.T) {
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := collector.New(cfg, &collector.Services{})
|
||||
svc := collector.New(cfg)
|
||||
|
||||
minCleanV := int32(2)
|
||||
minTextV := int32(4)
|
||||
@@ -59,7 +59,7 @@ func TestListQueries(t *testing.T) {
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := collector.New(cfg, &collector.Services{})
|
||||
svc := collector.New(cfg)
|
||||
|
||||
clientId := uuid.New()
|
||||
ogc := []*resultprocessor.Query{
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
cleanversion "queryorchestration/internal/document/clean/version"
|
||||
textversion "queryorchestration/internal/document/text/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -17,19 +15,12 @@ type Collector struct {
|
||||
Fields map[string]uuid.UUID
|
||||
}
|
||||
|
||||
type Services struct {
|
||||
CleanVersion *cleanversion.Service
|
||||
TextVersion *textversion.Service
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
cfg serviceconfig.ConfigProvider
|
||||
svc *Services
|
||||
}
|
||||
|
||||
func New(cfg serviceconfig.ConfigProvider, svc *Services) *Service {
|
||||
func New(cfg serviceconfig.ConfigProvider) *Service {
|
||||
return &Service{
|
||||
cfg,
|
||||
svc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@ func TestService(t *testing.T) {
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := collector.New(cfg, &collector.Services{})
|
||||
svc := collector.New(cfg)
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package collectorupdate
|
||||
package collectorset
|
||||
|
||||
import (
|
||||
collector "queryorchestration/internal/collector"
|
||||
@@ -1,11 +1,10 @@
|
||||
package collectorupdate_test
|
||||
package collectorset_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"queryorchestration/internal/collector"
|
||||
collectorset "queryorchestration/internal/collector/set"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -15,10 +14,10 @@ import (
|
||||
func TestService(t *testing.T) {
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := collector.New(cfg, &collector.Services{})
|
||||
svc := collectorset.New(cfg, &collectorset.Services{})
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package collectorupdate
|
||||
package collectorset
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type UpdateParams struct {
|
||||
type SetParams struct {
|
||||
ClientID uuid.UUID
|
||||
ActiveVersion *int32
|
||||
MinCleanVersion *int32
|
||||
@@ -24,23 +24,23 @@ type UpdateParams struct {
|
||||
Fields *map[string]uuid.UUID
|
||||
}
|
||||
|
||||
func (s *Service) UpdateByClientId(ctx context.Context, params *UpdateParams) error {
|
||||
func (s *Service) SetByClientId(ctx context.Context, params *SetParams) error {
|
||||
current, err := s.svc.Collector.GetByClientID(ctx, params.ClientID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dbparams, err := s.getUpdateParams(ctx, current, params)
|
||||
dbparams, err := s.getSetParams(ctx, current, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.submitUpdate(ctx, current, dbparams)
|
||||
err = s.submitSet(ctx, current, dbparams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.informUpdate(ctx, dbparams)
|
||||
err = s.informSet(ctx, dbparams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -48,7 +48,7 @@ func (s *Service) UpdateByClientId(ctx context.Context, params *UpdateParams) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) informUpdate(ctx context.Context, update *dbUpdateParams) error {
|
||||
func (s *Service) informSet(ctx context.Context, update *dbSetParams) error {
|
||||
if update.ActiveVersion == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -61,7 +61,7 @@ func (s *Service) informUpdate(ctx context.Context, update *dbUpdateParams) erro
|
||||
})
|
||||
}
|
||||
|
||||
type dbUpdateParams struct {
|
||||
type dbSetParams struct {
|
||||
ClientID pgtype.UUID
|
||||
ActiveVersion *int32
|
||||
MinCleanVersion *int32
|
||||
@@ -69,13 +69,13 @@ type dbUpdateParams struct {
|
||||
Fields *map[string]pgtype.UUID
|
||||
}
|
||||
|
||||
func (s *Service) getUpdateParams(ctx context.Context, current *collector.Collector, params *UpdateParams) (*dbUpdateParams, error) {
|
||||
func (s *Service) getSetParams(ctx context.Context, current *collector.Collector, params *SetParams) (*dbSetParams, error) {
|
||||
err := s.normalizeCodeVersions(current, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fields, err := s.normalizeUpdateFieldsToDB(ctx, current.Fields, params.Fields)
|
||||
fields, err := s.normalizeSetFieldsToDB(ctx, current.Fields, params.Fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func (s *Service) getUpdateParams(ctx context.Context, current *collector.Collec
|
||||
return nil, errors.New("no changes")
|
||||
}
|
||||
|
||||
return &dbUpdateParams{
|
||||
return &dbSetParams{
|
||||
ClientID: database.MustToDBUUID(params.ClientID),
|
||||
ActiveVersion: params.ActiveVersion,
|
||||
MinCleanVersion: params.MinCleanVersion,
|
||||
@@ -109,7 +109,7 @@ func (s *Service) getUpdateParams(ctx context.Context, current *collector.Collec
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) normalizeCodeVersions(current *collector.Collector, params *UpdateParams) error {
|
||||
func (s *Service) normalizeCodeVersions(current *collector.Collector, params *SetParams) error {
|
||||
if current == nil {
|
||||
return errors.New("current collector required")
|
||||
}
|
||||
@@ -143,7 +143,7 @@ func (s *Service) normalizeCodeVersions(current *collector.Collector, params *Up
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) normalizeActiveVersion(current *collector.Collector, params *UpdateParams) error {
|
||||
func (s *Service) normalizeActiveVersion(current *collector.Collector, params *SetParams) error {
|
||||
if current == nil {
|
||||
return errors.New("current collector required")
|
||||
}
|
||||
@@ -160,7 +160,7 @@ func (s *Service) normalizeActiveVersion(current *collector.Collector, params *U
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) normalizeUpdateFieldsToDB(ctx context.Context, current map[string]uuid.UUID, ofields *map[string]uuid.UUID) (*map[string]pgtype.UUID, error) {
|
||||
func (s *Service) normalizeSetFieldsToDB(ctx context.Context, current map[string]uuid.UUID, ofields *map[string]uuid.UUID) (*map[string]pgtype.UUID, error) {
|
||||
if ofields == nil || *ofields == nil {
|
||||
return nil, nil
|
||||
} else if len(*ofields) == 0 {
|
||||
@@ -179,10 +179,10 @@ func (s *Service) normalizeUpdateFieldsToDB(ctx context.Context, current map[str
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return s.svc.Collector.NormalizeFieldsToDB(ctx, ofields)
|
||||
return s.normalizeFieldsToDB(ctx, ofields)
|
||||
}
|
||||
|
||||
func (s *Service) submitUpdate(ctx context.Context, current *collector.Collector, params *dbUpdateParams) error {
|
||||
func (s *Service) submitSet(ctx context.Context, current *collector.Collector, params *dbSetParams) error {
|
||||
err := s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, qtx *repository.Queries) error {
|
||||
activeName, err := validation.GetFieldName(params, params.ActiveVersion)
|
||||
if err != nil {
|
||||
@@ -304,3 +304,36 @@ func getAddFields(current map[string]uuid.UUID, update *map[string]pgtype.UUID)
|
||||
|
||||
return diff
|
||||
}
|
||||
|
||||
func (s *Service) normalizeFieldsToDB(ctx context.Context, ofields *map[string]uuid.UUID) (*map[string]pgtype.UUID, error) {
|
||||
if ofields == nil || *ofields == nil {
|
||||
return nil, nil
|
||||
} else if len(*ofields) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
dbm := map[string]pgtype.UUID{}
|
||||
for name, id := range *ofields {
|
||||
dbm[name] = database.MustToDBUUID(id)
|
||||
}
|
||||
|
||||
dbids := []pgtype.UUID{}
|
||||
for _, id := range dbm {
|
||||
dbids = append(dbids, id)
|
||||
}
|
||||
|
||||
dedup := validation.DeduplicateArray(dbids)
|
||||
|
||||
if len(dedup) != len(dbids) {
|
||||
return nil, errors.New("duplicate output fields")
|
||||
}
|
||||
|
||||
exist, err := s.cfg.GetDBQueries().AllQueriesExist(ctx, dbids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !exist {
|
||||
return nil, errors.New("not all required ids are present")
|
||||
}
|
||||
|
||||
return &dbm, nil
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package collectorset_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"queryorchestration/internal/collector"
|
||||
collectorset "queryorchestration/internal/collector/set"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/clientsync"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type CollectorSetConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ClientSyncConfig
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.ClientSyncURL = "here"
|
||||
|
||||
svc := collectorset.New(cfg, &collectorset.Services{
|
||||
Collector: collector.New(cfg),
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
current := collector.Collector{
|
||||
ClientID: uuid.New(),
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 4,
|
||||
}
|
||||
av := int32(2)
|
||||
mv := int32(1)
|
||||
update := collectorset.SetParams{
|
||||
ClientID: current.ClientID,
|
||||
ActiveVersion: &av,
|
||||
MinCleanVersion: &mv,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(database.MustToDBUUID(update.ClientID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(database.MustToDBUUID(current.ClientID), current.MinCleanVersion, current.MinTextVersion, current.ActiveVersion, current.LatestVersion, []byte("")),
|
||||
)
|
||||
pool.ExpectBeginTx(pgx.TxOptions{})
|
||||
rv := current.LatestVersion + 1
|
||||
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(database.MustToDBUUID(current.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(rv),
|
||||
)
|
||||
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), int32(2)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), rv, *update.MinCleanVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", update.ClientID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
err = svc.SetByClientId(ctx, &update)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
t.Run("no collector", func(t *testing.T) {
|
||||
current := collector.Collector{
|
||||
ClientID: uuid.New(),
|
||||
}
|
||||
av := int32(1)
|
||||
mv := int32(1)
|
||||
update := collectorset.SetParams{
|
||||
ClientID: current.ClientID,
|
||||
ActiveVersion: &av,
|
||||
MinCleanVersion: &mv,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(database.MustToDBUUID(update.ClientID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(database.MustToDBUUID(current.ClientID), current.MinCleanVersion, current.MinTextVersion, current.ActiveVersion, current.LatestVersion, []byte("")),
|
||||
)
|
||||
pool.ExpectBeginTx(pgx.TxOptions{})
|
||||
rv := current.LatestVersion + 1
|
||||
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(database.MustToDBUUID(current.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(rv),
|
||||
)
|
||||
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), int32(1)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), rv, *update.MinCleanVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", update.ClientID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
err = svc.SetByClientId(ctx, &update)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
+93
-53
@@ -1,4 +1,4 @@
|
||||
package collectorupdate
|
||||
package collectorset
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -24,25 +24,25 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type CollectorUpdateConfig struct {
|
||||
type CollectorSetConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ClientSyncConfig
|
||||
}
|
||||
|
||||
func TestGetUpdateParams(t *testing.T) {
|
||||
func TestGetSetParams(t *testing.T) {
|
||||
t.Run("all params", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Collector: collector.New(cfg, &collector.Services{}),
|
||||
Collector: collector.New(cfg),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 10,
|
||||
}
|
||||
params := UpdateParams{
|
||||
params := SetParams{
|
||||
ClientID: uuid.New(),
|
||||
ActiveVersion: &aV,
|
||||
MinCleanVersion: &minCleanV,
|
||||
@@ -68,9 +68,9 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
AddRow(true),
|
||||
)
|
||||
|
||||
dbparams, err := svc.getUpdateParams(ctx, ¤t, ¶ms)
|
||||
dbparams, err := svc.getSetParams(ctx, ¤t, ¶ms)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualExportedValues(t, &dbUpdateParams{
|
||||
assert.EqualExportedValues(t, &dbSetParams{
|
||||
ClientID: database.MustToDBUUID(params.ClientID),
|
||||
ActiveVersion: &aV,
|
||||
MinCleanVersion: &minCleanV,
|
||||
@@ -82,7 +82,7 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
|
||||
(*params.Fields)["second_key"] = (*params.Fields)["example_key"]
|
||||
assert.Len(t, *params.Fields, 2)
|
||||
_, err = svc.getUpdateParams(ctx, ¤t, ¶ms)
|
||||
_, err = svc.getSetParams(ctx, ¤t, ¶ms)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
@@ -91,7 +91,7 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
@@ -101,10 +101,10 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
}
|
||||
|
||||
current := collector.Collector{}
|
||||
params := UpdateParams{
|
||||
params := SetParams{
|
||||
ClientID: current.ClientID,
|
||||
}
|
||||
_, err = svc.getUpdateParams(ctx, ¤t, ¶ms)
|
||||
_, err = svc.getSetParams(ctx, ¤t, ¶ms)
|
||||
assert.EqualError(t, err, "no changes")
|
||||
})
|
||||
|
||||
@@ -113,7 +113,7 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
@@ -127,11 +127,11 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
LatestVersion: 4,
|
||||
}
|
||||
version := int32(5)
|
||||
params := UpdateParams{
|
||||
params := SetParams{
|
||||
ClientID: current.ClientID,
|
||||
ActiveVersion: &version,
|
||||
}
|
||||
_, err = svc.getUpdateParams(ctx, ¤t, ¶ms)
|
||||
_, err = svc.getSetParams(ctx, ¤t, ¶ms)
|
||||
assert.EqualError(t, err, "no changes")
|
||||
})
|
||||
|
||||
@@ -140,7 +140,7 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
@@ -154,25 +154,25 @@ func TestGetUpdateParams(t *testing.T) {
|
||||
"example": uuid.New(),
|
||||
},
|
||||
}
|
||||
params := UpdateParams{
|
||||
params := SetParams{
|
||||
ClientID: current.ClientID,
|
||||
Fields: &map[string]uuid.UUID{
|
||||
"example": current.Fields["example"],
|
||||
},
|
||||
}
|
||||
_, err = svc.getUpdateParams(ctx, ¤t, ¶ms)
|
||||
_, err = svc.getSetParams(ctx, ¤t, ¶ms)
|
||||
assert.EqualError(t, err, "no changes")
|
||||
})
|
||||
}
|
||||
|
||||
func TestSubmitUpdate(t *testing.T) {
|
||||
func TestSubmitSet(t *testing.T) {
|
||||
t.Run("all fields", func(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
@@ -195,7 +195,7 @@ func TestSubmitUpdate(t *testing.T) {
|
||||
},
|
||||
}
|
||||
aV := int32(2)
|
||||
params := dbUpdateParams{
|
||||
params := dbSetParams{
|
||||
ClientID: database.MustToDBUUID(current.ClientID),
|
||||
ActiveVersion: &aV,
|
||||
MinCleanVersion: &minCleanV,
|
||||
@@ -231,7 +231,7 @@ func TestSubmitUpdate(t *testing.T) {
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = svc.submitUpdate(ctx, ¤t, ¶ms)
|
||||
err = svc.submitSet(ctx, ¤t, ¶ms)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
t.Run("only active version", func(t *testing.T) {
|
||||
@@ -240,7 +240,7 @@ func TestSubmitUpdate(t *testing.T) {
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
@@ -258,7 +258,7 @@ func TestSubmitUpdate(t *testing.T) {
|
||||
Fields: map[string]uuid.UUID{},
|
||||
}
|
||||
av := int32(2)
|
||||
params := dbUpdateParams{
|
||||
params := dbSetParams{
|
||||
ClientID: database.MustToDBUUID(current.ClientID),
|
||||
ActiveVersion: &av,
|
||||
}
|
||||
@@ -268,7 +268,7 @@ func TestSubmitUpdate(t *testing.T) {
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
err = svc.submitUpdate(ctx, ¤t, ¶ms)
|
||||
err = svc.submitSet(ctx, ¤t, ¶ms)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
@@ -296,7 +296,7 @@ func TestNormalizeActiveVersion(t *testing.T) {
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 4,
|
||||
}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
err := svc.normalizeActiveVersion(¤t, &update)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, update.ActiveVersion)
|
||||
@@ -308,7 +308,7 @@ func TestNormalizeActiveVersion(t *testing.T) {
|
||||
ActiveVersion: version,
|
||||
LatestVersion: 4,
|
||||
}
|
||||
update := UpdateParams{
|
||||
update := SetParams{
|
||||
ActiveVersion: &version,
|
||||
}
|
||||
err := svc.normalizeActiveVersion(¤t, &update)
|
||||
@@ -322,7 +322,7 @@ func TestNormalizeActiveVersion(t *testing.T) {
|
||||
LatestVersion: 4,
|
||||
}
|
||||
version := int32(5)
|
||||
update := UpdateParams{
|
||||
update := SetParams{
|
||||
ActiveVersion: &version,
|
||||
}
|
||||
err := svc.normalizeActiveVersion(¤t, &update)
|
||||
@@ -336,7 +336,7 @@ func TestNormalizeActiveVersion(t *testing.T) {
|
||||
LatestVersion: 4,
|
||||
}
|
||||
version := int32(6)
|
||||
update := UpdateParams{
|
||||
update := SetParams{
|
||||
ActiveVersion: &version,
|
||||
}
|
||||
err := svc.normalizeActiveVersion(¤t, &update)
|
||||
@@ -345,7 +345,7 @@ func TestNormalizeActiveVersion(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNormalizeCodeVersions(t *testing.T) {
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
svc := Service{
|
||||
svc: &Services{
|
||||
CleanVersion: cleanversion.New(cfg),
|
||||
@@ -366,7 +366,7 @@ func TestNormalizeCodeVersions(t *testing.T) {
|
||||
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
current := collector.Collector{}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
|
||||
err := svc.normalizeCodeVersions(¤t, &update)
|
||||
assert.NoError(t, err)
|
||||
@@ -376,7 +376,7 @@ func TestNormalizeCodeVersions(t *testing.T) {
|
||||
|
||||
t.Run("valid clean", func(t *testing.T) {
|
||||
current := collector.Collector{}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
|
||||
cv := int32(1)
|
||||
update.MinCleanVersion = &cv
|
||||
@@ -388,7 +388,7 @@ func TestNormalizeCodeVersions(t *testing.T) {
|
||||
|
||||
t.Run("valid text", func(t *testing.T) {
|
||||
current := collector.Collector{}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
|
||||
tv := int32(1)
|
||||
update.MinTextVersion = &tv
|
||||
@@ -400,7 +400,7 @@ func TestNormalizeCodeVersions(t *testing.T) {
|
||||
|
||||
t.Run("valid clean and text", func(t *testing.T) {
|
||||
current := collector.Collector{}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
|
||||
current.MinCleanVersion = 1
|
||||
current.MinTextVersion = 1
|
||||
@@ -414,7 +414,7 @@ func TestNormalizeCodeVersions(t *testing.T) {
|
||||
current := collector.Collector{
|
||||
MinCleanVersion: 1,
|
||||
}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
|
||||
update.MinCleanVersion = ¤t.MinCleanVersion
|
||||
err := svc.normalizeCodeVersions(¤t, &update)
|
||||
@@ -427,7 +427,7 @@ func TestNormalizeCodeVersions(t *testing.T) {
|
||||
current := collector.Collector{
|
||||
MinTextVersion: 1,
|
||||
}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
|
||||
update.MinTextVersion = ¤t.MinTextVersion
|
||||
err := svc.normalizeCodeVersions(¤t, &update)
|
||||
@@ -441,7 +441,7 @@ func TestNormalizeCodeVersions(t *testing.T) {
|
||||
MinCleanVersion: 1,
|
||||
MinTextVersion: 1,
|
||||
}
|
||||
update := UpdateParams{}
|
||||
update := SetParams{}
|
||||
|
||||
update.MinCleanVersion = ¤t.MinCleanVersion
|
||||
update.MinTextVersion = ¤t.MinTextVersion
|
||||
@@ -527,21 +527,21 @@ func TestGetAddFields(t *testing.T) {
|
||||
}, add)
|
||||
}
|
||||
|
||||
func TestNormalizeUpdateFieldsToDB(t *testing.T) {
|
||||
func TestNormalizeSetFieldsToDB(t *testing.T) {
|
||||
t.Run("valid key", func(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Collector: collector.New(cfg, &collector.Services{}),
|
||||
Collector: collector.New(cfg),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -555,7 +555,7 @@ func TestNormalizeUpdateFieldsToDB(t *testing.T) {
|
||||
AddRow(true),
|
||||
)
|
||||
|
||||
dbparams, err := svc.normalizeUpdateFieldsToDB(ctx, current, &fields)
|
||||
dbparams, err := svc.normalizeSetFieldsToDB(ctx, current, &fields)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualExportedValues(t, &map[string]pgtype.UUID{
|
||||
"example_key": database.MustToDBUUID(fields["example_key"]),
|
||||
@@ -567,7 +567,7 @@ func TestNormalizeUpdateFieldsToDB(t *testing.T) {
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
@@ -587,17 +587,17 @@ func TestNormalizeUpdateFieldsToDB(t *testing.T) {
|
||||
AddRow(true),
|
||||
)
|
||||
|
||||
_, err = svc.normalizeUpdateFieldsToDB(ctx, current, &fields)
|
||||
_, err = svc.normalizeSetFieldsToDB(ctx, current, &fields)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
t.Run("no changes", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Collector: collector.New(cfg, &collector.Services{}),
|
||||
Collector: collector.New(cfg),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -608,19 +608,19 @@ func TestNormalizeUpdateFieldsToDB(t *testing.T) {
|
||||
"example_key": current["example_key"],
|
||||
}
|
||||
|
||||
val, err := svc.normalizeUpdateFieldsToDB(ctx, current, &fields)
|
||||
val, err := svc.normalizeSetFieldsToDB(ctx, current, &fields)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, val)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInformUpdate(t *testing.T) {
|
||||
func TestInformSet(t *testing.T) {
|
||||
t.Run("with version update", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
@@ -630,7 +630,7 @@ func TestInformUpdate(t *testing.T) {
|
||||
svc := New(cfg, &Services{})
|
||||
|
||||
av := int32(2)
|
||||
update := dbUpdateParams{
|
||||
update := dbSetParams{
|
||||
ClientID: database.MustToDBUUID(uuid.New()),
|
||||
ActiveVersion: &av,
|
||||
}
|
||||
@@ -645,7 +645,7 @@ func TestInformUpdate(t *testing.T) {
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
err = svc.informUpdate(ctx, &update)
|
||||
err = svc.informSet(ctx, &update)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
t.Run("with no update", func(t *testing.T) {
|
||||
@@ -653,7 +653,7 @@ func TestInformUpdate(t *testing.T) {
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
@@ -662,11 +662,51 @@ func TestInformUpdate(t *testing.T) {
|
||||
|
||||
svc := New(cfg, &Services{})
|
||||
|
||||
update := dbUpdateParams{
|
||||
update := dbSetParams{
|
||||
ClientID: database.MustToDBUUID(uuid.New()),
|
||||
}
|
||||
|
||||
err = svc.informUpdate(ctx, &update)
|
||||
err = svc.informSet(ctx, &update)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNormalizeFieldsToDB(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorSetConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{},
|
||||
}
|
||||
|
||||
fields := map[string]uuid.UUID{
|
||||
"example_key": uuid.New(),
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{fields["example_key"]})).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"all_exist"}).
|
||||
AddRow(true),
|
||||
)
|
||||
|
||||
dbparams, err := svc.normalizeFieldsToDB(ctx, &fields)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualExportedValues(t, &map[string]pgtype.UUID{
|
||||
"example_key": database.MustToDBUUID(fields["example_key"]),
|
||||
}, dbparams)
|
||||
|
||||
fields["second_key"] = fields["example_key"]
|
||||
|
||||
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{fields["example_key"]})).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"all_exist"}).
|
||||
AddRow(true),
|
||||
)
|
||||
|
||||
_, err = svc.normalizeFieldsToDB(ctx, &fields)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package collectorupdate_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"queryorchestration/internal/collector"
|
||||
collectorupdate "queryorchestration/internal/collector/update"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/clientsync"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type CollectorUpdateConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
clientsync.ClientSyncConfig
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &CollectorUpdateConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.ClientSyncURL = "here"
|
||||
|
||||
svc := collectorupdate.New(cfg, &collectorupdate.Services{
|
||||
Collector: collector.New(cfg, &collector.Services{}),
|
||||
})
|
||||
|
||||
current := collector.Collector{
|
||||
ClientID: uuid.New(),
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 4,
|
||||
}
|
||||
av := int32(2)
|
||||
mv := int32(1)
|
||||
update := collectorupdate.UpdateParams{
|
||||
ClientID: current.ClientID,
|
||||
ActiveVersion: &av,
|
||||
MinCleanVersion: &mv,
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(database.MustToDBUUID(update.ClientID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
|
||||
AddRow(database.MustToDBUUID(current.ClientID), current.MinCleanVersion, current.MinTextVersion, current.ActiveVersion, current.LatestVersion, []byte("")),
|
||||
)
|
||||
pool.ExpectBeginTx(pgx.TxOptions{})
|
||||
rv := current.LatestVersion + 1
|
||||
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(database.MustToDBUUID(current.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"version"}).
|
||||
AddRow(rv),
|
||||
)
|
||||
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), int32(2)).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(database.MustToDBUUID(current.ClientID), rv, *update.MinCleanVersion).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", update.ClientID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
err = svc.UpdateByClientId(ctx, &update)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user