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

     1  package formationtemplate_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/formationtemplate"
    13  	"github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplate/automock"
    14  	"github.com/kyma-incubator/compass/components/director/internal/repo/testdb"
    15  )
    16  
    17  func TestRepository_Get(t *testing.T) {
    18  	suite := testdb.RepoGetTestSuite{
    19  		Name:       "Get Formation Template By ID",
    20  		MethodName: "Get",
    21  		SQLQueryDetails: []testdb.SQLQueryDetails{
    22  			{
    23  				Query:    regexp.QuoteMeta(`SELECT id, name, application_types, runtime_types, runtime_type_display_name, runtime_artifact_kind, leading_product_ids, tenant_id FROM public.formation_templates WHERE id = $1`),
    24  				Args:     []driver.Value{testID},
    25  				IsSelect: true,
    26  				ValidRowsProvider: func() []*sqlmock.Rows {
    27  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(formationTemplateEntity.ID, formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntity.TenantID)}
    28  				},
    29  				InvalidRowsProvider: func() []*sqlmock.Rows {
    30  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())}
    31  				},
    32  			},
    33  		},
    34  		ConverterMockProvider: func() testdb.Mock {
    35  			return &automock.EntityConverter{}
    36  		},
    37  		RepoConstructorFunc:       formationtemplate.NewRepository,
    38  		ExpectedModelEntity:       &formationTemplateModel,
    39  		ExpectedDBEntity:          &formationTemplateEntity,
    40  		MethodArgs:                []interface{}{testID},
    41  		DisableConverterErrorTest: false,
    42  	}
    43  
    44  	suite.Run(t)
    45  }
    46  
    47  func TestRepository_GetByNameAndTenant(t *testing.T) {
    48  	suiteWithTenant := testdb.RepoGetTestSuite{
    49  		Name:       "Get Formation Template By name and tenant when tenant is present",
    50  		MethodName: "GetByNameAndTenant",
    51  		SQLQueryDetails: []testdb.SQLQueryDetails{
    52  			{
    53  				Query:    regexp.QuoteMeta(`SELECT id, name, application_types, runtime_types, runtime_type_display_name, runtime_artifact_kind, leading_product_ids, tenant_id FROM public.formation_templates WHERE tenant_id = $1 AND name = $2`),
    54  				Args:     []driver.Value{testTenantID, formationTemplateName},
    55  				IsSelect: true,
    56  				ValidRowsProvider: func() []*sqlmock.Rows {
    57  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(formationTemplateEntity.ID, formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntity.TenantID)}
    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:       formationtemplate.NewRepository,
    68  		ExpectedModelEntity:       &formationTemplateModel,
    69  		ExpectedDBEntity:          &formationTemplateEntity,
    70  		MethodArgs:                []interface{}{formationTemplateName, testTenantID},
    71  		DisableConverterErrorTest: false,
    72  		ExpectNotFoundError:       true,
    73  		AfterNotFoundErrorSQLQueryDetails: []testdb.SQLQueryDetails{
    74  			{
    75  				Query:    regexp.QuoteMeta(`SELECT id, name, application_types, runtime_types, runtime_type_display_name, runtime_artifact_kind, leading_product_ids, tenant_id FROM public.formation_templates WHERE name = $1 AND tenant_id IS NULL`),
    76  				Args:     []driver.Value{formationTemplateName},
    77  				IsSelect: true,
    78  				ValidRowsProvider: func() []*sqlmock.Rows {
    79  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(formationTemplateEntity.ID, formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntityNullTenant.TenantID)}
    80  				},
    81  			},
    82  		},
    83  		AfterNotFoundErrorExpectedDBEntity:    &formationTemplateEntityNullTenant,
    84  		AfterNotFoundErrorExpectedModelEntity: &formationTemplateModelNullTenant,
    85  	}
    86  
    87  	suiteWithoutTenant := testdb.RepoGetTestSuite{
    88  		Name:       "Get Formation Template By name and tenant when tenant is not present",
    89  		MethodName: "GetByNameAndTenant",
    90  		SQLQueryDetails: []testdb.SQLQueryDetails{
    91  			{
    92  				Query:    regexp.QuoteMeta(`SELECT id, name, application_types, runtime_types, runtime_type_display_name, runtime_artifact_kind, leading_product_ids, tenant_id FROM public.formation_templates WHERE name = $1 AND tenant_id IS NULL`),
    93  				Args:     []driver.Value{formationTemplateName},
    94  				IsSelect: true,
    95  				ValidRowsProvider: func() []*sqlmock.Rows {
    96  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(formationTemplateEntity.ID, formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntityNullTenant.TenantID)}
    97  				},
    98  				InvalidRowsProvider: func() []*sqlmock.Rows {
    99  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())}
   100  				},
   101  			},
   102  		},
   103  		ConverterMockProvider: func() testdb.Mock {
   104  			return &automock.EntityConverter{}
   105  		},
   106  		RepoConstructorFunc:       formationtemplate.NewRepository,
   107  		ExpectedModelEntity:       &formationTemplateModelNullTenant,
   108  		ExpectedDBEntity:          &formationTemplateEntityNullTenant,
   109  		MethodArgs:                []interface{}{formationTemplateName, ""},
   110  		DisableConverterErrorTest: false,
   111  	}
   112  
   113  	suiteWithoutTenant.Run(t)
   114  	suiteWithTenant.Run(t)
   115  }
   116  
   117  func TestRepository_Create(t *testing.T) {
   118  	suite := testdb.RepoCreateTestSuite{
   119  		Name:       "Create Formation Template",
   120  		MethodName: "Create",
   121  		SQLQueryDetails: []testdb.SQLQueryDetails{
   122  			{
   123  				Query:       `^INSERT INTO public.formation_templates \(.+\) VALUES \(.+\)$`,
   124  				Args:        []driver.Value{formationTemplateEntity.ID, formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntity.TenantID},
   125  				ValidResult: sqlmock.NewResult(-1, 1),
   126  			},
   127  		},
   128  		ConverterMockProvider: func() testdb.Mock {
   129  			return &automock.EntityConverter{}
   130  		},
   131  		RepoConstructorFunc:       formationtemplate.NewRepository,
   132  		ModelEntity:               &formationTemplateModel,
   133  		DBEntity:                  &formationTemplateEntity,
   134  		NilModelEntity:            nilModelEntity,
   135  		IsGlobal:                  true,
   136  		DisableConverterErrorTest: false,
   137  	}
   138  
   139  	suite.Run(t)
   140  }
   141  
   142  func TestRepository_Exists(t *testing.T) {
   143  	suite := testdb.RepoExistTestSuite{
   144  		Name: "Exists Formation Template By ID",
   145  		SQLQueryDetails: []testdb.SQLQueryDetails{
   146  			{
   147  				Query:    regexp.QuoteMeta(`SELECT 1 FROM public.formation_templates WHERE id = $1`),
   148  				Args:     []driver.Value{testID},
   149  				IsSelect: true,
   150  				ValidRowsProvider: func() []*sqlmock.Rows {
   151  					return []*sqlmock.Rows{testdb.RowWhenObjectExist()}
   152  				},
   153  				InvalidRowsProvider: func() []*sqlmock.Rows {
   154  					return []*sqlmock.Rows{testdb.RowWhenObjectDoesNotExist()}
   155  				},
   156  			},
   157  		},
   158  		RepoConstructorFunc: formationtemplate.NewRepository,
   159  		ConverterMockProvider: func() testdb.Mock {
   160  			return &automock.EntityConverter{}
   161  		},
   162  		TargetID:   testID,
   163  		IsGlobal:   true,
   164  		MethodName: "Exists",
   165  		MethodArgs: []interface{}{testID},
   166  	}
   167  
   168  	suite.Run(t)
   169  }
   170  
   171  func TestRepository_Delete(t *testing.T) {
   172  	suiteWithoutTenant := testdb.RepoDeleteTestSuite{
   173  		Name: "Delete Formation Template By ID when there is no tenant",
   174  		SQLQueryDetails: []testdb.SQLQueryDetails{
   175  			{
   176  				Query:         regexp.QuoteMeta(`DELETE FROM public.formation_templates WHERE id = $1 AND tenant_id IS NULL`),
   177  				Args:          []driver.Value{testID},
   178  				ValidResult:   sqlmock.NewResult(-1, 1),
   179  				InvalidResult: sqlmock.NewResult(-1, 2),
   180  			},
   181  		},
   182  		RepoConstructorFunc: formationtemplate.NewRepository,
   183  		ConverterMockProvider: func() testdb.Mock {
   184  			return &automock.EntityConverter{}
   185  		},
   186  		IsGlobal:   true,
   187  		MethodArgs: []interface{}{testID, ""},
   188  	}
   189  
   190  	suiteWithTenant := testdb.RepoDeleteTestSuite{
   191  		Name: "Delete Formation Template By ID when there is tenant",
   192  		SQLQueryDetails: []testdb.SQLQueryDetails{
   193  			{
   194  				Query:         regexp.QuoteMeta(`DELETE FROM public.formation_templates WHERE tenant_id = $1 AND id = $2`),
   195  				Args:          []driver.Value{testTenantID, testID},
   196  				ValidResult:   sqlmock.NewResult(-1, 1),
   197  				InvalidResult: sqlmock.NewResult(-1, 2),
   198  			},
   199  		},
   200  		RepoConstructorFunc: formationtemplate.NewRepository,
   201  		ConverterMockProvider: func() testdb.Mock {
   202  			return &automock.EntityConverter{}
   203  		},
   204  		MethodArgs: []interface{}{testID, testTenantID},
   205  	}
   206  
   207  	suiteWithTenant.Run(t)
   208  	suiteWithoutTenant.Run(t)
   209  }
   210  
   211  func TestRepository_List(t *testing.T) {
   212  	suiteWithEmptyTenantID := testdb.RepoListPageableTestSuite{
   213  		Name:       "List Formation Templates with paging when there is no tenant",
   214  		MethodName: "List",
   215  		SQLQueryDetails: []testdb.SQLQueryDetails{
   216  			{
   217  				Query:    regexp.QuoteMeta(`SELECT id, name, application_types, runtime_types, runtime_type_display_name, runtime_artifact_kind, leading_product_ids, tenant_id FROM public.formation_templates WHERE tenant_id IS NULL ORDER BY id LIMIT 3 OFFSET 0`),
   218  				IsSelect: true,
   219  				ValidRowsProvider: func() []*sqlmock.Rows {
   220  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(formationTemplateEntity.ID, formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntityNullTenant.TenantID)}
   221  				},
   222  				InvalidRowsProvider: func() []*sqlmock.Rows {
   223  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())}
   224  				},
   225  			},
   226  			{
   227  				Query:    regexp.QuoteMeta(`SELECT COUNT(*) FROM public.formation_templates`),
   228  				IsSelect: true,
   229  				ValidRowsProvider: func() []*sqlmock.Rows {
   230  					return []*sqlmock.Rows{sqlmock.NewRows([]string{"count"}).AddRow(1)}
   231  				},
   232  			},
   233  		},
   234  		Pages: []testdb.PageDetails{
   235  			{
   236  				ExpectedModelEntities: []interface{}{&formationTemplateModelNullTenant},
   237  				ExpectedDBEntities:    []interface{}{&formationTemplateEntityNullTenant},
   238  				ExpectedPage: &model.FormationTemplatePage{
   239  					Data: []*model.FormationTemplate{&formationTemplateModelNullTenant},
   240  					PageInfo: &pagination.Page{
   241  						StartCursor: "",
   242  						EndCursor:   "",
   243  						HasNextPage: false,
   244  					},
   245  					TotalCount: 1,
   246  				},
   247  			},
   248  		},
   249  		ConverterMockProvider: func() testdb.Mock {
   250  			return &automock.EntityConverter{}
   251  		},
   252  		RepoConstructorFunc:       formationtemplate.NewRepository,
   253  		MethodArgs:                []interface{}{"", 3, ""},
   254  		DisableConverterErrorTest: false,
   255  	}
   256  
   257  	suiteWithTenantID := testdb.RepoListPageableTestSuite{
   258  		Name:       "List Formation Templates with paging when tenant is passed",
   259  		MethodName: "List",
   260  		SQLQueryDetails: []testdb.SQLQueryDetails{
   261  			{
   262  				Query:    regexp.QuoteMeta(`SELECT id, name, application_types, runtime_types, runtime_type_display_name, runtime_artifact_kind, leading_product_ids, tenant_id FROM public.formation_templates WHERE (tenant_id IS NULL OR tenant_id = $1) ORDER BY id LIMIT 3 OFFSET 0`),
   263  				Args:     []driver.Value{testTenantID},
   264  				IsSelect: true,
   265  				ValidRowsProvider: func() []*sqlmock.Rows {
   266  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns()).AddRow(formationTemplateEntity.ID, formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntity.TenantID)}
   267  				},
   268  				InvalidRowsProvider: func() []*sqlmock.Rows {
   269  					return []*sqlmock.Rows{sqlmock.NewRows(fixColumns())}
   270  				},
   271  			},
   272  			{
   273  				Query:    regexp.QuoteMeta(`SELECT COUNT(*) FROM public.formation_templates`),
   274  				IsSelect: true,
   275  				ValidRowsProvider: func() []*sqlmock.Rows {
   276  					return []*sqlmock.Rows{sqlmock.NewRows([]string{"count"}).AddRow(1)}
   277  				},
   278  			},
   279  		},
   280  		Pages: []testdb.PageDetails{
   281  			{
   282  				ExpectedModelEntities: []interface{}{&formationTemplateModel},
   283  				ExpectedDBEntities:    []interface{}{&formationTemplateEntity},
   284  				ExpectedPage: &model.FormationTemplatePage{
   285  					Data: []*model.FormationTemplate{&formationTemplateModel},
   286  					PageInfo: &pagination.Page{
   287  						StartCursor: "",
   288  						EndCursor:   "",
   289  						HasNextPage: false,
   290  					},
   291  					TotalCount: 1,
   292  				},
   293  			},
   294  		},
   295  		ConverterMockProvider: func() testdb.Mock {
   296  			return &automock.EntityConverter{}
   297  		},
   298  		RepoConstructorFunc:       formationtemplate.NewRepository,
   299  		MethodArgs:                []interface{}{testTenantID, 3, ""},
   300  		DisableConverterErrorTest: false,
   301  	}
   302  
   303  	suiteWithEmptyTenantID.Run(t)
   304  	suiteWithTenantID.Run(t)
   305  }
   306  
   307  func TestRepository_Update(t *testing.T) {
   308  	updateStmtWithoutTenant := regexp.QuoteMeta(`UPDATE public.formation_templates SET name = ?, application_types = ?, runtime_types = ?, runtime_type_display_name = ?, runtime_artifact_kind = ?, leading_product_ids = ? WHERE id = ?`)
   309  	suiteWithoutTenant := testdb.RepoUpdateTestSuite{
   310  		Name: "Update Formation Template By ID without tenant",
   311  		SQLQueryDetails: []testdb.SQLQueryDetails{
   312  			{
   313  				Query:         updateStmtWithoutTenant,
   314  				Args:          []driver.Value{formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntity.ID},
   315  				ValidResult:   sqlmock.NewResult(-1, 1),
   316  				InvalidResult: sqlmock.NewResult(-1, 0),
   317  			},
   318  		},
   319  		RepoConstructorFunc: formationtemplate.NewRepository,
   320  		ConverterMockProvider: func() testdb.Mock {
   321  			return &automock.EntityConverter{}
   322  		},
   323  		ModelEntity:    &formationTemplateModelNullTenant,
   324  		DBEntity:       &formationTemplateEntityNullTenant,
   325  		NilModelEntity: nilModelEntity,
   326  		IsGlobal:       true,
   327  	}
   328  
   329  	updateStmtWithTenant := regexp.QuoteMeta(`UPDATE public.formation_templates SET name = ?, application_types = ?, runtime_types = ?, runtime_type_display_name = ?, runtime_artifact_kind = ?, leading_product_ids = ? WHERE id = ? AND tenant_id = ?`)
   330  	suiteWithTenant := testdb.RepoUpdateTestSuite{
   331  		Name: "Update Formation Template By ID with tenant",
   332  		SQLQueryDetails: []testdb.SQLQueryDetails{
   333  			{
   334  				Query:         updateStmtWithTenant,
   335  				Args:          []driver.Value{formationTemplateEntity.Name, formationTemplateEntity.ApplicationTypes, formationTemplateEntity.RuntimeTypes, formationTemplateEntity.RuntimeTypeDisplayName, formationTemplateEntity.RuntimeArtifactKind, formationTemplateEntity.LeadingProductIDs, formationTemplateEntity.ID, formationTemplateEntity.TenantID},
   336  				ValidResult:   sqlmock.NewResult(-1, 1),
   337  				InvalidResult: sqlmock.NewResult(-1, 0),
   338  			},
   339  		},
   340  		RepoConstructorFunc: formationtemplate.NewRepository,
   341  		ConverterMockProvider: func() testdb.Mock {
   342  			return &automock.EntityConverter{}
   343  		},
   344  		ModelEntity:    &formationTemplateModel,
   345  		DBEntity:       &formationTemplateEntity,
   346  		NilModelEntity: nilModelEntity,
   347  	}
   348  
   349  	suiteWithTenant.Run(t)
   350  	suiteWithoutTenant.Run(t)
   351  }