-
Notifications
You must be signed in to change notification settings - Fork 84
/
generic_repo_update_tests.go
191 lines (165 loc) · 6.82 KB
/
generic_repo_update_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
package testdb
import (
"context"
"fmt"
"reflect"
"testing"
"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/require"
)
// RepoUpdateTestSuite represents a generic test suite for repository Update and UpdateWithVersion methods of any global entity or entity that has externally managed tenants in m2m table/view.
type RepoUpdateTestSuite struct {
Name string
SQLQueryDetails []SQLQueryDetails
ConverterMockProvider func() Mock
RepoConstructorFunc interface{}
ModelEntity interface{}
DBEntity interface{}
NilModelEntity interface{}
TenantID string
DisableConverterErrorTest bool
UpdateMethodName string
IsGlobal bool
}
// Run runs the generic repo update test suite
func (suite *RepoUpdateTestSuite) Run(t *testing.T) bool {
if len(suite.UpdateMethodName) == 0 {
suite.UpdateMethodName = "Update"
}
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 := callUpdate(pgRepository, ctx, suite.TenantID, suite.ModelEntity, suite.UpdateMethodName)
// THEN
require.NoError(t, err)
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 := callUpdate(pgRepository, ctx, suite.TenantID, suite.ModelEntity, suite.UpdateMethodName)
// THEN
require.Error(t, err)
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.UpdateMethodName == "UpdateWithVersion" { // We have an exists check
t.Run("error when entity 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 := callUpdate(pgRepository, ctx, suite.TenantID, suite.ModelEntity, suite.UpdateMethodName)
// THEN
require.Error(t, err)
require.Equal(t, apperrors.InvalidOperation, apperrors.ErrorCode(err))
require.Contains(t, err.Error(), "entity does not exist or caller tenant does not have owner access")
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 := callUpdate(pgRepository, ctx, suite.TenantID, suite.ModelEntity, suite.UpdateMethodName)
// THEN
require.Error(t, err)
require.Contains(t, err.Error(), testErr.Error())
sqlMock.AssertExpectations(t)
convMock.AssertExpectations(t)
})
}
t.Run("error if 0 affected rows", func(t *testing.T) {
sqlxDB, sqlMock := MockDatabase(t)
ctx := persistence.SaveToContext(context.TODO(), sqlxDB)
configureInvalidUpdateSQLQuery(sqlMock, suite.SQLQueryDetails)
convMock := suite.ConverterMockProvider()
convMock.On("ToEntity", suite.ModelEntity).Return(suite.DBEntity, nil).Once()
pgRepository := createRepo(suite.RepoConstructorFunc, convMock)
// WHEN
err := callUpdate(pgRepository, ctx, suite.TenantID, suite.ModelEntity, suite.UpdateMethodName)
// THEN
require.Error(t, err)
if suite.UpdateMethodName == "UpdateWithVersion" {
require.Equal(t, apperrors.ConcurrentUpdate, apperrors.ErrorCode(err))
require.Contains(t, err.Error(), apperrors.ConcurrentUpdateMsg)
} else if suite.IsGlobal {
require.Equal(t, apperrors.InternalError, apperrors.ErrorCode(err))
require.Contains(t, err.Error(), fmt.Sprintf(apperrors.ShouldUpdateSingleRowButUpdatedMsgF, 0))
} else {
require.Equal(t, apperrors.Unauthorized, apperrors.ErrorCode(err))
require.Contains(t, err.Error(), apperrors.ShouldBeOwnerMsg)
}
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 := callUpdate(pgRepository, ctx, suite.TenantID, suite.NilModelEntity, suite.UpdateMethodName)
// THEN
require.Error(t, err)
assert.Contains(t, err.Error(), "Internal Server Error")
convMock.AssertExpectations(t)
})
})
}
func callUpdate(repo interface{}, ctx context.Context, tenant string, modelEntity interface{}, methodName string) 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("Update 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
}
func configureInvalidUpdateSQLQuery(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.InvalidResult)
}
}
}