-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and implement /kbbi command
- Loading branch information
Kemal Maulana
committed
Jun 11, 2016
1 parent
25cd515
commit 9b43082
Showing
15 changed files
with
786 additions
and
476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from tululbot.utils.kbbi import lookup_kbbi_definition | ||
|
||
|
||
def test_lookup_kbbi(mocker): | ||
class FakeResponse: | ||
def json(self): | ||
return { | ||
'kateglo': { | ||
'definition': [ | ||
{ | ||
'lex_class_ref': 'nomina', | ||
'def_text': 'foo bar', | ||
'sample': 'foo bar foo bar' | ||
}, | ||
{ | ||
'lex_class_ref': 'adjektiva', | ||
'def_text': 'baz quux', | ||
'sample': 'baz baz quux quux' | ||
} | ||
] | ||
} | ||
} | ||
|
||
def raise_for_status(self): | ||
pass | ||
|
||
fake_term = 'asdf asdf' | ||
mock_get = mocker.patch('tululbot.utils.kbbi.requests.get', return_value=FakeResponse(), | ||
autospec=True) | ||
|
||
rv = lookup_kbbi_definition(fake_term) | ||
|
||
assert len(rv) == 2 | ||
assert { | ||
'class': 'nomina', | ||
'def_text': 'foo bar', | ||
'sample': 'foo bar foo bar' | ||
} in rv | ||
assert { | ||
'class': 'adjektiva', | ||
'def_text': 'baz quux', | ||
'sample': 'baz baz quux quux' | ||
} in rv | ||
mock_get.assert_called_once_with('http://kateglo.com/api.php', | ||
params={'format': 'json', 'phrase': fake_term}) | ||
|
||
|
||
def test_lookup_kbbi_term_not_found(mocker): | ||
class FakeResponse: | ||
def json(self): | ||
raise ValueError | ||
|
||
def raise_for_status(self): | ||
pass | ||
|
||
mocker.patch('tululbot.utils.kbbi.requests.get', return_value=FakeResponse(), | ||
autospec=True) | ||
|
||
rv = lookup_kbbi_definition('asdf asdf') | ||
|
||
assert rv == [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from tululbot.utils.leli import search_on_wikipedia, search_on_google | ||
|
||
|
||
def test_search_on_wikipedia_and_found(mocker): | ||
mock_requests = mocker.patch('tululbot.utils.leli.requests', autospec=True) | ||
|
||
mock_requests.get.return_value.text = ( | ||
'<html>' | ||
' <div id="mw-content-text">' | ||
' <p>Tulul is the synonym of cool.</p>' | ||
' </div>' | ||
'</html>' | ||
) | ||
|
||
rv = search_on_wikipedia('tulul') | ||
|
||
assert rv == 'Tulul is the synonym of cool.' | ||
|
||
|
||
def test_ambiguous_term_on_wikipedia(mocker): | ||
mock_requests = mocker.patch('tululbot.utils.leli.requests', autospec=True) | ||
|
||
class FakeResponse: | ||
def __init__(self, text): | ||
self.text = text | ||
|
||
def raise_for_status(self): | ||
pass | ||
|
||
response1_text = ( | ||
'<html>' | ||
' <div id="mw-content-text">' | ||
' <p>Snowden may refer to:</p>' | ||
' <ul>' | ||
' <li><a href="/wiki/link1">Snowden1</a></li>' | ||
' <li><a href="/wiki/link2">Snowden2</a></li>' | ||
' </ul>' | ||
' </div>' | ||
'</html>' | ||
) | ||
response1 = FakeResponse(response1_text) | ||
|
||
response2_text = ( | ||
'<html>' | ||
' <div id="mw-content-text">' | ||
' <p>Snowden is former CIA employee.</p>' | ||
' </div>' | ||
'</html>' | ||
) | ||
response2 = FakeResponse(response2_text) | ||
|
||
mock_requests.get.side_effect = [response1, response2] | ||
|
||
rv = search_on_wikipedia('snowden') | ||
|
||
assert rv == 'Snowden is former CIA employee.' | ||
|
||
|
||
def test_search_on_google(): | ||
rv = search_on_google('wazaundtechnik') | ||
|
||
expected_text = ( | ||
'Jangan ngeleli! Googling dong: ' | ||
'https://google.com/search?q=wazaundtechnik' | ||
) | ||
assert rv == expected_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
from tululbot.utils.slang import lookup_kamusslang, lookup_urbandictionary, lookup_slang | ||
|
||
|
||
def test_lookup_kamusslang(mocker): | ||
class FakeParagraph: | ||
def __init__(self, strings): | ||
self.strings = strings | ||
|
||
strings = ['asdf', 'alsjdf', 'kfdg'] | ||
|
||
side_effect_pair = { | ||
'close-word-suggestion-text': None, | ||
'term-def': FakeParagraph(strings) | ||
} | ||
|
||
class FakeSoup: | ||
def find(self, class_): | ||
return side_effect_pair[class_] | ||
|
||
mocker.patch('tululbot.utils.slang.requests.get', autospec=True) | ||
mocker.patch('tululbot.utils.slang.BeautifulSoup', return_value=FakeSoup(), autospec=True) | ||
|
||
rv = lookup_kamusslang('jdflafj') | ||
|
||
assert rv == ''.join(strings) | ||
|
||
|
||
def test_lookup_kamusslang_no_definition_found(mocker): | ||
side_effect_pair = { | ||
'close-word-suggestion-text': None, | ||
'term-def': None | ||
} | ||
|
||
class FakeSoup: | ||
def find(self, class_): | ||
return side_effect_pair[class_] | ||
|
||
mocker.patch('tululbot.utils.slang.requests.get', autospec=True) | ||
mocker.patch('tululbot.utils.slang.BeautifulSoup', return_value=FakeSoup(), autospec=True) | ||
|
||
rv = lookup_kamusslang('jdflafj') | ||
|
||
assert rv is None | ||
|
||
|
||
def test_lookup_kamusslang_close_word_suggestion(mocker): | ||
class FakeParagraph: | ||
def __init__(self, strings): | ||
self.strings = strings | ||
|
||
strings = ['asdf', 'alsjdf', 'kfdg'] | ||
side_effect_pair = { | ||
'close-word-suggestion-text': 'Apalah', | ||
'term-def': FakeParagraph(strings) | ||
} | ||
|
||
class FakeSoup: | ||
def find(self, class_): | ||
return side_effect_pair[class_] | ||
|
||
mocker.patch('tululbot.utils.slang.requests.get', autospec=True) | ||
mocker.patch('tululbot.utils.slang.BeautifulSoup', return_value=FakeSoup(), autospec=True) | ||
|
||
rv = lookup_kamusslang('jdflafj') | ||
|
||
assert rv is None | ||
|
||
|
||
def test_lookup_urbandictionary(mocker): | ||
fake_definition = [ | ||
{ | ||
'def': 'mmeeeeooowww', | ||
'example': 'guk guk guk', | ||
'word': 'kaing kaing' | ||
}, | ||
{ | ||
'def': 'grrrrrrrr', | ||
'example': 'tokkeeeeekk tokkeeeekk', | ||
'word': 'aaauuuuuuuu' | ||
} | ||
] | ||
mocker.patch('tululbot.utils.slang.ud.define', return_value=fake_definition, autospec=True) | ||
|
||
rv = lookup_urbandictionary('eemmbeekk') | ||
|
||
assert rv == fake_definition[0]['def'] | ||
|
||
|
||
def test_lookup_urbandictionary_no_definition_found(mocker): | ||
fake_no_definition = [ | ||
{ | ||
'def': "\nThere aren't any definitions for kimcil yet.\nCan you define it?\n", | ||
'example': '', | ||
'word': '¯\\_(ツ)_/¯\n' | ||
} | ||
] | ||
mocker.patch('tululbot.utils.slang.ud.define', return_value=fake_no_definition, | ||
autospec=True) | ||
|
||
rv = lookup_urbandictionary('eemmbeekk') | ||
|
||
assert rv is None | ||
|
||
|
||
def test_lookup_slang_when_only_urbandictionary_has_definition(mocker): | ||
fake_definition = 'soba ni itai yo' | ||
mocker.patch('tululbot.utils.slang.lookup_urbandictionary', return_value=fake_definition, | ||
autospec=True) | ||
mocker.patch('tululbot.utils.slang.lookup_kamusslang', return_value=None, autospec=True) | ||
|
||
rv = lookup_slang('kimi no tame ni dekiru koto ga, boku ni aru kana?') | ||
|
||
assert rv == fake_definition | ||
|
||
|
||
def test_lookup_slang_when_only_kamusslang_has_definition(mocker): | ||
fake_definition = 'nagareru kisetsu no mannaka de' | ||
mocker.patch('tululbot.utils.slang.lookup_urbandictionary', return_value=None, | ||
autospec=True) | ||
mocker.patch('tululbot.utils.slang.lookup_kamusslang', return_value=fake_definition, | ||
autospec=True) | ||
|
||
rv = lookup_slang('futohi no nagasa wo kanjimasu') | ||
|
||
assert rv == fake_definition | ||
|
||
|
||
def test_lookup_slang_when_both_urbandictionary_and_kamusslang_have_no_definition(mocker): | ||
mocker.patch('tululbot.utils.slang.lookup_urbandictionary', return_value=None, | ||
autospec=True) | ||
mocker.patch('tululbot.utils.slang.lookup_kamusslang', return_value=None, autospec=True) | ||
|
||
rv = lookup_slang('hitomi wo tojireba anata ga') | ||
|
||
assert rv == 'Gak nemu cuy' | ||
|
||
|
||
def test_lookup_slang_when_both_urbandictionary_and_kamusslang_have_definition(mocker): | ||
fake_urbandict_def = 'mabuta no ura ni iru koto de' | ||
fake_kamusslang_def = 'dore hodo tsuyoku nareta deshou' | ||
mocker.patch('tululbot.utils.slang.lookup_urbandictionary', | ||
return_value=fake_urbandict_def, autospec=True) | ||
mocker.patch('tululbot.utils.slang.lookup_kamusslang', return_value=fake_kamusslang_def, | ||
autospec=True) | ||
|
||
rv = lookup_slang('anata ni totte watashi mo') | ||
|
||
fake_definition = ( | ||
'\U000026AB *urbandictionary*:\n{}' | ||
'\n\n' | ||
'\U000026AB *kamusslang*:\n{}' | ||
).format(fake_urbandict_def, fake_kamusslang_def) | ||
|
||
assert rv == fake_definition |
Oops, something went wrong.