github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/formationconstraint/repository_test.go (about) 1 package formationconstraint_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/formationconstraint" 10 "github.com/kyma-incubator/compass/components/director/internal/domain/formationconstraint/automock" 11 "github.com/kyma-incubator/compass/components/director/internal/repo/testdb" 12 ) 13 14 func TestRepository_Get(t *testing.T) { 15 suite := testdb.RepoGetTestSuite{ 16 Name: "Get Formation Constraint By ID", 17 MethodName: "Get", 18 SQLQueryDetails: []testdb.SQLQueryDetails{ 19 { 20 Query: regexp.QuoteMeta(`SELECT id, name, constraint_type, target_operation, operator, resource_type, resource_subtype, input_template, constraint_scope FROM public.formation_constraints WHERE id = $1`), 21 Args: []driver.Value{testID}, 22 IsSelect: true, 23 ValidRowsProvider: func() []*sqlmock.Rows { 24 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(entity.ID, entity.Name, entity.ConstraintType, entity.TargetOperation, entity.Operator, entity.ResourceType, entity.ResourceSubtype, entity.InputTemplate, entity.ConstraintScope)} 25 }, 26 InvalidRowsProvider: func() []*sqlmock.Rows { 27 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 28 }, 29 }, 30 }, 31 ConverterMockProvider: func() testdb.Mock { 32 return &automock.EntityConverter{} 33 }, 34 RepoConstructorFunc: formationconstraint.NewRepository, 35 ExpectedModelEntity: formationConstraintModel, 36 ExpectedDBEntity: &entity, 37 MethodArgs: []interface{}{testID}, 38 DisableConverterErrorTest: true, 39 } 40 41 suite.Run(t) 42 } 43 44 func TestRepository_ListAll(t *testing.T) { 45 suite := testdb.RepoListTestSuite{ 46 Name: "List Formation Constraints", 47 MethodName: "ListAll", 48 SQLQueryDetails: []testdb.SQLQueryDetails{ 49 { 50 Query: regexp.QuoteMeta(`SELECT id, name, constraint_type, target_operation, operator, resource_type, resource_subtype, input_template, constraint_scope FROM public.formation_constraints`), 51 IsSelect: true, 52 ValidRowsProvider: func() []*sqlmock.Rows { 53 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(entity.ID, entity.Name, entity.ConstraintType, entity.TargetOperation, entity.Operator, entity.ResourceType, entity.ResourceSubtype, entity.InputTemplate, entity.ConstraintScope)} 54 }, 55 InvalidRowsProvider: func() []*sqlmock.Rows { 56 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 57 }, 58 }, 59 }, 60 ConverterMockProvider: func() testdb.Mock { 61 conv := &automock.EntityConverter{} 62 return conv 63 }, 64 RepoConstructorFunc: formationconstraint.NewRepository, 65 MethodArgs: []interface{}{}, 66 ExpectedDBEntities: []interface{}{&entity}, 67 ExpectedModelEntities: []interface{}{formationConstraintModel}, 68 DisableConverterErrorTest: true, 69 } 70 71 suite.Run(t) 72 } 73 74 func TestRepository_ListByIDs(t *testing.T) { 75 suite := testdb.RepoListTestSuite{ 76 Name: "List Formation Constraints", 77 MethodName: "ListByIDs", 78 SQLQueryDetails: []testdb.SQLQueryDetails{ 79 { 80 Query: regexp.QuoteMeta(`SELECT id, name, constraint_type, target_operation, operator, resource_type, resource_subtype, input_template, constraint_scope FROM public.formation_constraints WHERE id IN ($1)`), 81 IsSelect: true, 82 Args: []driver.Value{testID}, 83 ValidRowsProvider: func() []*sqlmock.Rows { 84 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(entity.ID, entity.Name, entity.ConstraintType, entity.TargetOperation, entity.Operator, entity.ResourceType, entity.ResourceSubtype, entity.InputTemplate, entity.ConstraintScope)} 85 }, 86 InvalidRowsProvider: func() []*sqlmock.Rows { 87 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 88 }, 89 }, 90 }, 91 ConverterMockProvider: func() testdb.Mock { 92 conv := &automock.EntityConverter{} 93 return conv 94 }, 95 RepoConstructorFunc: formationconstraint.NewRepository, 96 MethodArgs: []interface{}{[]string{testID}}, 97 ExpectedDBEntities: []interface{}{&entity}, 98 ExpectedModelEntities: []interface{}{formationConstraintModel}, 99 DisableConverterErrorTest: true, 100 } 101 102 suite.Run(t) 103 } 104 105 func TestRepository_ListMatchingFormationConstraints(t *testing.T) { 106 suite := testdb.RepoListTestSuite{ 107 Name: "List Formation Constraints", 108 MethodName: "ListMatchingFormationConstraints", 109 SQLQueryDetails: []testdb.SQLQueryDetails{ 110 { 111 Query: regexp.QuoteMeta(`SELECT id, name, constraint_type, target_operation, operator, resource_type, resource_subtype, input_template, constraint_scope FROM public.formation_constraints WHERE (target_operation = $1 AND constraint_type = $2 AND resource_type = $3 AND (resource_subtype = $4 OR resource_subtype = $5) AND (constraint_scope = $6 OR id IN ($7)))`), 112 IsSelect: true, 113 Args: []driver.Value{location.OperationName, location.ConstraintType, details.ResourceType, details.ResourceSubtype, resourceSubtypeANY, "GLOBAL", testID}, 114 ValidRowsProvider: func() []*sqlmock.Rows { 115 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(entity.ID, entity.Name, entity.ConstraintType, entity.TargetOperation, entity.Operator, entity.ResourceType, entity.ResourceSubtype, entity.InputTemplate, entity.ConstraintScope)} 116 }, 117 InvalidRowsProvider: func() []*sqlmock.Rows { 118 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 119 }, 120 }, 121 }, 122 ConverterMockProvider: func() testdb.Mock { 123 conv := &automock.EntityConverter{} 124 return conv 125 }, 126 RepoConstructorFunc: formationconstraint.NewRepository, 127 MethodArgs: []interface{}{[]string{testID}, location, matchingDetails}, 128 ExpectedDBEntities: []interface{}{&entity}, 129 ExpectedModelEntities: []interface{}{formationConstraintModel}, 130 DisableConverterErrorTest: true, 131 } 132 133 suite.Run(t) 134 } 135 136 func TestRepository_ListByIDsAndGlobal(t *testing.T) { 137 suite := testdb.RepoListTestSuite{ 138 Name: "List Formation Constraints by IDs and Global ones", 139 MethodName: "ListByIDsAndGlobal", 140 SQLQueryDetails: []testdb.SQLQueryDetails{ 141 { 142 Query: regexp.QuoteMeta(`SELECT id, name, constraint_type, target_operation, operator, resource_type, resource_subtype, input_template, constraint_scope FROM public.formation_constraints WHERE (constraint_scope = $1 OR id IN ($2))`), 143 IsSelect: true, 144 Args: []driver.Value{"GLOBAL", testID}, 145 ValidRowsProvider: func() []*sqlmock.Rows { 146 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(entity.ID, entity.Name, entity.ConstraintType, entity.TargetOperation, entity.Operator, entity.ResourceType, entity.ResourceSubtype, entity.InputTemplate, entity.ConstraintScope)} 147 }, 148 InvalidRowsProvider: func() []*sqlmock.Rows { 149 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 150 }, 151 }, 152 }, 153 ConverterMockProvider: func() testdb.Mock { 154 conv := &automock.EntityConverter{} 155 return conv 156 }, 157 RepoConstructorFunc: formationconstraint.NewRepository, 158 MethodArgs: []interface{}{[]string{testID}}, 159 ExpectedDBEntities: []interface{}{&entity}, 160 ExpectedModelEntities: []interface{}{formationConstraintModel}, 161 DisableConverterErrorTest: true, 162 } 163 164 suite.Run(t) 165 } 166 167 func TestRepository_Create(t *testing.T) { 168 suite := testdb.RepoCreateTestSuite{ 169 Name: "Create Formation Constraint", 170 MethodName: "Create", 171 SQLQueryDetails: []testdb.SQLQueryDetails{ 172 { 173 Query: `^INSERT INTO public.formation_constraints \(.+\) VALUES \(.+\)$`, 174 Args: []driver.Value{entity.ID, entity.Name, entity.ConstraintType, entity.TargetOperation, entity.Operator, entity.ResourceType, entity.ResourceSubtype, entity.InputTemplate, entity.ConstraintScope}, 175 ValidResult: sqlmock.NewResult(-1, 1), 176 }, 177 }, 178 ConverterMockProvider: func() testdb.Mock { 179 return &automock.EntityConverter{} 180 }, 181 RepoConstructorFunc: formationconstraint.NewRepository, 182 ModelEntity: formationConstraintModel, 183 DBEntity: &entity, 184 NilModelEntity: nilModelEntity, 185 IsGlobal: true, 186 DisableConverterErrorTest: true, 187 } 188 189 suite.Run(t) 190 } 191 192 func TestRepository_Delete(t *testing.T) { 193 suite := testdb.RepoDeleteTestSuite{ 194 Name: "Delete Formation Constraint by ID", 195 SQLQueryDetails: []testdb.SQLQueryDetails{ 196 { 197 Query: regexp.QuoteMeta(`DELETE FROM public.formation_constraints WHERE id = $1`), 198 Args: []driver.Value{testID}, 199 ValidResult: sqlmock.NewResult(-1, 1), 200 InvalidResult: sqlmock.NewResult(-1, 2), 201 }, 202 }, 203 RepoConstructorFunc: formationconstraint.NewRepository, 204 ConverterMockProvider: func() testdb.Mock { 205 return &automock.EntityConverter{} 206 }, 207 IsGlobal: true, 208 MethodArgs: []interface{}{testID}, 209 } 210 211 suite.Run(t) 212 } 213 214 func TestRepository_Update(t *testing.T) { 215 updateStmt := regexp.QuoteMeta(`UPDATE public.formation_constraints SET input_template = ? WHERE id = ?`) 216 suite := testdb.RepoUpdateTestSuite{ 217 Name: "Update Formation Constraint By ID", 218 SQLQueryDetails: []testdb.SQLQueryDetails{ 219 { 220 Query: updateStmt, 221 Args: []driver.Value{entity.InputTemplate, entity.ID}, 222 ValidResult: sqlmock.NewResult(-1, 1), 223 InvalidResult: sqlmock.NewResult(-1, 0), 224 }, 225 }, 226 RepoConstructorFunc: formationconstraint.NewRepository, 227 ConverterMockProvider: func() testdb.Mock { 228 return &automock.EntityConverter{} 229 }, 230 ModelEntity: formationConstraintModel, 231 DBEntity: &entity, 232 NilModelEntity: nilModelEntity, 233 DisableConverterErrorTest: true, 234 IsGlobal: true, 235 } 236 237 suite.Run(t) 238 }