From a1d8e10762321bea718e622cc7c7ab798b98318b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 19 Jul 2020 21:00:14 -0300 Subject: [PATCH 1/2] setting up testing environment --- .gitignore | 1 + bots/MsTaco/src/__init__.py | 0 bots/MsTaco/src/slack.py | 2 +- bots/MsTaco/test/__init__.py | 0 bots/MsTaco/test/persistence.py | 27 +++++++++++++++++++++++++++ bots/MsTaco/test/use_cases.py | 28 ++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 bots/MsTaco/src/__init__.py create mode 100644 bots/MsTaco/test/__init__.py create mode 100644 bots/MsTaco/test/persistence.py create mode 100644 bots/MsTaco/test/use_cases.py diff --git a/.gitignore b/.gitignore index 761c365..731e26e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ bots/MsTaco/db/*.json bots/MsTaco/src/config.py **/__pycache__/* +*.pyc diff --git a/bots/MsTaco/src/__init__.py b/bots/MsTaco/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bots/MsTaco/src/slack.py b/bots/MsTaco/src/slack.py index 7064560..8787464 100644 --- a/bots/MsTaco/src/slack.py +++ b/bots/MsTaco/src/slack.py @@ -4,7 +4,7 @@ slack_client = SlackClient(MSTACO_BOT_TOKEN) -channels = slack_client.api_call("conversations.list")['channels'] +channels = slack_client.api_call("conversations.list").get('channels',[]) users = slack_client.api_call("users.list") starterbot_id = None diff --git a/bots/MsTaco/test/__init__.py b/bots/MsTaco/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bots/MsTaco/test/persistence.py b/bots/MsTaco/test/persistence.py new file mode 100644 index 0000000..8d01145 --- /dev/null +++ b/bots/MsTaco/test/persistence.py @@ -0,0 +1,27 @@ +import unittest +from src.persistence import users_db, DBUser + +class PersistenceTestFixture(unittest.TestCase): + + def setUp(self): + users_db.purge() + self.db_user = DBUser(1) + + def test_AddTwoTacos(self): + self.db_user.add_tacos(1) + self.db_user.add_tacos(1) + tacos = self.db_user.owned_tacos() + self.assertEqual(2, tacos) + + def test_endWithOneTacoAfterAddingAndRemoving(self): + self.db_user.add_tacos(2) + self.db_user.remove_tacos(1) + tacos = self.db_user.owned_tacos() + self.assertEqual(2, tacos) + + def tearDown(self): + del(self.db_user) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/bots/MsTaco/test/use_cases.py b/bots/MsTaco/test/use_cases.py new file mode 100644 index 0000000..87a0237 --- /dev/null +++ b/bots/MsTaco/test/use_cases.py @@ -0,0 +1,28 @@ +import unittest +import src.use_cases as u +from src.persistence import users_db, DBUser + +ALICE = 10 +BOB = 20 + +class UseCasesTestFixture(unittest.TestCase): + + def setUp(self): + users_db.purge() + + def test_AliceGiveTacoToBob(self): + #given + self.assertEqual(5, DBUser(ALICE).remaining_tacos()) + self.assertEqual(0, DBUser(BOB).owned_tacos()) + + #when + u.give_tacos(ALICE, BOB, 1) + + #then + self.assertEqual(4, DBUser(ALICE).remaining_tacos()) + self.assertEqual(1, DBUser(BOB).owned_tacos()) + +# def tearDown(self): + +if __name__ == '__main__': + unittest.main() From d5b3e7e59f23bc907f331d5bbe2b1e23f3687b75 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 30 Jul 2020 18:33:16 -0300 Subject: [PATCH 2/2] add url to persistence --- bots/MsTaco/__init__.py | 6 +++-- bots/MsTaco/src/config.sample.py | 1 + bots/MsTaco/src/persistence.py | 8 +++++- bots/MsTaco/src/slack.py | 9 +++++++ bots/MsTaco/src/use_cases.py | 10 ++++--- bots/MsTaco/test/use_cases.py | 45 +++++++++++++++++++++++++++++++- 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/bots/MsTaco/__init__.py b/bots/MsTaco/__init__.py index ae10d82..31874ef 100644 --- a/bots/MsTaco/__init__.py +++ b/bots/MsTaco/__init__.py @@ -22,7 +22,7 @@ def handle_mention(event): channel = event["channel"] if giver_id != slack.starterbot_id and receiver_id != slack.starterbot_id and giver_id != receiver_id: - use_cases.give_tacos(giver_id, receiver_id, given_tacos, False, channel) + use_cases.give_tacos(giver_id, receiver_id, given_tacos, False, channel, event["text"]) def handle_reaction(event): @@ -31,9 +31,11 @@ def handle_reaction(event): receiver_id = event['item_user'] given_tacos = 1 if event['reaction'] == TACO.replace(':', '') else 0.5 channel = event["item"]["channel"] + ts = event["item"]['ts'] + messages = slack.get_messages(channel, ts) if giver_id != slack.starterbot_id and receiver_id != slack.starterbot_id and giver_id != receiver_id: - use_cases.give_tacos(giver_id, receiver_id, given_tacos, True, channel) + use_cases.give_tacos(giver_id, receiver_id, given_tacos, True, channel, messages[0]) def handle_direct_command(event): diff --git a/bots/MsTaco/src/config.sample.py b/bots/MsTaco/src/config.sample.py index 1e96c11..19ff36f 100644 --- a/bots/MsTaco/src/config.sample.py +++ b/bots/MsTaco/src/config.sample.py @@ -6,6 +6,7 @@ MSTACO_BOT_TOKEN = "aqui va el token" RTM_READ_DELAY = 1 # 1 second delay between reading from RTM MENTION_REGEX = "<@(|[WU].+?)>(.*)" +URL_REGEX = "(.*?)(https:\/\/.+?)($| (.*))" RESET_HOUR = 2 # Reset system at 6am. TACO = ":taco:" diff --git a/bots/MsTaco/src/persistence.py b/bots/MsTaco/src/persistence.py index 0ef724b..16ebc3d 100644 --- a/bots/MsTaco/src/persistence.py +++ b/bots/MsTaco/src/persistence.py @@ -24,6 +24,7 @@ def get_user(user_id): 'user_id': user_id, 'daily_tacos': DAILY_TACOS, 'daily_bonus': True, + 'urls': [], 'owned_tacos': 0 }) db_user = users_db.get(doc_id=db_user_id) @@ -49,10 +50,12 @@ def get_prev_weekly_info(): def update(self): self.user = users_db.get(Query()['user_id'] == self.user_id) - def add_tacos(self, amount, bonus=False): + def add_tacos(self, amount, bonus=False, url=''): users_db.update(add('owned_tacos', amount), Query()['user_id'] == self.user_id) if bonus: users_db.update({'daily_bonus': False}, Query()['user_id'] == self.user_id) + if url: + users_db.update(add('urls', [url]), Query()['user_id'] == self.user_id) self.update() def remove_tacos(self, amount): @@ -62,6 +65,9 @@ def remove_tacos(self, amount): def remaining_tacos(self): return self.user['daily_tacos'] + def urls(self): + return self.user['urls'] + def owned_tacos(self): return self.user['owned_tacos'] diff --git a/bots/MsTaco/src/slack.py b/bots/MsTaco/src/slack.py index 8787464..1464c71 100644 --- a/bots/MsTaco/src/slack.py +++ b/bots/MsTaco/src/slack.py @@ -23,6 +23,15 @@ def send_message(user_id, message): text=message ) +def get_messages(channel, msg_ts): + resp = slack_client.api_call( + "conversations.history", + channel=channel, + inclusive=true, + limit=1, + latest=msg_ts + ) + return resp.json()["messages"] def setup(): global starterbot_id diff --git a/bots/MsTaco/src/use_cases.py b/bots/MsTaco/src/use_cases.py index 5b44fe6..b931417 100644 --- a/bots/MsTaco/src/use_cases.py +++ b/bots/MsTaco/src/use_cases.py @@ -2,16 +2,18 @@ import operator import src.persistence as persistence import src.slack as slack -from src.config import DAILY_TACOS, MENTION_REGEX +from src.config import DAILY_TACOS, MENTION_REGEX, URL_REGEX from src.time_utils import get_today, get_time_left - -def give_tacos(giver_id, receiver_id, given_tacos, reaction=False, channel=None): +def give_tacos(giver_id, receiver_id, given_tacos, reaction=False, channel=None, message=""): giver = persistence.DBUser(giver_id) receiver = persistence.DBUser(receiver_id) + match_url = re.search(URL_REGEX, message) + url = match_url.group(2) if match_url else None + if giver.remaining_tacos() >= given_tacos: - receiver.add_tacos(given_tacos) + receiver.add_tacos(given_tacos, url=url) giver.remove_tacos(given_tacos) _notify_tacos_sent(giver.user_id, receiver.user_id, given_tacos, giver.remaining_tacos()) diff --git a/bots/MsTaco/test/use_cases.py b/bots/MsTaco/test/use_cases.py index 87a0237..fbef5e2 100644 --- a/bots/MsTaco/test/use_cases.py +++ b/bots/MsTaco/test/use_cases.py @@ -4,6 +4,7 @@ ALICE = 10 BOB = 20 +CRIS = 20 class UseCasesTestFixture(unittest.TestCase): @@ -22,7 +23,49 @@ def test_AliceGiveTacoToBob(self): self.assertEqual(4, DBUser(ALICE).remaining_tacos()) self.assertEqual(1, DBUser(BOB).owned_tacos()) -# def tearDown(self): + def test_AliceGiveTacoToBobWithUrl_conTextoAntesYDespues(self): + #given + self.assertEqual([], DBUser(BOB).urls()) + + #when + u.give_tacos(ALICE, BOB, 1, message="aqui https://github.com/ml-hispano/MLH_bot/issues/14 pueden ver el issue") + + #then + self.assertEqual(['https://github.com/ml-hispano/MLH_bot/issues/14'], DBUser(BOB).urls()) + + def test_AliceGiveTacoToBobWithUrl_conTextoAntes(self): + #given + self.assertEqual([], DBUser(BOB).urls()) + + #when + u.give_tacos(ALICE, BOB, 1, message="aqui pueden ver el issue https://github.com/ml-hispano/MLH_bot/issues/14") + + #then + self.assertEqual(['https://github.com/ml-hispano/MLH_bot/issues/14'], DBUser(BOB).urls()) + + def test_AliceGiveTacoToBobWithUrl_conTextoDespues(self): + #given + self.assertEqual([], DBUser(BOB).urls()) + + #when + u.give_tacos(ALICE, BOB, 1, message="https://github.com/ml-hispano/MLH_bot/issues/14 aqui pueden ver el issue") + + #then + self.assertEqual(['https://github.com/ml-hispano/MLH_bot/issues/14'], DBUser(BOB).urls()) + + def test_AliceGiveTacoToBobWithUrl_conUrlRepetida(self): + #given + self.assertEqual([], DBUser(BOB).urls()) + + #when + u.give_tacos(ALICE, BOB, 1, message="https://github.com/ml-hispano/MLH_bot/issues/14 aqui pueden ver el issue") + u.give_tacos(CRIS, BOB, 1, message="aqui pueden ver el issue https://github.com/ml-hispano/MLH_bot/issues/14") + + #then + self.assertEqual([ + 'https://github.com/ml-hispano/MLH_bot/issues/14', + 'https://github.com/ml-hispano/MLH_bot/issues/14', + ], DBUser(BOB).urls()) if __name__ == '__main__': unittest.main()