github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/operation/repository_test.go (about)

     1  package operation_test
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"regexp"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/DATA-DOG/go-sqlmock"
    10  	"github.com/kyma-incubator/compass/components/director/internal/domain/operation"
    11  	"github.com/kyma-incubator/compass/components/director/internal/domain/operation/automock"
    12  	"github.com/kyma-incubator/compass/components/director/internal/model"
    13  	"github.com/kyma-incubator/compass/components/director/internal/repo/testdb"
    14  )
    15  
    16  func TestPgRepository_Create(t *testing.T) {
    17  	// GIVEN
    18  	var nilOperationModel *model.Operation
    19  	operationModel := fixOperationModel(ordOpType, model.OperationStatusScheduled)
    20  	operationEntity := fixEntityOperation(operationID, ordOpType, model.OperationStatusScheduled)
    21  
    22  	suite := testdb.RepoCreateTestSuite{
    23  		Name: "Create Operation",
    24  		SQLQueryDetails: []testdb.SQLQueryDetails{
    25  			{
    26  				Query:       `^INSERT INTO public.operation \(.+\) VALUES \(.+\)$`,
    27  				Args:        fixOperationCreateArgs(operationModel),
    28  				ValidResult: sqlmock.NewResult(-1, 1),
    29  			},
    30  		},
    31  		ConverterMockProvider: func() testdb.Mock {
    32  			return &automock.EntityConverter{}
    33  		},
    34  		RepoConstructorFunc:       operation.NewRepository,
    35  		ModelEntity:               operationModel,
    36  		DBEntity:                  operationEntity,
    37  		NilModelEntity:            nilOperationModel,
    38  		DisableConverterErrorTest: true,
    39  		IsGlobal:                  true,
    40  	}
    41  
    42  	suite.Run(t)
    43  }
    44  
    45  func TestPgRepository_DeleteOlderThan(t *testing.T) {
    46  	suite := testdb.RepoDeleteTestSuite{
    47  		Name: "DeleteOlderThan Operation",
    48  		SQLQueryDetails: []testdb.SQLQueryDetails{
    49  			{
    50  				Query:         regexp.QuoteMeta(`DELETE FROM public.operation WHERE finished_at IS NOT NULL AND op_type = $1 AND status = $2 AND finished_at < $3`),
    51  				Args:          []driver.Value{ordOpType, model.OperationStatusScheduled, time.Time{}},
    52  				ValidResult:   sqlmock.NewResult(-1, 1),
    53  				InvalidResult: sqlmock.NewResult(-1, 2),
    54  			},
    55  		},
    56  		ConverterMockProvider: func() testdb.Mock {
    57  			return &automock.EntityConverter{}
    58  		},
    59  		RepoConstructorFunc: operation.NewRepository,
    60  		MethodName:          "DeleteOlderThan",
    61  		MethodArgs:          []interface{}{ordOpType, model.OperationStatusScheduled, time.Time{}},
    62  		IsDeleteMany:        true,
    63  		IsGlobal:            true,
    64  	}
    65  
    66  	suite.Run(t)
    67  }