/
tester.go
320 lines (265 loc) · 12.4 KB
/
tester.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package dsunit
import (
"fmt"
"github.com/viant/toolbox"
"path"
"testing"
)
var LogF = fmt.Printf
type Tester interface {
// Register registers new datastore connection
Register(t *testing.T, request *RegisterRequest) bool
// RegisterFromURL registers new datastore connection, JSON request is fetched from URL
RegisterFromURL(t *testing.T, URL string) bool
// Recreate recreates datastore
Recreate(t *testing.T, request *RecreateRequest) bool
// RecreateFromURL recreates datastore, JSON request is fetched from URL
RecreateFromURL(t *testing.T, URL string) bool
// RunSQL runs supplied SQL
RunSQL(t *testing.T, request *RunSQLRequest) bool
// RunSQLFromURL runs supplied SQL, JSON request is fetched from URL
RunSQLFromURL(t *testing.T, URL string) bool
// RunScript runs supplied SQL scripts
RunScript(t *testing.T, request *RunScriptRequest) bool
// RunScriptFromURL runs supplied SQL scripts, JSON request is fetched from URL
RunScriptFromURL(t *testing.T, URL string) bool
// AddTableMapping adds table mapping
AddTableMapping(t *testing.T, request *MappingRequest) bool
// AddTableMappingFromURL adds table mapping, JSON request is fetched from URL
AddTableMappingFromURL(t *testing.T, URL string) bool
//Init inits datastore, (register, recreated, run sql, add mapping)
Init(t *testing.T, request *InitRequest) bool
// InitFromURL inits datastore, (register, recreated, run sql, add mapping), JSON request is fetched from URL
InitFromURL(t *testing.T, URL string) bool
// Prepare populates database with datasets
Prepare(t *testing.T, request *PrepareRequest) bool
// PrepareFromURL populates database with datasets, JSON request is fetched from URL
PrepareFromURL(t *testing.T, URL string) bool
// PopulateWithURL populates database with datasets, JSON requests are fetched from files in directory
PopulateWithURL(t *testing.T, URL string, datastore string, datasets ...*Dataset) bool
//PrepareDatastore matches all dataset files that are in the same location as a test file, with the same test file prefix, followed by lowe camel case test name.
PrepareDatastore(t *testing.T, datastore string) bool
// PrepareFor matches all dataset files that are located in baseDirectory with method name and
// populate datastore with all listed dataset
// Note the matchable dataset files in the base directory have the following naming:
//
// <lower_underscore method name>_populate_<table>.[json|csv]
// To prepare dataset to populate datastore table: 'users' and 'permissions' for test method ReadAll you would
// have you create the following files in the baseDirectory
//
// read_all_prepare_travelers2.json
// read_all_populate_permissions.json
//
PrepareFor(t *testing.T, datastore string, baseDirectory string, method string) bool
// Expect verifies datastore with supplied expected datasets
Expect(t *testing.T, request *ExpectRequest) bool
// ExpectFromURL verifies datastore with supplied expected datasets, JSON request is fetched from URL
ExpectFromURL(t *testing.T, URL string) bool
// ExpectWithURL verifies datastore with supplied expected datasets, JSON requests are fetched from files in directory
ExpectWithURL(t *testing.T, checkPolicy int, URL string, datastore string, datasets ...*Dataset) bool
// ExpectDatasets matches all dataset files that are located in the same directory as the test file with method name to
// verify that all listed dataset values are present in datastore
ExpectDatasets(t *testing.T, datastore string, checkPolicy int) bool
// ExpectFor matches all dataset files that are located in baseDirectory with method name to
// verify that all listed dataset values are present in datastore
// Note the matchable dataset files in the base directory have the following naming:
//
// <lower_underscore method name>_expect_<table>.[json|csv]
// To prepare expected dataset table: 'users' and 'permissions' for test method ReadAll you would
// have you create the following files in the baseDirectory
//
// read_all_expect_users.json
// read_all_expect_permissions.json
//
ExpectFor(t *testing.T, datastore string, checkPolicy int, baseDirectory string, method string) bool
// Ping waits until database is online or error
Ping(t *testing.T, datastore string, timeoutMs int) bool
}
type localTester struct {
service Service
}
func handleError(t *testing.T, err error) {
if err != nil {
file, method, line := toolbox.DiscoverCaller(2, 10, "stack_helper.go", "static.go", "tester.go", "helper.go")
_, file = path.Split(file)
fmt.Printf("%v:%v (%v)\n%v\n", file, line, method, err)
t.FailNow()
}
}
func handleResponse(t *testing.T, response *BaseResponse) bool {
file, method, line := toolbox.DiscoverCaller(3, 10, "stack_helper.go", "static.go", "tester.go", "helper.go")
_, file = path.Split(file)
if response.Status != StatusOk {
_, _ = LogF("%v:%v (%v)\n%v\n", file, line, method, response.Message)
t.Fail()
return false
}
return true
}
// Register registers new datastore connection
func (s *localTester) Register(t *testing.T, request *RegisterRequest) bool {
response := s.service.Register(request)
return handleResponse(t, response.BaseResponse)
}
// RegisterFromURL registers new datastore connection, JSON request is fetched from URL
func (s *localTester) RegisterFromURL(t *testing.T, URL string) bool {
request, err := NewRegisterRequestFromURL(URL)
handleError(t, err)
return s.Register(t, request)
}
// Recreate recreates datastore
func (s *localTester) Recreate(t *testing.T, request *RecreateRequest) bool {
response := s.service.Recreate(request)
return handleResponse(t, response.BaseResponse)
}
// RecreateFromURL Recreate recreates datastore, JSON request is fetched from URL
func (s *localTester) RecreateFromURL(t *testing.T, URL string) bool {
request, err := NewRecreateRequestFromURL(URL)
handleError(t, err)
return s.Recreate(t, request)
}
// RunSQL runs supplied SQL
func (s *localTester) RunSQL(t *testing.T, request *RunSQLRequest) bool {
response := s.service.RunSQL(request)
return handleResponse(t, response.BaseResponse)
}
// RunSQLFromURL runs supplied SQL, JSON request is fetched from URL
func (s *localTester) RunSQLFromURL(t *testing.T, URL string) bool {
request, err := NewRunSQLRequestFromURL(URL)
handleError(t, err)
return s.RunSQL(t, request)
}
// RunScript runs supplied SQL scripts
func (s *localTester) RunScript(t *testing.T, request *RunScriptRequest) bool {
response := s.service.RunScript(request)
return handleResponse(t, response.BaseResponse)
}
// RunScriptFromURL runs supplied SQL scripts, JSON request is fetched from URL
func (s *localTester) RunScriptFromURL(t *testing.T, URL string) bool {
request, err := NewRunScriptRequestFromURL(URL)
handleError(t, err)
return s.RunScript(t, request)
}
// AddTableMapping adds table mapping
func (s *localTester) AddTableMapping(t *testing.T, request *MappingRequest) bool {
response := s.service.AddTableMapping(request)
return handleResponse(t, response.BaseResponse)
}
// AddTableMappingFromURL adds table mapping, JSON request is fetched from URL
func (s *localTester) AddTableMappingFromURL(t *testing.T, URL string) bool {
request, err := NewMappingRequestFromURL(URL)
handleError(t, err)
return s.AddTableMapping(t, request)
}
// Init inits datastore, (register, recreated, run sql, add mapping)
func (s *localTester) Init(t *testing.T, request *InitRequest) bool {
response := s.service.Init(request)
return handleResponse(t, response.BaseResponse)
}
// InitFromURL inits datastore, (register, recreated, run sql, add mapping), JSON request is fetched from URL
func (s *localTester) InitFromURL(t *testing.T, URL string) bool {
request, err := NewInitRequestFromURL(URL)
handleError(t, err)
return s.Init(t, request)
}
// Prepare populates database with datasets
func (s *localTester) Prepare(t *testing.T, request *PrepareRequest) bool {
response := s.service.Prepare(request)
return handleResponse(t, response.BaseResponse)
}
// PrepareFromURL populates database with datasets, JSON request is fetched from URL
func (s *localTester) PrepareFromURL(t *testing.T, URL string) bool {
request, err := NewPrepareRequestFromURL(URL)
handleError(t, err)
return s.Prepare(t, request)
}
// PopulateWithURL populates database with datasets, JSON requests are fetched from files in directory
func (s *localTester) PopulateWithURL(t *testing.T, URL string, datastore string, datasets ...*Dataset) bool {
populateSet := NewDatasetResource(datastore, URL, "", "", datasets...)
return s.Prepare(t, NewPrepareRequest(populateSet))
}
// PrepareDatastore matches all dataset files that are in the same location as a test file, with the same test file prefix, followed by lowe camel case test name.
func (s *localTester) PrepareDatastore(t *testing.T, datastore string) bool {
URL, prefix := discoverBaseURLAndPrefix("prepare")
request := &PrepareRequest{
DatasetResource: NewDatasetResource(datastore, URL, prefix, ""),
}
return s.Prepare(t, request)
}
// PrepareFor matches all dataset files that are located in baseDirectory with method name and
// populate datastore with all listed dataset
// Note the matchable dataset files in the base directory have the following naming:
//
// <lower_underscore method name>_populate_<table>.[json|csv]
// To prepare dataset to populate datastore table: 'users' and 'permissions' for test method ReadAll you would
// have you create the following files in the baseDirectory
//
// read_all_prepare_travelers2.json
// read_all_populate_permissions.json
//
func (s *localTester) PrepareFor(t *testing.T, datastore, baseDirectory, method string) bool {
method = convertToLowerUnderscore(method)
request := &PrepareRequest{
DatasetResource: NewDatasetResource(datastore, baseDirectory, fmt.Sprintf("%v_prepare_", method), ""),
Expand: true,
}
return s.Prepare(t, request)
}
// Expect verifies datastore with supplied expected datasets
func (s *localTester) Expect(t *testing.T, request *ExpectRequest) bool {
response := s.service.Expect(request)
var result = handleResponse(t, response.BaseResponse)
return result
}
// ExpectFromURL verifies datastore with supplied expected datasets, JSON request is fetched from URL
func (s *localTester) ExpectFromURL(t *testing.T, URL string) bool {
request, err := NewExpectRequestFromURL(URL)
handleError(t, err)
return s.Expect(t, request)
}
// ExpectWithURL verifies datastore with supplied expected datasets, JSON requests are fetched from files in directory
func (s *localTester) ExpectWithURL(t *testing.T, checkPolicy int, URL string, datastore string, datasets ...*Dataset) bool {
expextSet := NewDatasetResource(datastore, URL, "", "", datasets...)
return s.Expect(t, NewExpectRequest(checkPolicy, expextSet))
}
// ExpectDatasets matches all dataset files that are located in the same directory as the test file with method name to
// verify that all listed dataset values are present in datastore
func (s *localTester) ExpectDatasets(t *testing.T, datastore string, checkPolicy int) bool {
URL, prefix := discoverBaseURLAndPrefix("expect")
request := &ExpectRequest{
CheckPolicy: checkPolicy,
DatasetResource: NewDatasetResource(datastore, URL, prefix, ""),
}
return s.Expect(t, request)
}
// ExpectFor matches all dataset files that are located in baseDirectory with method name to
// verify that all listed dataset values are present in datastore
// Note the matchable dataset files in the base directory have the following naming:
//
// <lower_underscore method name>_expect_<table>.[json|csv]
// To prepare expected dataset table: 'users' and 'permissions' for test method ReadAll you would
// have you create the following files in the baseDirectory
//
// read_all_expect_users.json
// read_all_expect_permissions.json
//
func (s *localTester) ExpectFor(t *testing.T, datastore string, checkPolicy int, baseDirectory, method string) bool {
method = convertToLowerUnderscore(method)
request := &ExpectRequest{
DatasetResource: NewDatasetResource(datastore, baseDirectory, fmt.Sprintf("%v_expect_", method), ""),
}
return s.Expect(t, request)
}
func (s *localTester) Ping(t *testing.T, datastore string, timeoutMs int) bool {
request := &PingRequest{Datastore: datastore, TimeoutMs: timeoutMs}
response := s.service.Ping(request)
return handleResponse(t, response.BaseResponse)
}
// NewTester creates a new local tester
func NewTester() Tester {
return &localTester{service: New()}
}
// NewRemoveTester creates a new remove tester
func NewRemoveTester(endpoint string) Tester {
return &localTester{service: NewServiceClient(endpoint)}
}