forked from MFALHI/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.alchemy_language.v1.js
79 lines (64 loc) · 2.49 KB
/
test.alchemy_language.v1.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
'use strict';
var assert = require('assert');
var watson = require('../lib/index');
var nock = require('nock');
var qs = require('querystring');
describe('alchemy_language', function() {
var noop = function() {};
// Test params
var service = {
api_key: 'foobar',
url: 'http://ibm.com:80/calls',
version: 'v1'
};
var apiPath = '/text/TextGetRankedNamedEntities';
var payload = {
text: 'sample text'
};
before(function() {
nock.disableNetConnect();
});
after(function() {
nock.cleanAll();
});
var alchemy = watson.alchemy_language(service);
var missingParameter = function(err) {
assert.ok((err instanceof Error) && /required parameters/.test(err));
};
describe('entities()', function() {
it('should check missing parameters', function() {
alchemy.entities({}, missingParameter);
alchemy.entities(null, missingParameter);
alchemy.entities(undefined, missingParameter);
alchemy.entities({foo: 'bar'}, missingParameter);
});
it('should generate a valid payload', function() {
var req = alchemy.entities(payload, noop);
assert.equal(req.uri.href, service.url + apiPath + '?apikey=' + service.api_key);
assert.equal(req.method, 'POST');
assert(req.form);
var body = new Buffer(req.body).toString('ascii');
assert.equal(body, qs.stringify({ text: payload.text, outputMode: 'json'}));
});
it('should use sentiment_target if target is specified', function() {
var req = alchemy.sentiment({text: payload.text, target:'bat'}, noop);
var sentimenTargetPath = service.url + '/text/TextGetTargetedSentiment?apikey=' + service.api_key;
assert.equal(req.uri.href, sentimenTargetPath);
assert.equal(req.method, 'POST');
assert(req.form);
var body = new Buffer(req.body).toString('ascii');
var expectedBody = qs.stringify({ text: payload.text,target: 'bat', outputMode: 'json'});
assert.equal(body, expectedBody);
});
it('should use sentiment if target is not specified', function() {
var req = alchemy.sentiment(payload, noop);
var sentimenPath = service.url + '/text/TextGetTextSentiment?apikey=' + service.api_key;
assert.equal(req.uri.href, sentimenPath);
assert.equal(req.method, 'POST');
assert(req.form);
var body = new Buffer(req.body).toString('ascii');
var expectedBody = qs.stringify({ text: payload.text, outputMode: 'json'});
assert.equal(body, expectedBody);
});
});
});