-
Notifications
You must be signed in to change notification settings - Fork 21
/
mysql-query.js
91 lines (79 loc) · 2.96 KB
/
mysql-query.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
/**
* MySQL Query
*
* Parameters
* query (JS) : MySQL query
* returnVariableName (JS) [Optional] : Name of return data variable.
* Uses queryResults if not defined
* Sequelize (NPM) : sequelize@latest
* mysql2 (NPM) : mysql2@latest
*
* Returns
* queryResults or returnVariableName if defined
*
* Return first row of results as individual test level variables with names matching the associated column(s)
*
* For example, if the recordset contains data with the columns ["firstName", "lastName"],
*
* Two variables will be created: "firstName" and "lastName" with values that match the first set of data generated
* firstName === queryResults[0].firstName
* lastName === queryResults[0].lastName
*
* Disclaimer
* This Custom Action is provided "AS IS". It is for instructional purposes only and is not officially supported by Testim
*
* Base Step
* CLI Action
*
* Installation
* Create a new "CLI action" step
* Name it "MySQL Query"
* Create parameters
* query (JS)
* returnVariableName (JS)
* Sequelize (NPM) and set its value = sequelize @ latest
* mysql2 (NPM) and set its value = mysql2 @ latest
* Set the new custom action's function body to this javascript
* Set connection information
* Exit the step editor
* Share the step if not already done so
* Save the test
* Bob's your uncle
*/
var dbName = "testdata";
var userName = "barry";
var password = "P@$$w0rdMS";
var host = "localhost";
var port = 3306;
/* if returnVariableName is defined then use it else use 'queryResults' as the return variable
*/
var return_variable_name = (typeof returnVariableName !== 'undefined' && returnVariableName !== null) ? returnVariableName : 'queryResults';
const sequelize = new Sequelize(dbName, userName, password, {
select: "mysql",
dialect: 'mysql',
host,
port,
});
return sequelize
.query(query, {
type: Sequelize.QueryTypes.SELECT,
})
.then(myTableRows => {
const result = myTableRows && JSON.stringify(myTableRows);
console.log("myTableRows", JSON.stringify(myTableRows));
console.log("Query result", result);
exportsTest[return_variable_name] = result;
// Take an index and store generatedData[0]'s values as naked top level variables
//
let naked_variable_index = 0;
function storeFirstAsGlobalNakedVariables(value) {
let variableName = value;
let variableValue = myTableRows[naked_variable_index][variableName];
exportsTest[variableName] = variableValue;
console.log(variableName + " = " + variableValue);
}
Object.keys(myTableRows[naked_variable_index]).forEach(storeFirstAsGlobalNakedVariables);
if (!myTableRows) {
return Promise.reject(new Error("Failed to find raw"));
}
});