Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Oct 19, 2021
1 parent 0b0ba1e commit 0f1326c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
16 changes: 9 additions & 7 deletions PyMultiDictionary/_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ class MultiDictionary(object):
Dictionary. Support synonyms, antonyms, meanings, and translations from some languages.
"""

_max_cached_websites: int # Maximum stored websites
_langs: Dict[str, Tuple[bool, bool, bool, bool]] # synonyms, meaning, translation, antonym
_test_cached_file: Dict[str, str] # If defined, loads that file instead
_tokenize: bool # Enables word tokenizer
_words: List[str]
_words_lang: str
_words: List[str] # List of words passed to the constructor
_words_lang: str # Language of the words passed to the constructor

def __init__(self, *words: Tuple[str, ...]) -> None:
"""
Expand Down Expand Up @@ -82,6 +83,7 @@ def __init__(self, *words: Tuple[str, ...]) -> None:
'uk': (True, True, True, False),
'zh': (True, True, True, False)
}
self._max_cached_websites = 15
self._test_cached_file = {}
self._tokenize = True
self._words = []
Expand Down Expand Up @@ -138,7 +140,7 @@ def _bsoup(self, link: str, encoding: str = 'utf-8') -> Optional['BeautifulSoup'
return None
bs = BeautifulSoup(data, 'html.parser')
_CACHED_SOUPS[link] = bs
if len(bs_keys) >= 50:
if len(bs_keys) >= self._max_cached_websites:
del _CACHED_SOUPS[bs[0]]
return bs

Expand Down Expand Up @@ -298,8 +300,8 @@ def antonym(self, lang: str, word: str, dictionary: str = DICT_SYNONYMCOM) -> An
words.append(w)
words.sort()

else:
raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')
# else:
# raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')

return words

Expand Down Expand Up @@ -456,8 +458,8 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
# Sort translations
words = sorted(words, key=lambda x: x[0])

else:
raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')
# else:
# raise InvalidDictionary(f'Dictionary {dictionary} cannot handle language {lang}')

return words

Expand Down
15 changes: 10 additions & 5 deletions test/test_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ def test_meaning(self) -> None:
# Empty
self.assertEqual(d.meaning('en', ''), ([], '', ''))

def test_meaning_wordnet(self) -> None:
"""
Test word meaning in wordnet.
"""
d = self._get_dictionary()
# Test wordnet
out = {'Noun': ['benefit', 'moral excellence or admirableness', 'that which is pleasing or valuable or useful',
'articles of commerce'],
'Adjective': ['having desirable or positive qualities especially those suitable for a thing specified',
Expand All @@ -110,6 +106,9 @@ def test_meaning_wordnet(self) -> None:
"completely and absolutely (`good' is sometimes used informally for `thoroughly'"]}
self.assertEqual(d.meaning('en', 'good', DICT_WORDNET), out)

# Test invalid dictionary
self.assertRaises(InvalidDictionary, lambda: d.meaning('es', 'word', DICT_WORDNET))

def test_translate(self) -> None:
"""
Test word parse before process.
Expand Down Expand Up @@ -154,6 +153,9 @@ def test_translate(self) -> None:
self.assertEqual(d.translate('en', '!!!'), [])
self.assertEqual(d.translate('en', ' !!! '), [])

# Test invalid dictionary
self.assertRaises(AssertionError, lambda: d.translate('es', 'word', dictionary=DICT_SYNONYMCOM))

def test_synonym(self) -> None:
"""
Test word synonym.
Expand Down Expand Up @@ -259,6 +261,9 @@ def test_antonym(self) -> None:
# Empty
self.assertEqual(d.antonym('en', '!!!'), [])

# Test invalid dictionary
# self.assertRaises(InvalidDictionary, lambda: d.antonym('es', 'word', dictionary=DICT_SYNONYMCOM))

def test_language_name(self) -> None:
"""
Test language name.
Expand Down

0 comments on commit 0f1326c

Please sign in to comment.