summaryrefslogtreecommitdiff
path: root/edge/pkg/devicetwin/dtclient/dbtest.go
blob: d48c6d6847e9767729bccaf36330cd69038f163d (about) (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright 2023 The KubeEdge Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package dtclient

import (
	"errors"
	"testing"

	"github.com/beego/beego/orm"

	"github.com/kubeedge/kubeedge/edge/mocks/beego"
	"github.com/kubeedge/kubeedge/pkg/testtools"
)

// errFailedDBOperation is common DB operation fail error
var errFailedDBOperation = errors.New("Failed DB Operation")

// CasesSaveStr is a struct for cases of save
type CasesSaveStr []struct {
	// name is name of the testcase
	name string
	// returnInt is first return of mock interface ormerMock
	returnInt int64
	// returnErr is second return of mock interface ormerMock which is also expected error
	returnErr error
}

// CasesDeleteStr is a struct for cases of delete
type CasesDeleteStr []struct {
	// name is name of the testcase
	name string
	// filterReturn is the return of mock interface querySeterMock's filter function
	filterReturn orm.QuerySeter
	// deleteReturnInt is the first return of mock interface querySeterMock's delete function
	deleteReturnInt int64
	// deleteReturnErr is the second return of mock interface querySeterMocks's delete function also expected error
	deleteReturnErr error
	// queryTableReturn is the return of mock interface ormerMock's QueryTable function
	queryTableReturn orm.QuerySeter
}

// CasesUpdateStr is a struct for cases of update
type CasesUpdateStr []struct {
	// name is name of the testcase
	name string
	// filterReturn is the return of mock interface querySeterMock's filter function
	filterReturn orm.QuerySeter
	// updateReturnInt is the first return of mock interface querySeterMock's update function
	updateReturnInt int64
	// updateReturnErr is the second return of mock interface querySeterMock's update function also expected error
	updateReturnErr error
	// queryTableReturn is the return of mock interface ormerMock's QueryTable function
	queryTableReturn orm.QuerySeter
}

// CasesQueryStr is a struct for cases of query
type CasesQueryStr []struct {
	// name is name of the testcase
	name string
	// filterReturn is the return of mock interface querySeterMock's filter function
	filterReturn orm.QuerySeter
	// allReturnInt is the first return of mock interface querySeterMock's all function
	allReturnInt int64
	// allReturnErr is the second return of mock interface querySeterMocks's all function also expected error
	allReturnErr error
	// queryTableReturn is the return of mock interface ormerMock's QueryTable function
	queryTableReturn orm.QuerySeter
}

// CasesTransStr is a struct for cases of trans
type CasesTransStr []struct {
	// name is name of the testcase
	name string
	// rollBackTimes is number of times rollback is expected
	rollBackTimes int
	// commitTimes is number of times commit is expected
	commitTimes int
	// beginTimes is number of times begin is expected
	beginTimes int
	// filterReturn is the return of mock interface querySeterMock's filter function
	filterReturn orm.QuerySeter
	// filterTimes is the number of times filter is called
	filterTimes int
	// insertReturnInt is the first return of mock interface ormerMock's Insert function
	insertReturnInt int64
	// insertReturnErr is the second return of mock interface ormerMock's Insert function
	insertReturnErr error
	// insertTimes is number of times Insert is expected
	insertTimes int
	// deleteReturnInt is the first return of mock interface ormerMock's Delete function
	deleteReturnInt int64
	// deleteReturnErr is the second return of mock interface ormerMock's Delete function
	deleteReturnErr error
	// deleteTimes is number of times Delete is expected
	deleteTimes int
	// updateReturnInt is the first return of mock interface ormerMock's Update function
	updateReturnInt int64
	// updateReturnErr is the second return of mock interface ormerMock's Update function
	updateReturnErr error
	// updateTimes is number of times Update is expected
	updateTimes int
	// queryTableReturn is the return of mock interface ormerMock's QueryTable function
	queryTableReturn orm.QuerySeter
	// queryTableTimes is the number of times queryTable is called
	queryTableTimes int
	// wantErr is expected error
	wantErr error
}

// GetCasesSave get cases for save
func GetCasesSave(t *testing.T) (*beego.MockOrmer, CasesSaveStr) {
	ormerMock, _ := testtools.InitOrmerMock(t)
	return ormerMock, CasesSaveStr{{
		// Success Case
		name:      "SuccessCase",
		returnInt: int64(1),
		returnErr: nil,
	}, {
		// Failure Case
		name:      "FailureCase",
		returnInt: int64(1),
		returnErr: errFailedDBOperation,
	},
	}
}

// GetCasesDelete get cases for delete
func GetCasesDelete(t *testing.T) (*beego.MockOrmer, *beego.MockQuerySeter, CasesDeleteStr) {
	ormerMock, querySeterMock := testtools.InitOrmerMock(t)
	return ormerMock, querySeterMock, CasesDeleteStr{{
		// Success Case
		name:             "SuccessCase",
		filterReturn:     querySeterMock,
		deleteReturnInt:  int64(1),
		deleteReturnErr:  nil,
		queryTableReturn: querySeterMock,
	}, {
		// Failure Case
		name:             "FailureCase",
		filterReturn:     querySeterMock,
		deleteReturnInt:  int64(0),
		deleteReturnErr:  errFailedDBOperation,
		queryTableReturn: querySeterMock,
	},
	}
}

// GetCasesUpdate get cases for update
func GetCasesUpdate(t *testing.T) (*beego.MockOrmer, *beego.MockQuerySeter, CasesUpdateStr) {
	ormerMock, querySeterMock := testtools.InitOrmerMock(t)
	return ormerMock, querySeterMock, CasesUpdateStr{{
		// Success Case
		name:             "SuccessCase",
		filterReturn:     querySeterMock,
		updateReturnInt:  int64(1),
		updateReturnErr:  nil,
		queryTableReturn: querySeterMock,
	}, {
		// Failure Case
		name:             "FailureCase",
		filterReturn:     querySeterMock,
		updateReturnInt:  int64(0),
		updateReturnErr:  errFailedDBOperation,
		queryTableReturn: querySeterMock,
	},
	}
}

// GetCasesQuery get cases for query
func GetCasesQuery(t *testing.T) (*beego.MockOrmer, *beego.MockQuerySeter, CasesQueryStr) {
	ormerMock, querySeterMock := testtools.InitOrmerMock(t)
	return ormerMock, querySeterMock, CasesQueryStr{{
		// Success Case
		name:             "SuccessCase",
		filterReturn:     querySeterMock,
		allReturnInt:     int64(1),
		allReturnErr:     nil,
		queryTableReturn: querySeterMock,
	}, {
		// Failure Case
		name:             "FailureCase",
		filterReturn:     querySeterMock,
		allReturnInt:     int64(0),
		allReturnErr:     errFailedDBOperation,
		queryTableReturn: querySeterMock,
	},
	}
}

// GetCasesTrans get cases for trans
func GetCasesTrans(caseName string, t *testing.T) (*beego.MockOrmer, *beego.MockQuerySeter, CasesTransStr) {
	ormerMock, querySeterMock := testtools.InitOrmerMock(t)
	return ormerMock, querySeterMock, CasesTransStr{{
		// Failure Case SaveDeviceAttr
		name:             caseName + "TransSaveFailureCase",
		rollBackTimes:    1,
		commitTimes:      0,
		beginTimes:       1,
		filterReturn:     nil,
		filterTimes:      0,
		insertReturnInt:  int64(1),
		insertReturnErr:  errFailedDBOperation,
		insertTimes:      1,
		deleteReturnInt:  int64(1),
		deleteReturnErr:  nil,
		deleteTimes:      0,
		updateReturnInt:  int64(1),
		updateReturnErr:  nil,
		updateTimes:      0,
		queryTableReturn: nil,
		queryTableTimes:  0,
		wantErr:          errFailedDBOperation,
	}, {
		// Failure Case DeleteDeviceAttr
		name:             caseName + "TransDeleteFailureCase",
		rollBackTimes:    1,
		commitTimes:      0,
		beginTimes:       1,
		filterReturn:     querySeterMock,
		filterTimes:      2,
		insertReturnInt:  int64(1),
		insertReturnErr:  nil,
		insertTimes:      1,
		deleteReturnInt:  int64(1),
		deleteReturnErr:  errFailedDBOperation,
		deleteTimes:      1,
		updateReturnInt:  int64(1),
		updateReturnErr:  nil,
		updateTimes:      0,
		queryTableReturn: querySeterMock,
		queryTableTimes:  1,
		wantErr:          errFailedDBOperation,
	}, {
		// Failure Case UpdateDeviceAttrFields
		name:             caseName + "TransUpdateFieldsFailureCase",
		rollBackTimes:    1,
		commitTimes:      0,
		beginTimes:       1,
		filterReturn:     querySeterMock,
		filterTimes:      4,
		insertReturnInt:  int64(1),
		insertReturnErr:  nil,
		insertTimes:      1,
		deleteReturnInt:  int64(1),
		deleteReturnErr:  nil,
		deleteTimes:      1,
		updateReturnInt:  int64(1),
		updateReturnErr:  errFailedDBOperation,
		updateTimes:      1,
		queryTableReturn: querySeterMock,
		queryTableTimes:  2,
		wantErr:          errFailedDBOperation,
	}, {
		// Success Case
		name:             caseName + "TransSuccessCase",
		rollBackTimes:    0,
		commitTimes:      1,
		beginTimes:       1,
		filterReturn:     querySeterMock,
		filterTimes:      4,
		insertReturnInt:  int64(1),
		insertReturnErr:  nil,
		insertTimes:      1,
		deleteReturnInt:  int64(1),
		deleteReturnErr:  nil,
		deleteTimes:      1,
		updateReturnInt:  int64(1),
		updateReturnErr:  nil,
		updateTimes:      1,
		queryTableReturn: querySeterMock,
		queryTableTimes:  2,
		wantErr:          nil,
	},
	}
}