Skip to content

Commit 809b985

Browse files
committed
Added tests for russian UserDict
1 parent d921e05 commit 809b985

File tree

1 file changed

+232
-0
lines changed

1 file changed

+232
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
'use strict';
2+
3+
const assert = require('chai').assert;
4+
const Response = require('node-fetch').Response;
5+
const CommunicatorMock = require('../communicator-mock');
6+
const UserDict = require('../../../src/russian/user-dict');
7+
const MorpherError = require('../../../src/morpher-error');
8+
const CorrectionEntry = require('../../../src/russian/correction-entry');
9+
const CorrectionForms = require('../../../src/russian/correction-forms');
10+
11+
describe('Russian UserDict', function() {
12+
13+
describe('#addOrUpdate()', async function() {
14+
15+
const communicatorMock = new CommunicatorMock();
16+
17+
const userDict = new UserDict(communicatorMock);
18+
19+
it('should use the correct parameters', async function() {
20+
21+
const response = true;
22+
23+
communicatorMock.response = new Response(
24+
JSON.stringify(response),
25+
{status: 200},
26+
);
27+
28+
await userDict.addOrUpdate({
29+
singular: {
30+
nominative: 'Кошка',
31+
genitive: 'Пантеру',
32+
},
33+
plural: {
34+
nominative: 'Кошки',
35+
genitive: 'Пантер',
36+
},
37+
});
38+
39+
assert.equal(
40+
userDict.communicator.lastPath,
41+
'/russian/userdict',
42+
);
43+
assert.equal(
44+
userDict.communicator.lastParams.get('И'),
45+
'Кошка',
46+
);
47+
assert.equal(
48+
userDict.communicator.lastParams.get('Р'),
49+
'Пантеру',
50+
);
51+
assert.equal(
52+
userDict.communicator.lastParams.get('М_И'),
53+
'Кошки',
54+
);
55+
assert.equal(
56+
userDict.communicator.lastParams.get('М_Р'),
57+
'Пантер',
58+
);
59+
assert.equal(
60+
userDict.communicator.lastHttpMethod,
61+
CommunicatorMock.METHOD_POST,
62+
);
63+
64+
});
65+
66+
it('should return true', async function() {
67+
68+
communicatorMock.response = new Response(
69+
'',
70+
{status: 200},
71+
);
72+
73+
const entry = {
74+
singular: {
75+
nominative: 'Кошка',
76+
dative: 'Пантере',
77+
},
78+
};
79+
80+
const result = await userDict.addOrUpdate(entry);
81+
82+
assert.equal(result, true);
83+
});
84+
85+
it('should throw MorpherError', async function() {
86+
87+
const response = {
88+
'code': 6,
89+
'message': 'Не указан обязательный параметр: И.',
90+
};
91+
92+
communicatorMock.response = new Response(
93+
JSON.stringify(response),
94+
{status: 496},
95+
);
96+
97+
try {
98+
await userDict.addOrUpdate();
99+
} catch (err) {
100+
assert.instanceOf(err, MorpherError);
101+
}
102+
103+
});
104+
105+
});
106+
107+
describe('#getAll()', async function() {
108+
109+
const communicatorMock = new CommunicatorMock();
110+
111+
const userDict = new UserDict(communicatorMock);
112+
113+
it('should return an array of CorrectionEntry', async function() {
114+
115+
const response = [
116+
{
117+
singular: {
118+
'И': 'Кошка',
119+
'Д': 'Пантере',
120+
}
121+
},
122+
];
123+
124+
communicatorMock.response = new Response(
125+
JSON.stringify(response),
126+
{status: 200},
127+
);
128+
129+
const result = await userDict.getAll();
130+
131+
assert.isArray(result);
132+
133+
result.forEach(ce => {
134+
assert.instanceOf(ce, CorrectionEntry);
135+
assert.instanceOf(ce.singular, CorrectionForms);
136+
});
137+
138+
assert.isUndefined(result.plural);
139+
});
140+
141+
it('should throw MorpherError', async function() {
142+
143+
const response = {
144+
'code': 9,
145+
'message': 'Данный токен не найден.',
146+
};
147+
148+
communicatorMock.response = new Response(
149+
JSON.stringify(response),
150+
{status: 496},
151+
);
152+
153+
try {
154+
await userDict.getAll();
155+
} catch (err) {
156+
assert.instanceOf(err, MorpherError);
157+
}
158+
159+
});
160+
161+
});
162+
163+
describe('#remove()', async function() {
164+
165+
const communicatorMock = new CommunicatorMock();
166+
167+
const userDict = new UserDict(communicatorMock);
168+
169+
it('should use the correct parameters', async function() {
170+
171+
const response = true;
172+
173+
communicatorMock.response = new Response(
174+
JSON.stringify(response),
175+
{status: 200},
176+
);
177+
178+
await userDict.remove('Кошка');
179+
180+
assert.equal(
181+
userDict.communicator.lastPath,
182+
'/russian/userdict',
183+
);
184+
assert.equal(
185+
userDict.communicator.lastParams.get('s'),
186+
'Кошка',
187+
);
188+
assert.equal(
189+
userDict.communicator.lastHttpMethod,
190+
CommunicatorMock.METHOD_DELETE,
191+
);
192+
193+
});
194+
195+
it('should return true', async function() {
196+
197+
const response = true;
198+
199+
communicatorMock.response = new Response(
200+
JSON.stringify(response),
201+
{status: 200},
202+
);
203+
204+
const result = await userDict.remove('Кошка');
205+
206+
assert.equal(result, true);
207+
208+
});
209+
210+
it('should throw MorpherError', async function() {
211+
212+
const response = {
213+
'code': 6,
214+
'message': 'Не указан обязательный параметр: s.',
215+
};
216+
217+
communicatorMock.response = new Response(
218+
JSON.stringify(response),
219+
{status: 496},
220+
);
221+
222+
try {
223+
await userDict.addOrUpdate();
224+
} catch (err) {
225+
assert.instanceOf(err, MorpherError);
226+
}
227+
228+
});
229+
230+
});
231+
232+
});

0 commit comments

Comments
 (0)