-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
263 lines (234 loc) · 9.05 KB
/
test.ts
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
import * as tmapi from './api';
// turn off SSL authorization
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
// authentificaton data
let USER = 'user';
let PASSWORD = ''; // default
let HOST = '';//'http://localhost:7007/tmapi/v1';
function createApi(apiType : string) {
let api = new tmapi[apiType];
if (USER != '')
api.username = USER;
if (PASSWORD != '')
api.password = PASSWORD;
if (HOST != '')
api.basePath = HOST;
return api;
}
let texts : string[] = [
'Elvis Presley was born in Tupelo, Mississippi.',
'Elvis Presley was an American singer'];
// base64 encoded texts
let testDocs = new tmapi.Documents();
testDocs.files = new Array<tmapi.Document>();
texts.forEach(element => {
let document = new tmapi.Document;
document.content = Buffer.from(element).toString('base64');
testDocs.files!.push(document);
});
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
var assert = require('assert');
// TEST SERVER
describe('TestServer', () => {
// test server information
it('TestServerInfo', async function() {
let server = createApi('ServerApi');
let gsi = await server.getServerInfo();
assert(gsi.body.languages!.length > 0);
assert(gsi.body.operations!.length > 0);
});
// test limits
it('TestLimits', async function() {
let server = createApi('LimitsApi');
let result = await server.getLimits();
let limits : any = result.body.limits;
assert(isEmpty(limits));
});
});
// TEST OPERATIONS
describe('TestOperations', function() {
this.timeout(100000);
let operations = createApi('OperationsApi');
// test language detection
it('TestLanguageDetection', async function() {
let result = await operations.getLanguages(texts[0]);
let documents : any = result.body.documents;
assert(documents.length == 1);
assert(documents[0].language == "English");
});
it('TestDocumentsLanguageDetection', async function() {
let result = await operations.getDocumentsLanguages(testDocs);
let documents : any = result.body.documents;
let docsCount = testDocs.files!.length;
assert(documents.length == docsCount);
for (let i = 0; i < docsCount; ++i)
assert(documents[i].language == "English");
});
// test tokens
it('TestTokens', async function() {
let result = await operations.extractTokens(texts[0]);
let documents : any = result.body.documents;
assert(documents.length == 1);
assert(documents[0].sentences.length > 0);
assert(documents[0].sentences[0].tokens.length > 0);
});
it('TestDocumentTokens', async function() {
let result = await operations.extractDocumentsTokens(testDocs);
let documents : any = result.body.documents;
assert(documents.length == testDocs.files!.length);
assert(documents[0].sentences.length > 0);
assert(documents[0].sentences[0].tokens.length > 0);
});
// operations
function testOperation(testName : string, operation, property, text : string) {
it(testName, async function() {
let result = await operations[operation](text);
let documents : any = result.body.documents;
assert(documents.length == 1);
assert(documents[0][property].length > 0);
});
}
function testDocumentsOperation(testName: string, operation, property, textDocs : tmapi.Documents) {
it(testName, async function() {
let result = await operations[operation](textDocs);
let documents : any = result.body.documents;
let docsCount = testDocs.files!.length;
assert(documents.length == docsCount);
for (let i = 0; i < docsCount; ++i)
assert(documents[i][property].length > 0);
});
}
// keywords
testOperation('TestKeywords', 'extractKeywords', 'keywords', texts[0]);
testDocumentsOperation('TestDocumentsKeywords', 'extractDocumentsKeywords', 'keywords', testDocs);
// entities
testOperation('TestEntities', 'extractEntities', 'entities', texts[0]);
testDocumentsOperation('TestDocumentsEntities', 'extractDocumentsEntities', 'entities', testDocs);
// sentiments
let sentimentsTexts = [
'New menu is good but the place in Toronto is dirty.',
'Pretty good food on average'];
testOperation('TestSentiments', 'extractSentiments', 'sentiments', sentimentsTexts[0]);
// documents sentiments
{
let docs = new tmapi.Documents();
docs.files = new Array<tmapi.Document>();
for (let text of sentimentsTexts) {
let base64Text = Buffer.from(text).toString('base64');
let doc = new tmapi.Document;
doc.content = Buffer.from(text).toString('base64');
docs.files!.push(doc);
}
testDocumentsOperation('TestDocumentsSentiments', 'extractDocumentsSentiments', 'sentiments', docs);
}
// facts
testOperation('TestFacts', 'extractFacts', 'facts', texts[0]);
testDocumentsOperation('TestDocumentsFacts', 'extractDocumentsFacts', 'facts', testDocs);
});
// TEST TASKS
describe('TestTasks', function() {
this.timeout(100000);
let tasks = createApi('TasksApi');
// test task information
it('TestTaskInfo', async function() {
let ct = await tasks.createTask(['tokens', 'entities'], testDocs, 1);
let taskId = ct.body['taskId'];
// check all tasks
let gti = await tasks.getTasksInfo([]);
let allTasks = gti.body.tasks!;
assert(allTasks.length > 0);
let bFound = false;
for (let task of allTasks) {
if (task.id == taskId) {
bFound = true;
break;
}
}
assert(bFound);
// check concrete task
gti = await tasks.getTasksInfo([taskId]);
let task = gti.body.tasks![0];
assert(taskId == task.id);
});
// test delete task
it('TestDeleteTask', async function() {
// create task
let ct = await tasks.createTask(['tokens'], testDocs, 1);
let id = ct.body['taskId'];
let gti = await tasks.getTasksInfo([id]);
assert(gti.body.tasks!.length > 0);
// delete task
await tasks.deleteTasks([id]);
// check task was deleted
gti = await tasks.getTasksInfo([id]);
assert(gti.body.tasks == undefined);
});
// test synchronous task
it('TestSyncTask', async function() {
// create task
let ct = await tasks.createTask(['entities'], testDocs, 0);
let result = ct.body;
assert(result.documents.length == testDocs.files!.length);
assert(result.documents[0].entities.length > 0);
});
// test asynchronous task
it('TestAsyncTask', async function() {
// create task
let ct = await tasks.createTask(['entities'], testDocs, 1);
let id = ct.body['taskId'];
await sleep(5000)
while (true) {
let gti = await tasks.getTasksInfo([id]);
let task = gti.body.tasks![0];
if (task.done == 100) {
break;
}
}
let gtr = await tasks.getTaskResult(id, ['entities']);
let result = gtr.body;
assert(result.documents.length == testDocs.files!.length);
assert(result.documents[0].entities.length > 0);
});
// test multiple task
it('TestMultipleTask', async function() {
let operations = ['tokens', 'keywords'];
let taskIds : string[] = [];
// create tasks
for (let op of operations) {
let ct = await tasks.createTask([op], testDocs, 1);
let id = ct.body['taskId'];
taskIds.push(id);
}
// wait till finished
await sleep(5000)
while (true) {
let gti = await tasks.getTasksInfo(taskIds);
let allFinished = true;
for (let task of gti.body.tasks!) {
if (task.done! < 100) {
allFinished = false;
break;
}
}
if (allFinished)
break;
await sleep(1000);
}
// check results
for (let i = 0; i < taskIds.length; i++) {
let gtr = await tasks.getTaskResult(taskIds[i], [operations[i]]);
assert(gtr.body.documents.length > 0);
}
// delete tasks
await tasks.deleteTasks(taskIds);
let gti = await tasks.getTasksInfo(taskIds);
assert(gti.body.tasks == undefined);
});
});