-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveToDB.js
103 lines (91 loc) · 2.13 KB
/
SaveToDB.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
const mysql = require('mysql');
const config = require(__dirname + "/config.json");
class SaveToDB {
constructor() {
this.conn = mysql.createConnection(config.dbCredentials);
this.conn.connect(function(err) {
if (err) {
console.log("Database must be online");
throw err;
}
console.log("MySQL Connected!");
});
};
save(data) {
this.conn.query({
sql: 'INSERT INTO results (test, test_name, cycle, ok, os, browser, mobile, incognito, https) VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?) ',
timeout: 40000, // 40s
values: [
data.id,
data.name,
data.cycle,
data.ok,
data.os,
data.browser,
data.mobile,
data.incognito,
data.https
]
}, function (error, results, fields) {
if (error) {
throw error;
}
var id = results.insertId;
if(id > 0) {
try {
this.saveSteps(id, data.steps);
} catch(e) {
throw e;
}
}
}.bind(this));
};
saveSteps(id, steps) {
for (let i = 0; i < steps.length; i++) {
let step = steps[i];
var status = {
expection : null,
received : null
};
var cb = Object.assign({}, status);
var ch = Object.assign({}, status);
if(step.validated) {
if(step.validated.ch) {
ch = step.validated.ch;
}
if(step.validated.cb) {
cb = step.validated.cb;
}
if(step.validated.st) {
status = step.validated.st;
}
}
this.conn.query({
sql: 'INSERT INTO results_steps (id_rel, url, method, ch, ch_expected, cb, cb_expected, status, status_expected, request, responded, missing, header_id, body_id, complete_header) VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ',
timeout: 40000, // 40s
values: [
id,
step.url,
step.method,
ch.received,
ch.expection,
cb.received,
cb.expection,
status.received,
status.expection,
JSON.stringify(step.requestHeader),
JSON.stringify(step.respondedHeader),
JSON.stringify(step.missingHeaders),
step.headerId,
step.bodyId,
step.allRespondedHeader
]
}, function (error, results, fields) {
if (error) {
throw error;
}
});
}
};
};
exports.SaveToDB = SaveToDB;