Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #497 from zdict/fix-itaigi-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Zheng committed Sep 7, 2021
2 parents 60a6d34 + 960ae12 commit 179e3c6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Expand Up @@ -255,11 +255,11 @@ jobs:
command: |
. venv/bin/activate
pip install wheel
python setup.py sdist
python setup.py bdist_wheel
python setup.py sdist bdist_wheel
- run:
name: upload to pypi
command: |
. venv/bin/activate
twine check dist/*
twine upload dist/*
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -9,4 +9,4 @@ flake8-ignore =
# F841 - local variable name is assigned to but never used

[bdist_wheel]
python-tag = py36+
python-tag = py36
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -66,6 +66,7 @@ def get_test_req():
keywords="cli, dictionary, framework",
description="The last online dictionary framework you need. (?)",
long_description=open("README.rst", encoding='utf-8').read(),
long_description_content_type="text/x-rst",
download_url="https://github.com/zdict/zdict/archive/v{}.zip".format(
version
),
Expand Down
57 changes: 42 additions & 15 deletions zdict/tests/dictionaries/test_itaigi.py
@@ -1,12 +1,33 @@
import json
import time
from pytest import raises
from unittest.mock import Mock, patch
from unittest.mock import Mock

from zdict.exceptions import NotFoundError
from zdict.zdict import get_args
from zdict.dictionaries.itaigi import iTaigiDict


def _filter_pronounce(d: dict) -> dict:
new_d = {}
for k, v in d.items():
# iTaigiDict sometimes return one item of result
# with same meaning but different pronounce
# which will make this unittest randomly fail.
if k == "pronounce":
continue

if isinstance(v, dict):
new_d[k] = _filter_pronounce(v)
elif isinstance(v, tuple):
new_d[k] = tuple(_filter_pronounce(_) for _ in v)
elif isinstance(v, list):
new_d[k] = list(_filter_pronounce(_) for _ in v)
else:
new_d[k] = v
return new_d


class TestiTaigiDict:
@classmethod
def setup_class(cls):
Expand Down Expand Up @@ -84,43 +105,49 @@ def test_show_verbose(self):
for record in self.verbose_records:
self.dict.show(record)

@patch('zdict.dictionaries.itaigi.Record')
def test_query_normal(self, Record):
def test_query_normal(self):
self.dict.args.verbose = False

query_record = None
for i, word in enumerate(self.words):
while True:
try:
self.dict.query(word)
query_record = self.dict.query(word)
except Exception:
# prevent itaigi API 500 error
time.sleep(5)
continue
else:
Record.assert_called_with(
word=word,
content=self.records[i].content,
source='itaigi',
assert (
_filter_pronounce(
json.loads(query_record.content)
) ==
_filter_pronounce(
json.loads(self.records[i].content)
)
)
break

@patch('zdict.dictionaries.itaigi.Record')
def test_query_verbose(self, Record):
def test_query_verbose(self):
self.dict.args.verbose = True

verbose_query_record = None
for i, word in enumerate(self.words):
while True:
try:
self.dict.query(word)
verbose_query_record = self.dict.query(word)
except Exception:
# prevent itaigi API 500 error
time.sleep(5)
continue
else:
Record.assert_called_with(
word=word,
content=self.verbose_records[i].content,
source='itaigi',
assert (
_filter_pronounce(
json.loads(verbose_query_record.content)
) ==
_filter_pronounce(
json.loads(self.verbose_records[i].content)
)
)
break

Expand Down
4 changes: 4 additions & 0 deletions zdict/tests/dictionaries/test_naer.py
Expand Up @@ -16,6 +16,10 @@ def setup_class(cls):
cls.words = ['西爾河', 'spring mass']
cls.not_found_word = 'dsafwwe'

# Set query_timeout from 5 seconds to 60 seconds,
# so it won't timeout that often.
cls.dict.args.query_timeout = 60

# Setup normal query data
cls.dict.args.verbose = False
cls.records = [cls.dict.query(word) for word in cls.words]
Expand Down

0 comments on commit 179e3c6

Please sign in to comment.