Skip to content

Commit

Permalink
contributors (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman <roman.rey@hawk.ai>
  • Loading branch information
rooom13 and hawk-roman-rey authored Apr 23, 2022
1 parent c4ac2ee commit 0b6356d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
7 changes: 5 additions & 2 deletions daily_word_bot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ def callback_on_addlevel(self, update: Update, context: CallbackContext) -> None
self.callback_on_mylevels(update, context)

def callback_on_info(self, update: Update, context: CallbackContext) -> None: # pragma: no cover
msg = f"""Version: <i>{config.VERSION}</i> deployed on {self.start_date}
\nWord bank info:\n - {len(self.word_bank.df.index)} words, last updated on {self.word_bank.last_updated_at}"""

msg = utils.build_info_msg(config.VERSION, self.start_date,
len(self.word_bank.df.index), self.word_bank.last_updated_at,
self.contributors)
update.message.reply_text(msg, parse_mode=ParseMode.HTML)

def callback_on_get_blockwords_callback(self, update: Update, context: CallbackContext) -> None: # pragma: no cover
Expand Down Expand Up @@ -355,6 +357,7 @@ def __init__(self):
self.dao = DAO(config.REDIS_HOST)
self.word_bank = WordBank(config.WORD_BANK_LOCAL)
self.backup_service = BackupService()
self.contributors = utils.fetch_contributors()

def run(self): # pragma: no cover
"""Run bot"""
Expand Down
27 changes: 26 additions & 1 deletion daily_word_bot/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re
import os
from collections import defaultdict
from typing import List
from typing import List, Dict
from datetime import datetime
import requests
from telegram import BotCommand, InlineKeyboardButton, InlineKeyboardMarkup

from daily_word_bot import utils
Expand Down Expand Up @@ -97,6 +99,22 @@ def build_available_commands_msg(bot_commands: List[BotCommand]) -> str:
return "Available commands:\n" + commands_str


hero_char = "🦸🏿‍♀️"


def build_info_msg(version: str, start_date: datetime,
word_bank_length: int, word_bank_update_date: datetime,
contributors: List[Dict]) -> str:
str_contributors = "\n".join([f" {hero_char} {c['login']}" for c in contributors if c['type'] != 'Bot'])
return (
f"Version: <i>{version}</i> deployed on {start_date}"
"\n"
f"\nWord bank info:\n - {word_bank_length} words, last updated on {word_bank_update_date}"
"\n"
f"\n Project hero contributors who deserve a cape: \n{str_contributors}"
)


def build_users_msg(users: List[dict]) -> str:

users_classified = defaultdict(list)
Expand Down Expand Up @@ -128,3 +146,10 @@ def build_users_msg(users: List[dict]) -> str:

def parse_admin_chat_ids_var() -> List[str]:
return (os.getenv("ADMIN_CHAT_IDS") or "").split(",")


def fetch_contributors() -> List[str]: # pragma: no cover
try:
return requests.get("https://api.github.com/repos/rooom13/dailyWord-bot/contributors").json()
except Exception:
return []
2 changes: 2 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from telegram.error import Unauthorized

from daily_word_bot.app import App
from daily_word_bot import utils


def test_is_admin():
Expand All @@ -14,6 +15,7 @@ def test_is_admin():

@pytest.fixture
def app():
utils.fetch_contributors = MagicMock()
app = App()
app.dao = MagicMock()
return app
Expand Down
36 changes: 26 additions & 10 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import os
import pytest
import unittest
from datetime import datetime
from telegram import BotCommand, InlineKeyboardButton

from daily_word_bot import utils

tc = unittest.TestCase()
import pytest


def test_highlight():
res = utils.highlight("a")
tc.assertEqual(res, "<b>a</b>")
assert res == "<b>a</b>"


@pytest.mark.parametrize("terms,expected", [
Expand All @@ -22,7 +21,7 @@ def test_highlight():
def test_get_terms_without_articles(terms, expected):

res = utils.get_terms_without_articles(terms)
tc.assertEqual(res, expected)
assert res == expected


@pytest.mark.parametrize("terms,sentence,expected", [
Expand All @@ -31,7 +30,7 @@ def test_get_terms_without_articles(terms, expected):
])
def test_highlight_in_sentence(terms, sentence, expected):
res = utils.highlight_in_sentence(sentence, terms)
tc.assertEqual(res, expected)
assert res == expected


def test_build_word_msg():
Expand All @@ -52,8 +51,7 @@ def test_build_word_msg():

res = utils.build_word_msg(word_data)

tc.assertEqual(res,
"\n🇩🇪 der pato/die pata"
assert res == ("\n🇩🇪 der pato/die pata"
"\n🇪🇸 el pato/la pata"
"\n"
"\n🇩🇪 der <b>pato</b> baila"
Expand All @@ -71,13 +69,31 @@ def test_build_available_commands_msg():
]

res = utils.build_available_commands_msg(bot_commands)
tc.assertEqual(res,
"Available commands:"
assert res == ("Available commands:"
"\n/command1 ➜ Description1"
"\n/command2 ➜ Description2"
"\n/command3 ➜ Description3")


def test_build_info_msg():
result = utils.build_info_msg("aVersion", datetime(2022, 12, 13), 500, datetime(2022, 12, 13), [
{"login": "romanito", "html_url": "http:/foo", "type": "User"},
{"login": "menganito", "html_url": "http:/foo", "type": "User"},
{"login": "bot", "html_url": "http:/foo", "type": "Bot"}
])

assert result == (
"Version: <i>aVersion</i> deployed on 2022-12-13 00:00:00"
"\n"
"\nWord bank info:"
"\n - 500 words, last updated on 2022-12-13 00:00:00"
"\n"
"\n Project hero contributors who deserve a cape: "
f"\n {utils.hero_char} romanito"
f"\n {utils.hero_char} menganito"
)


def test_build_users_msg():
users = [
{'name': 'romanito', 'isActive': True, 'chatId': 'aChatId', 'levels': ['beginner', 'intermediate']},
Expand Down

0 comments on commit 0b6356d

Please sign in to comment.