From daf9abce66091bfa5ccd49389f07c4fb48aea4e1 Mon Sep 17 00:00:00 2001 From: shunyi Date: Wed, 31 Aug 2016 22:00:42 +0800 Subject: [PATCH] [#101] Add unit test for YahooDict of vocabulary 'style' --- zdict/tests/dictionaries/test_yahoo.py | 60 ++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/zdict/tests/dictionaries/test_yahoo.py b/zdict/tests/dictionaries/test_yahoo.py index 6f0311b0..d999975a 100644 --- a/zdict/tests/dictionaries/test_yahoo.py +++ b/zdict/tests/dictionaries/test_yahoo.py @@ -1,14 +1,66 @@ +from pytest import raises +from unittest.mock import Mock, patch + +from zdict.exceptions import NotFoundError from zdict.dictionaries.yahoo import YahooDict from zdict.zdict import get_args class TestyDict: - def setup_method(self, method): - self.dict = YahooDict(get_args()) + @classmethod + def setup_class(cls): + cls.dict = YahooDict(get_args()) + cls.word = 'style' + cls.record = cls.dict.query(cls.word) + + @classmethod + def teardown_class(cls): + del cls.dict + del cls.word + del cls.record - def teardown_method(self, method): - del self.dict + def test_provider(self): + assert self.dict.provider == 'yahoo' + + def test_title(self): + assert self.dict.title == 'Yahoo Dictionary' def test__get_url(self): url = 'https://tw.dictionary.search.yahoo.com/search?p=test' assert url == self.dict._get_url('test') + + def test_show(self): + # god bless this method, hope that it do not raise any exception + self.dict.args.verbose = False + self.dict.show(self.record) + + def test_show_verbose(self): + # god bless this method, hope that it do not raise any exception + self.dict.args.verbose = True + self.dict.show(self.record) + + @patch('zdict.dictionaries.yahoo.Record') + def test_query_normal(self, Record): + self.dict.args.verbose = False + self.dict.query(self.word) + Record.assert_called_with( + word=self.word, + content=self.record.content, + source='yahoo', + ) + + @patch('zdict.dictionaries.yahoo.Record') + def test_query_verbose(self, Record): + self.dict.args.verbose = True + self.dict.query(self.word) + Record.assert_called_with( + word=self.word, + content=self.record.content, + source='yahoo', + ) + + def test_query_not_found(self): + self.dict._get_raw = Mock(return_value='{"data": []}') + with raises(NotFoundError): + self.dict.query(self.word) + self.dict._get_raw.assert_called_with(self.word)