-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_all.py
47 lines (40 loc) · 1.57 KB
/
test_all.py
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
import unittest.mock
from main import handler
from translation import translate
def test_main():
context = unittest.mock.Mock()
context.token = {'access_token': 'abcde'}
event = {
'version': 1,
'session': {},
'request': {
'command': 'переведи слово katze с немецкого на английский',
'nlu': {
'intents': {
'translate_full': {
'slots': {
'phrase': {'value': 'katze'},
'from': {'value': 'de'},
'to': {'value': 'en'},
}
}
}
}
}
}
with unittest.mock.patch('main.translate') as mock_translate:
mock_translate.return_value = None, 'cat'
resp = handler(event=event, context=context)
mock_translate.assert_called_with(token='abcde', lang_from='de', lang_to='en', text='katze')
assert resp['response']['text'] == 'cat'
def test_translate():
with unittest.mock.patch('translation.requests.post') as mock_post:
mock_post.return_value.status_code = 200
mock_post.return_value.json = lambda: {'translations': [{'text': 'cat'}]}
result = translate(lang_from='de', lang_to='en', token='abcde', text='katze')
mock_post.assert_called()
assert result == (None, 'cat')
def test_translate_without_token():
error, result = translate(lang_from='de', lang_to='en', token=None, text='katze')
assert error
assert not result