From 7bbed104c512f680a0049739deb9a29337ffdf42 Mon Sep 17 00:00:00 2001 From: Chiu-Hsiang Hsu Date: Sat, 26 Aug 2017 20:55:57 +0800 Subject: [PATCH] Add test case for YandexDict --- zdict/tests/dictionaries/test_yandex.py | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 zdict/tests/dictionaries/test_yandex.py diff --git a/zdict/tests/dictionaries/test_yandex.py b/zdict/tests/dictionaries/test_yandex.py new file mode 100644 index 00000000..bb86e86b --- /dev/null +++ b/zdict/tests/dictionaries/test_yandex.py @@ -0,0 +1,51 @@ +from pytest import raises +from unittest.mock import Mock, patch + +from zdict.dictionaries.yandex import YandexDict, API_KEY +from zdict.exceptions import NotFoundError, QueryError +from zdict.models import Record +from zdict.zdict import get_args + + +class TestYandexDict: + def setup_method(self, method): + self.dict = YandexDict(get_args()) + + def teardown_method(self, method): + del self.dict + + def test__get_url(self): + url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?' \ + 'key={}&text=дом&lang=ru-en' + assert url.format(API_KEY) == self.dict._get_url('дом') + + def test_provider(self): + assert self.dict.provider == 'yandex' + + def test_query_timeout(self): + self.dict._get_raw = Mock(side_effect=QueryError('дом', 404)) + + with raises(NotFoundError): + self.dict.query('дом') + + self.dict._get_raw.assert_called_with('дом') + + @patch('zdict.dictionaries.yandex.Record') + def test_query_normal(self, Record): + content = '{"code":200,"lang":"ru-en","text":["house"]}' + self.dict._get_raw = Mock(return_value=content) + self.dict.query('дом') + Record.assert_called_with(word='дом', content=content, source='yandex') + + def test_show(self): + content = ''' + { + "code": 200, + "lang": "ru-en", + "text": ["house"] + } + ''' + r = Record(word='дом', content=content, source=self.dict.provider) + + # god bless this method, hope that it do not raise any exception + self.dict.show(r)