-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTestPlanRunService.js
444 lines (422 loc) · 15.9 KB
/
TestPlanRunService.js
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
const ModelService = require('./ModelService');
const {
TEST_PLAN_RUN_ATTRIBUTES,
TEST_PLAN_REPORT_ATTRIBUTES,
USER_ATTRIBUTES,
TEST_PLAN_VERSION_ATTRIBUTES,
AT_ATTRIBUTES,
BROWSER_ATTRIBUTES,
TEST_PLAN_ATTRIBUTES
} = require('./helpers');
const { TestPlanRun, sequelize } = require('../');
// association helpers to be included with Models' results
/**
* @param {string[]} testPlanReportAttributes - TestPlanReport attributes
* @param {string[]} testPlanRunAttributes - TestPlanRun attributes
* @param {string[]} testPlanVersionAttributes - TestPlanVersion attributes
* @param {string[]} atAttributes - AT attributes
* @param {string[]} browserAttributes - Browser attributes
* @param {string[]} userAttributes - User attributes
* @returns {{association: string, attributes: string[]}}
*/
const testPlanReportAssociation = (
testPlanReportAttributes,
testPlanRunAttributes,
testPlanVersionAttributes,
testPlanAttributes,
atAttributes,
browserAttributes,
userAttributes
) => ({
association: 'testPlanReport',
attributes: testPlanReportAttributes,
include: [
// eslint-disable-next-line no-use-before-define
testPlanRunAssociation(testPlanRunAttributes, userAttributes),
// eslint-disable-next-line no-use-before-define
testPlanVersionAssociation(testPlanVersionAttributes, testPlanAttributes),
// eslint-disable-next-line no-use-before-define
atAssociation(atAttributes),
// eslint-disable-next-line no-use-before-define
browserAssociation(browserAttributes)
]
});
/**
* When the test plan run model loads the test plan report, it should still load
* all the test plan runs, otherwise you would not be able to compare the
* current run to the complete list of runs.
* @param {string[]} testPlanRunAttributes - TestPlanRun attributes
* @param {string[]} userAttributes - User attributes
* @returns {{association: string, attributes: string[]}}
*/
const testPlanRunAssociation = (testPlanRunAttributes, userAttributes) => ({
association: 'testPlanRuns',
attributes: testPlanRunAttributes,
include: [
// eslint-disable-next-line no-use-before-define
userAssociation(userAttributes)
]
});
/**
* @param {string[]} testPlanVersionAttributes - TestPlanVersion attributes
* @returns {{association: string, attributes: string[]}}
*/
const testPlanVersionAssociation = (
testPlanVersionAttributes,
testPlanAttributes
) => ({
association: 'testPlanVersion',
attributes: testPlanVersionAttributes,
include: [
// eslint-disable-next-line no-use-before-define
testPlanAssociation(testPlanAttributes)
]
});
/**
* @param {string[]} testPlanAttributes - TestPlan attributes
* @returns {{association: string, attributes: string[]}}
*/
const testPlanAssociation = testPlanAttributes => ({
association: 'testPlan',
attributes: testPlanAttributes
});
/**
* @param {string[]} atAttributes - AT attributes
* @returns {{association: string, attributes: string[]}}
*/
const atAssociation = atAttributes => ({
association: 'at',
attributes: atAttributes
});
/**
* @param {string[]} browserAttributes - Browser attributes
* @returns {{association: string, attributes: string[]}}
*/
const browserAssociation = browserAttributes => ({
association: 'browser',
attributes: browserAttributes
});
/**
* @param {string[]} userAttributes - User attributes
* @returns {{association: string, attributes: string[]}}
*/
const userAssociation = userAttributes => ({
association: 'tester',
attributes: userAttributes
});
// TestPlanRun
/**
* @param {object} options
* @param {number} options.id - id of TestPlanRun to be retrieved
* @param {string[]} options.testPlanRunAttributes - TestPlanRun attributes to be returned in the result
* @param {string[]} options.nestedTestPlanRunAttributes - TestPlanRun attributes associated to the TestPlanReport model to be returned
* @param {string[]} options.testPlanReportAttributes - TestPlanReport attributes to be returned in the result
* @param {string[]} options.testPlanVersionAttributes - TestPlanVersion attributes to be returned in the result
* @param {string[]} options.testPlanAttributes - TestPlan attributes to be returned in the result
* @param {string[]} options.atAttributes - AT attributes to be returned in the result
* @param {string[]} options.browserAttributes - Browser attributes to be returned in the result
* @param {string[]} options.userAttributes - User attributes to be returned in the result
* @param {*} options.transaction - Sequelize transaction
* @returns {Promise<*>}
*/
const getTestPlanRunById = async ({
id,
testPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
nestedTestPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
testPlanReportAttributes = TEST_PLAN_REPORT_ATTRIBUTES,
testPlanVersionAttributes = TEST_PLAN_VERSION_ATTRIBUTES,
testPlanAttributes = TEST_PLAN_ATTRIBUTES,
atAttributes = AT_ATTRIBUTES,
browserAttributes = BROWSER_ATTRIBUTES,
userAttributes = USER_ATTRIBUTES,
transaction
}) => {
return ModelService.getById(TestPlanRun, {
id,
attributes: testPlanRunAttributes,
include: [
testPlanReportAssociation(
testPlanReportAttributes,
nestedTestPlanRunAttributes,
testPlanVersionAttributes,
testPlanAttributes,
atAttributes,
browserAttributes,
userAttributes
),
userAssociation(userAttributes)
],
transaction
});
};
/**
* @param {object} options
* @param {object} options.where - use this define conditions to be passed to Sequelize's where clause
* @param {string[]} options.testPlanRunAttributes - TestPlanRun attributes to be returned in the result
* @param {string[]} options.nestedTestPlanRunAttributes - TestPlanRun attributes associated to the TestPlanReport model to be returned
* @param {string[]} options.testPlanReportAttributes - TestPlanReport attributes to be returned in the result
* @param {string[]} options.testPlanVersionAttributes - TestPlanVersion attributes to be returned in the result
* @param {string[]} options.testPlanAttributes - TestPlan attributes to be returned in the result
* @param {string[]} options.atAttributes - AT attributes to be returned in the result
* @param {string[]} options.browserAttributes - Browser attributes to be returned in the result
* @param {string[]} options.userAttributes - User attributes to be returned in the result
* @param {object} options.pagination - pagination options for query
* @param {number} options.pagination.page - page to be queried in the pagination result (affected by {@param pagination.enablePagination})
* @param {number} options.pagination.limit - amount of results to be returned per page (affected by {@param pagination.enablePagination})
* @param {string[][]} options.pagination.order- expects a Sequelize structured input dataset for sorting the Sequelize Model results (NOT affected by {@param pagination.enablePagination}). See {@link https://sequelize.org/v5/manual/querying.html#ordering} and {@example [ [ 'username', 'DESC' ], [..., ...], ... ]}
* @param {boolean} options.pagination.enablePagination - use to enable pagination for a query result as well useful values. Data for all items matching query if not enabled
* @param {*} options.transaction - Sequelize transaction
* @returns {Promise<*>}
*/
const getTestPlanRuns = async ({
// search, // Nothing to search
where = {},
testPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
nestedTestPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
testPlanReportAttributes = TEST_PLAN_REPORT_ATTRIBUTES,
testPlanVersionAttributes = TEST_PLAN_VERSION_ATTRIBUTES,
testPlanAttributes = TEST_PLAN_ATTRIBUTES,
atAttributes = AT_ATTRIBUTES,
browserAttributes = BROWSER_ATTRIBUTES,
userAttributes = USER_ATTRIBUTES,
pagination = {},
transaction
}) => {
// search and filtering options
return ModelService.get(TestPlanRun, {
where,
attributes: testPlanRunAttributes,
include: [
testPlanReportAssociation(
testPlanReportAttributes,
nestedTestPlanRunAttributes,
testPlanVersionAttributes,
testPlanAttributes,
atAttributes,
browserAttributes,
userAttributes
),
userAssociation(userAttributes)
],
pagination,
transaction
});
};
/**
* @param {object} options
* @param {object} options.values - values to be used to create the TestPlanRun
* @param {string} options.values.automationService
* @param {string[]} options.testPlanRunAttributes - TestPlanRun attributes to be returned in the result
* @param {string[]} options.nestedTestPlanRunAttributes - TestPlanRun attributes associated to the TestPlanReport model to be returned
* @param {string[]} options.testPlanReportAttributes - TestPlanReport attributes to be returned in the result
* @param {string[]} options.testPlanVersionAttributes - TestPlanVersion attributes to be returned in the result
* @param {string[]} options.testPlanAttributes - TestPlan attributes to be returned in the result
* @param {string[]} options.atAttributes - AT attributes to be returned in the result
* @param {string[]} options.browserAttributes - Browser attributes to be returned in the result
* @param {string[]} options.userAttributes - User attributes to be returned in the result
* @param {*} options.transaction - Sequelize transaction
* @returns {Promise<*>}
*/
const createTestPlanRun = async ({
values: {
testerUserId,
testPlanReportId,
testResults = [],
isAutomated = false,
automationService = null
},
testPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
nestedTestPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
testPlanReportAttributes = TEST_PLAN_REPORT_ATTRIBUTES,
testPlanVersionAttributes = TEST_PLAN_VERSION_ATTRIBUTES,
testPlanAttributes = TEST_PLAN_ATTRIBUTES,
atAttributes = AT_ATTRIBUTES,
browserAttributes = BROWSER_ATTRIBUTES,
userAttributes = USER_ATTRIBUTES,
transaction
}) => {
// shouldn't have duplicate entries for a tester unless it's automated
if (!isAutomated) {
const existingTestPlanRuns = await getTestPlanRuns({
where: {
testerUserId,
testPlanReportId
},
testPlanRunAttributes,
nestedTestPlanRunAttributes,
testPlanReportAttributes,
testPlanVersionAttributes,
testPlanAttributes,
atAttributes,
browserAttributes,
userAttributes,
transaction
});
if (existingTestPlanRuns.length) return existingTestPlanRuns[0];
}
const testPlanRunResult = await ModelService.create(TestPlanRun, {
values: {
testerUserId,
testPlanReportId,
testResults,
initiatedByAutomation: isAutomated,
automationService
},
transaction
});
const { id } = testPlanRunResult;
// to ensure the structure being returned matches what we expect for simple queries and can be controlled
return ModelService.getById(TestPlanRun, {
id,
attributes: testPlanRunAttributes,
include: [
testPlanReportAssociation(
testPlanReportAttributes,
nestedTestPlanRunAttributes,
testPlanVersionAttributes,
testPlanAttributes,
atAttributes,
browserAttributes,
userAttributes
),
userAssociation(userAttributes)
],
transaction
});
};
/**
* @param {object} options
* @param {number} options.id - id of the TestPlanRun record to be updated
* @param {object} options.values - values to be used to update columns for the record being referenced for {@param id}
* @param {string[]} options.testPlanRunAttributes - TestPlanRun attributes to be returned in the result
* @param {string[]} options.nestedTestPlanRunAttributes - TestPlanRun attributes associated to the TestPlanReport model to be returned
* @param {string[]} options.testPlanReportAttributes - TestPlanReport attributes to be returned in the result
* @param {string[]} options.testPlanVersionAttributes - TestPlanVersion attributes to be returned in the result
* @param {string[]} options.testPlanAttributes - TestPlan attributes to be returned in the result
* @param {string[]} options.atAttributes - AT attributes to be returned in the result
* @param {string[]} options.browserAttributes - Browser attributes to be returned in the result
* @param {string[]} options.userAttributes - User attributes to be returned in the result
* @param {*} options.transaction - Sequelize transaction
* @returns {Promise<*>}
*/
const updateTestPlanRunById = async ({
id,
values: { testerUserId, testResults, isPrimary },
testPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
nestedTestPlanRunAttributes = TEST_PLAN_RUN_ATTRIBUTES,
testPlanReportAttributes = TEST_PLAN_REPORT_ATTRIBUTES,
testPlanVersionAttributes = TEST_PLAN_VERSION_ATTRIBUTES,
testPlanAttributes = TEST_PLAN_ATTRIBUTES,
atAttributes = AT_ATTRIBUTES,
browserAttributes = BROWSER_ATTRIBUTES,
userAttributes = USER_ATTRIBUTES,
transaction
}) => {
await ModelService.update(TestPlanRun, {
where: { id },
values: { testResults, testerUserId, isPrimary },
transaction
});
return ModelService.getById(TestPlanRun, {
id,
attributes: testPlanRunAttributes,
include: [
testPlanReportAssociation(
testPlanReportAttributes,
nestedTestPlanRunAttributes,
testPlanVersionAttributes,
testPlanAttributes,
atAttributes,
browserAttributes,
userAttributes
),
userAssociation(userAttributes)
],
transaction
});
};
/**
* @param {object} options
* @param {number} options.id - id of the TestPlanRun record to be removed
* @param {boolean} options.truncate - Sequelize specific deletion options that could be passed
* @param {*} options.transaction - Sequelize transaction
* @returns {Promise<boolean>}
*/
const removeTestPlanRunById = async ({ id, truncate = false, transaction }) => {
return ModelService.removeById(TestPlanRun, { id, truncate, transaction });
};
/**
* @param {number} where - conditions to be passed to Sequelize's where clause
* @param {boolean} options.truncate - Sequelize specific deletion options that could be passed
* @param {*} options.transaction - Sequelize transaction
* @returns {Promise<boolean>}
*/
const removeTestPlanRunByQuery = async ({
where: { testerUserId, testPlanReportId },
truncate = false,
transaction
}) => {
const deleteWhere =
testerUserId === undefined
? { testPlanReportId }
: { testPlanReportId, testerUserId };
return ModelService.removeByQuery(TestPlanRun, {
where: deleteWhere,
truncate,
transaction
});
};
/**
* @param {object} options
* @param {number} options.where - conditions to be passed to Sequelize's where clause
* @param {*} options.transaction - Sequelize transaction
* @returns {Promise<boolean>}
*/
const removeTestPlanRunResultsByQuery = async ({
where: { testerUserId, testPlanReportId },
transaction
}) => {
return ModelService.update(TestPlanRun, {
where: { testerUserId, testPlanReportId },
values: { testResults: [] },
transaction
});
};
/**
* Allows you to check if a given AtVersion is in use by any test results.
* @param {number} atVersionId - An AT version ID
* @param {object} options
* @param {*} options.transaction - Sequelize transaction
* @returns
*/
const getTestResultsUsingAtVersion = async (atVersionId, { transaction }) => {
const [results] = await sequelize.query(
`
WITH "testPlanRunTestResult" AS (
SELECT
id,
jsonb_array_elements("testResults") AS "testResult"
FROM "TestPlanRun"
)
SELECT
id AS "testPlanRunId",
"testResult" ->> 'id' AS "testResultId"
FROM "testPlanRunTestResult"
WHERE "testResult" ->> 'atVersionId' = ?
`,
{ replacements: [atVersionId], transaction }
);
return results;
};
module.exports = {
// TestPlanRun
getTestPlanRunById,
getTestPlanRuns,
createTestPlanRun,
updateTestPlanRunById,
removeTestPlanRunById,
removeTestPlanRunByQuery,
removeTestPlanRunResultsByQuery,
// Custom functions
getTestResultsUsingAtVersion
};