Skip to content

Commit

Permalink
Refactor and implement /kbbi command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kemal Maulana committed Jun 11, 2016
1 parent 25cd515 commit 9b43082
Show file tree
Hide file tree
Showing 15 changed files with 786 additions and 476 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

**/quote** - Get random quote from [our database](https://github.com/tulul/tulul-quotes)

**/slang** - Lookup slang word definition; the result may surprise you!
**/slang** - Look up slang word definition; the result may surprise you!

**/hotline** - Dev group only

**/who** - Know your tululbot

**/hbd** - Send your friend a birthday blessing message

**/kbbi** - Look up word definition in KBBI

## Development Guide

### How to set up your machine
Expand Down
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import code
from os import environ
from os.path import join, dirname
from os.path import dirname, join
import subprocess
import sys

Expand Down
314 changes: 207 additions & 107 deletions tests/test_commands.py

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions tests/test_kbbi.py
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 == []
66 changes: 66 additions & 0 deletions tests/test_leli.py
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
154 changes: 154 additions & 0 deletions tests/test_slang.py
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

0 comments on commit 9b43082

Please sign in to comment.