forked from koorchik/node-mole-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoTester.js
More file actions
158 lines (129 loc) · 6.4 KB
/
AutoTester.js
File metadata and controls
158 lines (129 loc) · 6.4 KB
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
const { assert } = require('chai');
const X = require('./X');
const {
functionsToExpose,
lastResultsStore
} = require('./AutoTester/exposeFunctions/functionsToExpose');
const positiveTestsData = require('./AutoTester/exposeFunctions/positiveTestsData');
const negativeTestsData = require('./AutoTester/exposeFunctions/negativeTestsData');
class AutoTester {
constructor({ simpleClient, proxifiedClient, server }) {
if (!simpleClient) throw new Error('"simpleClient" required');
if (!proxifiedClient) throw new Error('"proxifiedClient" required');
if (!server) throw new Error('"server" required');
this.simpleClient = simpleClient;
this.proxifiedClient = proxifiedClient;
this.server = server;
}
async runAllTests() {
await this._exposeServerMethods();
// POSITIVE TESTS
console.log('Run simple positive tests for simpleClient:');
await this._runSimplePositiveTests(this.simpleClient, positiveTestsData);
console.log('Run simple positive notification tests for simpleClient');
await this._runSimplePositiveNotificationTests(this.simpleClient, positiveTestsData);
console.log('Run positive batch tests for simpleClient:');
await this._runBatchTests(this.simpleClient, positiveTestsData);
console.log('Run simple positive tests for proxifiedClient:');
await this._runSimplePositiveTests(this.proxifiedClient, positiveTestsData);
console.log('Run simple positive notification tests for proxifiedClient');
await this._runSimplePositiveNotificationTests(this.proxifiedClient, positiveTestsData);
console.log('Run proxy positive tests for proxifiedClient:');
await this._runProxyPositiveTests(this.proxifiedClient, positiveTestsData);
console.log('Run proxy positive notification tests for proxifiedClient');
await this._runProxyPositiveNotificationTests(this.proxifiedClient, positiveTestsData);
// NEGATIVE TESTS
console.log('Run simple negative tests for simpleClient:');
await this._runSimpleNegativeTests(this.simpleClient, negativeTestsData);
console.log('Run simple negative tests for proxifiedClient:');
await this._runSimpleNegativeTests(this.proxifiedClient, negativeTestsData);
console.log('Run proxy negative tests for proxifiedClient:');
await this._runProxyNegativeTests(this.proxifiedClient, negativeTestsData);
}
async _exposeServerMethods() {
this.server.expose(functionsToExpose);
await this.server.run();
}
async _runSimplePositiveTests(client, positiveTestsData) {
for (const { callMethod, args, expectedResult } of positiveTestsData) {
console.log(`Positive test: calling ${callMethod}`);
const gotResult = await client.callMethod(callMethod, args);
assert.deepEqual(gotResult, expectedResult);
}
console.log('\n');
}
async _runSimplePositiveNotificationTests(client, positiveTestsData) {
for (const { callMethod, args, expectedResult } of positiveTestsData) {
console.log(`Positive test: notifying ${callMethod}`);
const gotResult = await client.notify(callMethod, args);
assert.equal(gotResult, true);
assert.deepEqual(lastResultsStore[callMethod], expectedResult);
}
console.log('\n');
}
async _runBatchTests(client, positiveTestsData) {
const requestData = [];
const expectedResults = [];
for (const { callMethod, args, expectedResult } of positiveTestsData) {
requestData.push([callMethod, args]);
expectedResults.push({
success: true,
result: expectedResult
});
}
console.log(`Positive test: calling batch`);
const gotResults = await client.runBatch(requestData);
assert.deepEqual(gotResults, expectedResults);
console.log('\n');
}
async _runProxyPositiveTests(client, positiveTestsData) {
for (const { callMethod, args, expectedResult } of positiveTestsData) {
console.log(`Positive test via proxy: calling ${callMethod}`);
const gotResult = await client.callMethod[callMethod](...args);
assert.deepEqual(gotResult, expectedResult);
}
console.log('\n');
}
async _runProxyPositiveNotificationTests(client, positiveTestsData) {
for (const { callMethod, args, expectedResult } of positiveTestsData) {
console.log(`Positive test via proxy: notifying ${callMethod}`);
const gotResult = await client.notify[callMethod](...args);
assert.equal(gotResult, true);
assert.deepEqual(lastResultsStore[callMethod], expectedResult);
}
console.log('\n');
}
async _runSimpleNegativeTests(client, negativeTestsData) {
for (const { callMethod, args, expectedError, expectedClass } of negativeTestsData) {
try {
console.log(`Negative test: calling ${callMethod}`);
await client.callMethod(callMethod, args);
throw new Error(
`Method "${callMethod}" should fail but was executed without any error`
);
} catch (gotError) {
assert.instanceOf(gotError, expectedClass, 'check error class');
assert.deepEqual(gotError.message, expectedError.message, 'check error message');
assert.deepEqual(gotError.code, expectedError.code, 'check error code');
}
}
console.log('\n');
}
async _runProxyNegativeTests(client, negativeTestsData) {
for (const { callMethod, args, expectedError, expectedClass } of negativeTestsData) {
try {
console.log(`Negative test via proxy: calling ${callMethod}`);
await client[callMethod](...args);
throw new Error(
`Method "${callMethod}" should fail but was executed without any error`
);
} catch (gotError) {
assert.instanceOf(gotError, expectedClass, 'check error class');
assert.deepEqual(gotError.message, expectedError.message, 'check error message');
assert.deepEqual(gotError.code, expectedError.code, 'check error code');
}
}
console.log('\n');
}
}
module.exports = AutoTester;