github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/formationtemplateconstraintreferences/repository_test.go (about) 1 package formationtemplateconstraintreferences_test 2 3 import ( 4 "database/sql/driver" 5 "regexp" 6 "testing" 7 8 "github.com/DATA-DOG/go-sqlmock" 9 "github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplateconstraintreferences" 10 "github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplateconstraintreferences/automock" 11 "github.com/kyma-incubator/compass/components/director/internal/repo/testdb" 12 ) 13 14 func TestRepository_ListMatchingFormationConstraints(t *testing.T) { 15 suite := testdb.RepoListTestSuite{ 16 Name: "List Formation Constraints", 17 MethodName: "ListByFormationTemplateID", 18 SQLQueryDetails: []testdb.SQLQueryDetails{ 19 { 20 Query: regexp.QuoteMeta(`SELECT formation_constraint_id, formation_template_id FROM public.formation_template_constraint_references WHERE formation_template_id = $1`), 21 IsSelect: true, 22 Args: []driver.Value{templateID}, 23 ValidRowsProvider: func() []*sqlmock.Rows { 24 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(entity.FormationTemplateID, entity.ConstraintID)} 25 }, 26 InvalidRowsProvider: func() []*sqlmock.Rows { 27 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 28 }, 29 }, 30 }, 31 ConverterMockProvider: func() testdb.Mock { 32 conv := &automock.ConstraintReferenceConverter{} 33 return conv 34 }, 35 RepoConstructorFunc: formationtemplateconstraintreferences.NewRepository, 36 MethodArgs: []interface{}{templateID}, 37 ExpectedDBEntities: []interface{}{entity}, 38 ExpectedModelEntities: []interface{}{constraintReference}, 39 DisableConverterErrorTest: true, 40 } 41 42 suite.Run(t) 43 } 44 45 func TestRepository_Create(t *testing.T) { 46 suite := testdb.RepoCreateTestSuite{ 47 Name: "Create Formation Constraint", 48 MethodName: "Create", 49 SQLQueryDetails: []testdb.SQLQueryDetails{ 50 { 51 Query: `^INSERT INTO public.formation_template_constraint_references \(.+\) VALUES \(.+\)$`, 52 Args: []driver.Value{entity.ConstraintID, entity.FormationTemplateID}, 53 ValidResult: sqlmock.NewResult(-1, 1), 54 }, 55 }, 56 ConverterMockProvider: func() testdb.Mock { 57 return &automock.ConstraintReferenceConverter{} 58 }, 59 RepoConstructorFunc: formationtemplateconstraintreferences.NewRepository, 60 ModelEntity: constraintReference, 61 DBEntity: entity, 62 NilModelEntity: nilModel, 63 IsGlobal: true, 64 DisableConverterErrorTest: true, 65 } 66 67 suite.Run(t) 68 } 69 70 func TestRepository_Delete(t *testing.T) { 71 suite := testdb.RepoDeleteTestSuite{ 72 Name: "Delete Formation Constraint by ID", 73 SQLQueryDetails: []testdb.SQLQueryDetails{ 74 { 75 Query: regexp.QuoteMeta(`DELETE FROM public.formation_template_constraint_references WHERE formation_template_id = $1 AND formation_constraint_id = $2`), 76 Args: []driver.Value{templateID, constraintID}, 77 ValidResult: sqlmock.NewResult(-1, 1), 78 InvalidResult: sqlmock.NewResult(-1, 2), 79 }, 80 }, 81 RepoConstructorFunc: formationtemplateconstraintreferences.NewRepository, 82 ConverterMockProvider: func() testdb.Mock { 83 return &automock.ConstraintReferenceConverter{} 84 }, 85 IsGlobal: true, 86 MethodArgs: []interface{}{templateID, constraintID}, 87 } 88 89 suite.Run(t) 90 } 91 92 func TestRepository_ListByFormationTemplateIDs(t *testing.T) { 93 suite := testdb.RepoListTestSuite{ 94 Name: "List Formation Constraints by Template IDs", 95 MethodName: "ListByFormationTemplateIDs", 96 SQLQueryDetails: []testdb.SQLQueryDetails{ 97 { 98 Query: regexp.QuoteMeta(`SELECT formation_constraint_id, formation_template_id FROM public.formation_template_constraint_references WHERE formation_template_id IN ($1, $2)`), 99 IsSelect: true, 100 Args: []driver.Value{templateID, templateIDSecond}, 101 ValidRowsProvider: func() []*sqlmock.Rows { 102 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(entity.FormationTemplateID, entity.ConstraintID)} 103 }, 104 InvalidRowsProvider: func() []*sqlmock.Rows { 105 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 106 }, 107 }, 108 }, 109 ConverterMockProvider: func() testdb.Mock { 110 conv := &automock.ConstraintReferenceConverter{} 111 return conv 112 }, 113 RepoConstructorFunc: formationtemplateconstraintreferences.NewRepository, 114 MethodArgs: []interface{}{[]string{templateID, templateIDSecond}}, 115 ExpectedDBEntities: []interface{}{entity}, 116 ExpectedModelEntities: []interface{}{constraintReference}, 117 DisableConverterErrorTest: true, 118 } 119 120 suite.Run(t) 121 }