-
Notifications
You must be signed in to change notification settings - Fork 84
/
generic_repo_create_tests.go
247 lines (215 loc) · 8.44 KB
/
generic_repo_create_tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package testdb
import (
"context"
"database/sql/driver"
"fmt"
"reflect"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
"github.com/kyma-incubator/compass/components/director/pkg/persistence"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// Mock represents a mockery Mock.
type Mock interface {
AssertExpectations(t mock.TestingT) bool
On(methodName string, arguments ...interface{}) *mock.Call
}
// SQLQueryDetails represent an SQL expected query details to provide to the DB mock.
type SQLQueryDetails struct {
Query string
IsSelect bool
Args []driver.Value
ValidResult driver.Result
InvalidResult driver.Result
ValidRowsProvider func() []*sqlmock.Rows
InvalidRowsProvider func() []*sqlmock.Rows
SkipFailValidation bool
}
// RepoCreateTestSuite represents a generic test suite for repository Create method of any global entity or entity that has externally managed tenants in m2m table/view.
type RepoCreateTestSuite struct {
Name string
SQLQueryDetails []SQLQueryDetails
ConverterMockProvider func() Mock
RepoConstructorFunc interface{}
ModelEntity interface{}
DBEntity interface{}
NilModelEntity interface{}
TenantID string
DisableConverterErrorTest bool
MethodName string
IsTopLevelEntity bool
IsGlobal bool
}
// Run runs the generic repo create test suite
func (suite *RepoCreateTestSuite) Run(t *testing.T) bool {
if len(suite.MethodName) == 0 {
suite.MethodName = "Create"
}
return t.Run(suite.Name, func(t *testing.T) {
testErr := errors.New("test error")
t.Run("success", func(t *testing.T) {
sqlxDB, sqlMock := MockDatabase(t)
ctx := persistence.SaveToContext(context.TODO(), sqlxDB)
configureValidSQLQueries(sqlMock, suite.SQLQueryDetails)
convMock := suite.ConverterMockProvider()
convMock.On("ToEntity", suite.ModelEntity).Return(suite.DBEntity, nil).Once()
pgRepository := createRepo(suite.RepoConstructorFunc, convMock)
// WHEN
err := callCreate(pgRepository, suite.MethodName, ctx, suite.TenantID, suite.ModelEntity)
// THEN
require.NoError(t, err)
sqlMock.AssertExpectations(t)
convMock.AssertExpectations(t)
})
if !suite.IsTopLevelEntity && !suite.IsGlobal {
t.Run("error when parent access is missing", func(t *testing.T) {
sqlxDB, sqlMock := MockDatabase(t)
ctx := persistence.SaveToContext(context.TODO(), sqlxDB)
configureInvalidSelect(sqlMock, suite.SQLQueryDetails)
convMock := suite.ConverterMockProvider()
convMock.On("ToEntity", suite.ModelEntity).Return(suite.DBEntity, nil).Once()
pgRepository := createRepo(suite.RepoConstructorFunc, convMock)
// WHEN
err := callCreate(pgRepository, suite.MethodName, ctx, suite.TenantID, suite.ModelEntity)
// THEN
require.Error(t, err)
require.Equal(t, apperrors.Unauthorized, apperrors.ErrorCode(err))
require.Contains(t, err.Error(), fmt.Sprintf("Tenant %s does not have access to the parent", suite.TenantID))
sqlMock.AssertExpectations(t)
convMock.AssertExpectations(t)
})
}
for i := range suite.SQLQueryDetails {
t.Run(fmt.Sprintf("error if SQL query %d fail", i), func(t *testing.T) {
sqlxDB, sqlMock := MockDatabase(t)
ctx := persistence.SaveToContext(context.TODO(), sqlxDB)
configureFailureForSQLQueryOnIndex(sqlMock, suite.SQLQueryDetails, i, testErr)
convMock := suite.ConverterMockProvider()
convMock.On("ToEntity", suite.ModelEntity).Return(suite.DBEntity, nil).Once()
pgRepository := createRepo(suite.RepoConstructorFunc, convMock)
// WHEN
err := callCreate(pgRepository, suite.MethodName, ctx, suite.TenantID, suite.ModelEntity)
// THEN
require.Error(t, err)
if suite.SQLQueryDetails[i].IsSelect {
require.Equal(t, apperrors.Unauthorized, apperrors.ErrorCode(err))
require.Contains(t, err.Error(), fmt.Sprintf("Tenant %s does not have access to the parent", suite.TenantID))
} else {
require.Equal(t, apperrors.InternalError, apperrors.ErrorCode(err))
require.Contains(t, err.Error(), "Internal Server Error: Unexpected error while executing SQL query")
}
sqlMock.AssertExpectations(t)
convMock.AssertExpectations(t)
})
}
if !suite.DisableConverterErrorTest {
t.Run("error when conversion fail", func(t *testing.T) {
sqlxDB, sqlMock := MockDatabase(t)
ctx := persistence.SaveToContext(context.TODO(), sqlxDB)
convMock := suite.ConverterMockProvider()
convMock.On("ToEntity", suite.ModelEntity).Return(nil, testErr).Once()
pgRepository := createRepo(suite.RepoConstructorFunc, convMock)
// WHEN
err := callCreate(pgRepository, suite.MethodName, ctx, suite.TenantID, suite.ModelEntity)
// THEN
require.Error(t, err)
require.Contains(t, err.Error(), testErr.Error())
sqlMock.AssertExpectations(t)
convMock.AssertExpectations(t)
})
}
t.Run("returns error when item is nil", func(t *testing.T) {
ctx := context.TODO()
convMock := suite.ConverterMockProvider()
pgRepository := createRepo(suite.RepoConstructorFunc, convMock)
// WHEN
err := callCreate(pgRepository, suite.MethodName, ctx, suite.TenantID, suite.NilModelEntity)
// THEN
require.Error(t, err)
assert.Contains(t, err.Error(), "Internal Server Error")
convMock.AssertExpectations(t)
})
})
}
// callCreate calls the Create method of the given repository.
// In order to do this for all the different repository implementations we need to do it via reflection.
func callCreate(repo interface{}, methodName string, ctx context.Context, tenant string, modelEntity interface{}) error {
args := []reflect.Value{reflect.ValueOf(ctx)}
if len(tenant) > 0 {
args = append(args, reflect.ValueOf(tenant))
}
args = append(args, reflect.ValueOf(modelEntity))
results := reflect.ValueOf(repo).MethodByName(methodName).Call(args)
if len(results) != 1 {
panic("Create should return one argument")
}
result := results[0].Interface()
if result == nil {
return nil
}
err, ok := result.(error)
if !ok {
panic("Expected result to be an error")
}
return err
}
// createRepo creates a new repository by the provided constructor func.
// In order to do this for all the different repository implementations we need to do it via reflection.
func createRepo(repoConstructorFunc interface{}, convMock interface{}) interface{} {
v := reflect.ValueOf(repoConstructorFunc)
if v.Kind() != reflect.Func {
panic("Repo constructor should be a function")
}
t := v.Type()
if t.NumOut() != 1 {
panic("Repo constructor should return only one argument")
}
if t.NumIn() == 0 {
return v.Call(nil)[0].Interface()
}
if t.NumIn() != 1 {
panic("Repo constructor should accept zero or one arguments")
}
mockVal := reflect.ValueOf(convMock)
return v.Call([]reflect.Value{mockVal})[0].Interface()
}
func configureValidSQLQueries(sqlMock DBMock, sqlQueryDetails []SQLQueryDetails) {
for _, sqlDetails := range sqlQueryDetails {
if sqlDetails.IsSelect {
sqlMock.ExpectQuery(sqlDetails.Query).WithArgs(sqlDetails.Args...).WillReturnRows(sqlDetails.ValidRowsProvider()...)
} else {
sqlMock.ExpectExec(sqlDetails.Query).WithArgs(sqlDetails.Args...).WillReturnResult(sqlDetails.ValidResult)
}
}
}
func configureInvalidSelect(sqlMock DBMock, sqlQueryDetails []SQLQueryDetails) {
for _, sqlDetails := range sqlQueryDetails {
if sqlDetails.IsSelect {
sqlMock.ExpectQuery(sqlDetails.Query).WithArgs(sqlDetails.Args...).WillReturnRows(sqlDetails.InvalidRowsProvider()...)
break
}
}
}
func configureFailureForSQLQueryOnIndex(sqlMock DBMock, sqlQueryDetails []SQLQueryDetails, i int, expectedErr error) {
for _, sqlDetails := range sqlQueryDetails {
if sqlDetails.IsSelect {
if sqlDetails.Query == sqlQueryDetails[i].Query {
sqlMock.ExpectQuery(sqlDetails.Query).WithArgs(sqlDetails.Args...).WillReturnError(expectedErr)
break
} else {
sqlMock.ExpectQuery(sqlDetails.Query).WithArgs(sqlDetails.Args...).WillReturnRows(sqlDetails.ValidRowsProvider()...)
}
} else {
if sqlDetails.Query == sqlQueryDetails[i].Query {
sqlMock.ExpectExec(sqlDetails.Query).WithArgs(sqlDetails.Args...).WillReturnError(expectedErr)
break
} else {
sqlMock.ExpectExec(sqlDetails.Query).WithArgs(sqlDetails.Args...).WillReturnResult(sqlDetails.ValidResult)
}
}
}
}