github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/formation/repository_test.go (about) 1 package formation_test 2 3 import ( 4 "database/sql/driver" 5 "regexp" 6 "testing" 7 8 "github.com/kyma-incubator/compass/components/director/internal/model" 9 "github.com/kyma-incubator/compass/components/director/pkg/pagination" 10 11 "github.com/DATA-DOG/go-sqlmock" 12 "github.com/kyma-incubator/compass/components/director/internal/domain/formation" 13 "github.com/kyma-incubator/compass/components/director/internal/domain/formation/automock" 14 "github.com/kyma-incubator/compass/components/director/internal/repo/testdb" 15 ) 16 17 var ( 18 formationEntity = fixFormationEntity() 19 formationModel = fixFormationModel() 20 ) 21 22 func TestRepository_Create(t *testing.T) { 23 suite := testdb.RepoCreateTestSuite{ 24 Name: "Create Formation", 25 MethodName: "Create", 26 SQLQueryDetails: []testdb.SQLQueryDetails{ 27 { 28 Query: `^INSERT INTO public.formations \(.+\) VALUES \(.+\)$`, 29 Args: []driver.Value{FormationID, TntInternalID, FormationTemplateID, testFormationName, testFormationState, testFormationEmptyError}, 30 ValidResult: sqlmock.NewResult(-1, 1), 31 }, 32 }, 33 ConverterMockProvider: func() testdb.Mock { 34 return &automock.EntityConverter{} 35 }, 36 RepoConstructorFunc: formation.NewRepository, 37 ModelEntity: formationModel, 38 DBEntity: formationEntity, 39 NilModelEntity: nilFormationModel, 40 IsGlobal: true, 41 DisableConverterErrorTest: true, 42 } 43 44 suite.Run(t) 45 } 46 47 func TestRepository_Get(t *testing.T) { 48 suite := testdb.RepoGetTestSuite{ 49 Name: "Get Formation by ID", 50 MethodName: "Get", 51 SQLQueryDetails: []testdb.SQLQueryDetails{ 52 { 53 Query: regexp.QuoteMeta(`SELECT id, tenant_id, formation_template_id, name, state, error FROM public.formations WHERE tenant_id = $1 AND id = $2`), 54 Args: []driver.Value{TntInternalID, FormationID}, 55 IsSelect: true, 56 ValidRowsProvider: func() []*sqlmock.Rows { 57 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(FormationID, TntInternalID, FormationTemplateID, testFormationName, testFormationState, testFormationEmptyError)} 58 }, 59 InvalidRowsProvider: func() []*sqlmock.Rows { 60 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 61 }, 62 }, 63 }, 64 ConverterMockProvider: func() testdb.Mock { 65 return &automock.EntityConverter{} 66 }, 67 RepoConstructorFunc: formation.NewRepository, 68 ExpectedModelEntity: formationModel, 69 ExpectedDBEntity: formationEntity, 70 MethodArgs: []interface{}{FormationID, TntInternalID}, 71 DisableConverterErrorTest: true, 72 } 73 74 suite.Run(t) 75 } 76 77 func TestRepository_GetGlobalByID(t *testing.T) { 78 suite := testdb.RepoGetTestSuite{ 79 Name: "Get Formation Globally by ID", 80 MethodName: "GetGlobalByID", 81 SQLQueryDetails: []testdb.SQLQueryDetails{ 82 { 83 Query: regexp.QuoteMeta(`SELECT id, tenant_id, formation_template_id, name, state, error FROM public.formations WHERE id = $1`), 84 Args: []driver.Value{FormationID}, 85 IsSelect: true, 86 ValidRowsProvider: func() []*sqlmock.Rows { 87 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(FormationID, TntInternalID, FormationTemplateID, testFormationName, testFormationState, testFormationEmptyError)} 88 }, 89 InvalidRowsProvider: func() []*sqlmock.Rows { 90 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 91 }, 92 }, 93 }, 94 ConverterMockProvider: func() testdb.Mock { 95 return &automock.EntityConverter{} 96 }, 97 RepoConstructorFunc: formation.NewRepository, 98 ExpectedModelEntity: formationModel, 99 ExpectedDBEntity: formationEntity, 100 MethodArgs: []interface{}{FormationID}, 101 DisableConverterErrorTest: true, 102 } 103 104 suite.Run(t) 105 } 106 107 func TestRepository_GetByName(t *testing.T) { 108 suite := testdb.RepoGetTestSuite{ 109 Name: "Get Formation By Name", 110 MethodName: "GetByName", 111 SQLQueryDetails: []testdb.SQLQueryDetails{ 112 { 113 Query: regexp.QuoteMeta(`SELECT id, tenant_id, formation_template_id, name, state, error FROM public.formations WHERE tenant_id = $1 AND name = $2`), 114 Args: []driver.Value{TntInternalID, testFormationName}, 115 IsSelect: true, 116 ValidRowsProvider: func() []*sqlmock.Rows { 117 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(FormationID, TntInternalID, FormationTemplateID, testFormationName, testFormationState, testFormationEmptyError)} 118 }, 119 InvalidRowsProvider: func() []*sqlmock.Rows { 120 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 121 }, 122 }, 123 }, 124 ConverterMockProvider: func() testdb.Mock { 125 return &automock.EntityConverter{} 126 }, 127 RepoConstructorFunc: formation.NewRepository, 128 ExpectedModelEntity: formationModel, 129 ExpectedDBEntity: formationEntity, 130 MethodArgs: []interface{}{testFormationName, TntInternalID}, 131 DisableConverterErrorTest: true, 132 } 133 134 suite.Run(t) 135 } 136 137 func TestRepository_List(t *testing.T) { 138 suite := testdb.RepoListPageableTestSuite{ 139 Name: "List Formations ", 140 MethodName: "List", 141 SQLQueryDetails: []testdb.SQLQueryDetails{ 142 { 143 Query: regexp.QuoteMeta(`SELECT id, tenant_id, formation_template_id, name, state, error FROM public.formations WHERE tenant_id = $1 ORDER BY id LIMIT 4 OFFSET 0`), 144 Args: []driver.Value{TntInternalID}, 145 IsSelect: true, 146 ValidRowsProvider: func() []*sqlmock.Rows { 147 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(FormationID, TntInternalID, FormationTemplateID, testFormationName, testFormationState, testFormationEmptyError)} 148 }, 149 InvalidRowsProvider: func() []*sqlmock.Rows { 150 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 151 }, 152 }, 153 { 154 Query: regexp.QuoteMeta(`SELECT COUNT(*) FROM public.formations`), 155 IsSelect: true, 156 ValidRowsProvider: func() []*sqlmock.Rows { 157 return []*sqlmock.Rows{sqlmock.NewRows([]string{"count"}).AddRow(1)} 158 }, 159 }, 160 }, 161 ConverterMockProvider: func() testdb.Mock { 162 return &automock.EntityConverter{} 163 }, 164 RepoConstructorFunc: formation.NewRepository, 165 Pages: []testdb.PageDetails{ 166 { 167 ExpectedModelEntities: []interface{}{formationModel}, 168 ExpectedDBEntities: []interface{}{formationEntity}, 169 ExpectedPage: &model.FormationPage{ 170 Data: []*model.Formation{formationModel}, 171 PageInfo: &pagination.Page{ 172 StartCursor: "", 173 EndCursor: "", 174 HasNextPage: false, 175 }, 176 TotalCount: 1, 177 }, 178 }, 179 }, 180 MethodArgs: []interface{}{TntInternalID, 4, ""}, 181 DisableConverterErrorTest: true, 182 } 183 184 suite.Run(t) 185 } 186 187 func TestRepository_ListByFormationNames(t *testing.T) { 188 suite := testdb.RepoListTestSuite{ 189 Name: "List Formations ", 190 MethodName: "ListByFormationNames", 191 SQLQueryDetails: []testdb.SQLQueryDetails{ 192 { 193 Query: regexp.QuoteMeta(`SELECT id, tenant_id, formation_template_id, name, state, error FROM public.formations WHERE tenant_id = $1 AND name IN ($2)`), 194 Args: []driver.Value{TntInternalID, formationModel.Name}, 195 IsSelect: true, 196 ValidRowsProvider: func() []*sqlmock.Rows { 197 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(FormationID, TntInternalID, FormationTemplateID, testFormationName, testFormationState, testFormationEmptyError)} 198 }, 199 InvalidRowsProvider: func() []*sqlmock.Rows { 200 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())} 201 }, 202 }, 203 }, 204 ConverterMockProvider: func() testdb.Mock { 205 return &automock.EntityConverter{} 206 }, 207 RepoConstructorFunc: formation.NewRepository, 208 ExpectedModelEntities: []interface{}{formationModel}, 209 ExpectedDBEntities: []interface{}{formationEntity}, 210 MethodArgs: []interface{}{[]string{formationModel.Name}, TntInternalID}, 211 DisableConverterErrorTest: true, 212 } 213 214 suite.Run(t) 215 } 216 217 func TestRepository_Update(t *testing.T) { 218 updateStmt := regexp.QuoteMeta(`UPDATE public.formations SET name = ?, state = ?, error = ? WHERE id = ? AND tenant_id = ?`) 219 suite := testdb.RepoUpdateTestSuite{ 220 Name: "Update Formation by ID", 221 SQLQueryDetails: []testdb.SQLQueryDetails{ 222 { 223 Query: updateStmt, 224 Args: []driver.Value{testFormationName, testFormationState, testFormationEmptyError, FormationID, TntInternalID}, 225 ValidResult: sqlmock.NewResult(-1, 1), 226 InvalidResult: sqlmock.NewResult(-1, 0), 227 }, 228 }, 229 ConverterMockProvider: func() testdb.Mock { 230 return &automock.EntityConverter{} 231 }, 232 RepoConstructorFunc: formation.NewRepository, 233 ModelEntity: formationModel, 234 DBEntity: formationEntity, 235 NilModelEntity: nilFormationModel, 236 DisableConverterErrorTest: true, 237 } 238 239 suite.Run(t) 240 } 241 242 func TestRepository_Delete(t *testing.T) { 243 suite := testdb.RepoDeleteTestSuite{ 244 Name: "Delete Formation by name", 245 SQLQueryDetails: []testdb.SQLQueryDetails{ 246 { 247 Query: regexp.QuoteMeta(`DELETE FROM public.formations WHERE tenant_id = $1 AND name = $2`), 248 Args: []driver.Value{TntInternalID, testFormationName}, 249 ValidResult: sqlmock.NewResult(-1, 1), 250 InvalidResult: sqlmock.NewResult(-1, 2), 251 }, 252 }, 253 RepoConstructorFunc: formation.NewRepository, 254 ConverterMockProvider: func() testdb.Mock { 255 return &automock.EntityConverter{} 256 }, 257 MethodName: "DeleteByName", 258 MethodArgs: []interface{}{TntInternalID, testFormationName}, 259 } 260 261 suite.Run(t) 262 } 263 264 func TestRepository_Exists(t *testing.T) { 265 suite := testdb.RepoExistTestSuite{ 266 Name: "Exists Formation by ID", 267 SQLQueryDetails: []testdb.SQLQueryDetails{ 268 { 269 Query: regexp.QuoteMeta(`SELECT 1 FROM public.formations WHERE tenant_id = $1 AND id = $2`), 270 Args: []driver.Value{TntInternalID, FormationID}, 271 IsSelect: true, 272 ValidRowsProvider: func() []*sqlmock.Rows { 273 return []*sqlmock.Rows{testdb.RowWhenObjectExist()} 274 }, 275 InvalidRowsProvider: func() []*sqlmock.Rows { 276 return []*sqlmock.Rows{testdb.RowWhenObjectDoesNotExist()} 277 }, 278 }, 279 }, 280 RepoConstructorFunc: formation.NewRepository, 281 ConverterMockProvider: func() testdb.Mock { 282 return &automock.EntityConverter{} 283 }, 284 TargetID: FormationID, 285 TenantID: TntInternalID, 286 MethodName: "Exists", 287 MethodArgs: []interface{}{FormationID, TntInternalID}, 288 } 289 290 suite.Run(t) 291 }