From b8a91182b88c84ed26589aa6e4b76ec7b5f3dc0f Mon Sep 17 00:00:00 2001 From: marcocallea <102212141+marcocallea@users.noreply.github.com> Date: Fri, 17 May 2024 21:29:26 +0200 Subject: [PATCH] Delete Desktop/qd-starter-pack-main 2 directory adesso dovremmo esserci --- .../.github/workflows/ci.yml | 33 --- Desktop/qd-starter-pack-main 2/README.md | 6 - .../requirements_dev.txt | 5 - Desktop/qd-starter-pack-main 2/src/bot.py | 178 -------------- .../qd-starter-pack-main 2/tests/pytest.py | 227 ------------------ 5 files changed, 449 deletions(-) delete mode 100644 Desktop/qd-starter-pack-main 2/.github/workflows/ci.yml delete mode 100644 Desktop/qd-starter-pack-main 2/README.md delete mode 100644 Desktop/qd-starter-pack-main 2/requirements_dev.txt delete mode 100644 Desktop/qd-starter-pack-main 2/src/bot.py delete mode 100644 Desktop/qd-starter-pack-main 2/tests/pytest.py diff --git a/Desktop/qd-starter-pack-main 2/.github/workflows/ci.yml b/Desktop/qd-starter-pack-main 2/.github/workflows/ci.yml deleted file mode 100644 index 4d2233f..0000000 --- a/Desktop/qd-starter-pack-main 2/.github/workflows/ci.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: CI - -on: - push: - branches: [main] - paths-ignore: - - "README.md" - - "docs/**" - pull_request: - branches: [main] - paths-ignore: - - "README.md" - - "docs/**" - -jobs: - test: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Set up Python 3.10 - uses: actions/setup-python@v2 - with: - python-version: "3.10" - - name: Install dependencies for requirements and testing - run: | - python -m pip install --upgrade pip - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi - - name: Lint with pylint - run: pylint $(git ls-files '*.py') - - name: Test with pytest - run: pytest --cov src tests/ diff --git a/Desktop/qd-starter-pack-main 2/README.md b/Desktop/qd-starter-pack-main 2/README.md deleted file mode 100644 index 17e4507..0000000 --- a/Desktop/qd-starter-pack-main 2/README.md +++ /dev/null @@ -1,6 +0,0 @@ -*in fase di lavorazione* - -RemindMe Bot Telegram - -/start permette di avviare il bot -/show permette di mostrare i promemoria già inseriti ed eventualmente rimuoverli diff --git a/Desktop/qd-starter-pack-main 2/requirements_dev.txt b/Desktop/qd-starter-pack-main 2/requirements_dev.txt deleted file mode 100644 index 7a9d244..0000000 --- a/Desktop/qd-starter-pack-main 2/requirements_dev.txt +++ /dev/null @@ -1,5 +0,0 @@ -pytest -pytest-mock -pytest-cov -pylint -python-telegram-bot \ No newline at end of file diff --git a/Desktop/qd-starter-pack-main 2/src/bot.py b/Desktop/qd-starter-pack-main 2/src/bot.py deleted file mode 100644 index aa21ee0..0000000 --- a/Desktop/qd-starter-pack-main 2/src/bot.py +++ /dev/null @@ -1,178 +0,0 @@ -from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup -from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, CallbackQueryHandler - -MIOTOKEN = "" # inserite il vostro token - -month_to_number = { - "Gen": 1, - "Feb": 2, - "Mar": 3, - "Apr": 4, - "Mag": 5, - "Giu": 6, - "Lug": 7, - "Ago": 8, - "Set": 9, - "Ott": 10, - "Nov": 11, - "Dic": 12, -} - -async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text( - f"Ciao {update.effective_user.first_name} {update.effective_user.last_name}, benvenuto su RemindMe Bot, usa il comando /add per iniziare! Ricorda puoi anche usare il comando /show per mostrare i promemoria già inseriti." - ) - -def paginate(items, page=0, per_page=9): - start = page * per_page - end = start + per_page - has_next = end < len(items) - has_prev = start > 0 - return items[start:end], has_prev, has_next - -def create_pagination_keyboard(items, page, per_page, prefix): - items_page, has_prev, has_next = paginate(items, page, per_page) - keyboard = [ - [ - InlineKeyboardButton(item, callback_data=f"{prefix}-{item}") - for item in items_page - ] - ] - # Aggiunta dei pulsanti di navigazione - navigation_row = [] - if has_prev: - navigation_row.append( - InlineKeyboardButton( - "← Precedente", callback_data=f"{prefix}-prec-{page - 1}" - ) - ) - if has_next: - navigation_row.append( - InlineKeyboardButton( - "Successivo →", callback_data=f"{prefix}-succ-{page + 1}" - ) - ) - if navigation_row: - keyboard.append(navigation_row) - - return InlineKeyboardMarkup(keyboard) - -async def calendar_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - query = update.callback_query - await query.answer() - data = query.data - components = data.split("-") - action_type = components[ - 0 - ] # Definisce il tipo di azione (day, month, year, hour, minute, interval) - - type_config = { - "day": { - "range": range(1, 32), - "per_page": 8, - "next_step": "month", - "converter": int, - }, - "month": { - "range": [ - "Gen", - "Feb", - "Mar", - "Apr", - "Mag", - "Giu", - "Lug", - "Ago", - "Set", - "Ott", - "Nov", - "Dic", - ], - "per_page": 6, - "next_step": "year", - "converter": str, - }, - "year": { - "range": range(2024, 2031), - "per_page": 5, - "next_step": "hour", - "converter": int, - }, - "hour": { - "range": range(24), - "per_page": 6, - "next_step": "minute", - "converter": int, - }, - "minute": { - "range": range(60), - "per_page": 8, - "next_step": "interval", - "converter": int, - }, - "interval": { - "range": range(30), - "per_page": 11, - "next_step": "final_message", - "converter": int, - }, - } - - if "succ" in components or "prec" in components: - page = int(components[-1]) - reply_markup = create_pagination_keyboard( - type_config[action_type]["range"], - page, - type_config[action_type]["per_page"], - action_type, - ) - await query.edit_message_text( - f"Scegli {action_type}:", reply_markup=reply_markup - ) - else: - converter = type_config[action_type]["converter"] - selected_value = converter(components[1]) - context.user_data[action_type] = selected_value - if action_type == "month": - str_month = context.user_data["month"] - if str_month in month_to_number: - month_number = month_to_number[str_month] - context.user_data["month"] = str(month_number) - if action_type == "interval": - await query.edit_message_text("Inserisci il messaggio del promemoria! :") - else: - next_step = type_config[action_type]["next_step"] - reply_markup = create_pagination_keyboard( - type_config[next_step]["range"], - 0, - type_config[next_step]["per_page"], - next_step, - ) - await query.edit_message_text( - f"Scegli {next_step}:", reply_markup=reply_markup - ) - -async def add(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - # Gestisce l'aggiunta dei promemoria - reply_markup = create_pagination_keyboard(range(1, 32), 0, 8, "day") - await update.message.reply_text("Scegli il giorno:", reply_markup=reply_markup) - -def main(): - application = ApplicationBuilder().token(MIOTOKEN).build() - application.add_handler(CommandHandler("start", start)) - application.add_handler(CommandHandler("add", add)) - - # Aggiunge gestori per le callback delle query - application.add_handler(CallbackQueryHandler(calendar_callback, pattern="^day-")) - application.add_handler(CallbackQueryHandler(calendar_callback, pattern="^month-")) - application.add_handler(CallbackQueryHandler(calendar_callback, pattern="^year-")) - application.add_handler(CallbackQueryHandler(calendar_callback, pattern="^hour-")) - application.add_handler(CallbackQueryHandler(calendar_callback, pattern="^minute-")) - application.add_handler( - CallbackQueryHandler(calendar_callback, pattern="^interval-") - ) - - application.run_polling() - -if __name__ == "__main__": - main() diff --git a/Desktop/qd-starter-pack-main 2/tests/pytest.py b/Desktop/qd-starter-pack-main 2/tests/pytest.py deleted file mode 100644 index d560ac5..0000000 --- a/Desktop/qd-starter-pack-main 2/tests/pytest.py +++ /dev/null @@ -1,227 +0,0 @@ -import unittest -from unittest.mock import AsyncMock, MagicMock, patch -from telegram import Bot, InlineKeyboardMarkup, InlineKeyboardButton -from bot import ( - start, - create_pagination_keyboard, - paginate, - calendar_callback, - add - -) -import bot - -class TestStartFunction(unittest.IsolatedAsyncioTestCase): - async def test_start(self): - # Mock the Update and Context objects - update = AsyncMock() - context = AsyncMock() - update.effective_user.first_name = "Nome" - update.effective_user.last_name = "Cognome" - - # Execute the start function - await start(update, context) - - # Verify that reply_text was called correctly - update.message.reply_text.assert_awaited_once_with( - "Ciao Nome Cognome, benvenuto su RemindMe Bot, usa il comando /add per iniziare! Ricorda puoi anche usare il comando /show per mostrare i promemoria già inseriti." - ) - -class TestPaginateFunction(unittest.TestCase): - def test_paginate_first_page(self): - items = list(range(100)) # Una lista di 100 elementi numerici - page, has_previous, has_next = paginate(items, page=0, per_page=10) - self.assertEqual( - page, list(range(10)) - ) # Conferma che la prima pagina sia corretta - self.assertFalse(has_previous) # Non ci dovrebbero essere pagine precedenti - self.assertTrue(has_next) # Ci dovrebbero essere pagine successive - - def test_paginate_middle_page(self): - items = list(range(100)) - page, has_previous, has_next = paginate(items, page=5, per_page=10) - self.assertEqual(page, list(range(50, 60))) # Elementi dalla posizione 50 a 59 - self.assertTrue(has_previous) # Ci dovrebbero essere pagine precedenti - self.assertTrue(has_next) # Ci dovrebbero essere pagine successive - - def test_paginate_last_page(self): - items = list(range(100)) - page, has_previous, has_next = paginate(items, page=9, per_page=10) - self.assertEqual(page, list(range(90, 100))) # Ultimi 10 elementi - self.assertTrue(has_previous) # Ci dovrebbero essere pagine precedenti - self.assertFalse(has_next) # Non ci dovrebbero essere pagine successive - - def test_paginate_empty(self): - items = [] - page, has_previous, has_next = paginate(items, page=0, per_page=10) - self.assertEqual(page, []) # Nessun elemento - self.assertFalse(has_previous) # Non ci dovrebbero essere pagine precedenti - self.assertFalse(has_next) # Non ci dovrebbero essere pagine successive - -class TestPaginationFunctions(unittest.IsolatedAsyncioTestCase): - def test_create_pagination_keyboard(self): - prefix = "day" - items = list(range(1, 32)) - page = 0 - per_page = 7 - keyboard_markup = create_pagination_keyboard(items, page, per_page, prefix) - - self.assertIsInstance(keyboard_markup, InlineKeyboardMarkup) - self.assertTrue(len(keyboard_markup.inline_keyboard) > 0) - - -class TestCalendarCallback(unittest.TestCase): - async def test_day_selection(self): - # Test the selection of a day - update = MagicMock() - context = MagicMock() - query = AsyncMock() - update.callback_query = query - query.data = "day-10" - context.user_data = {} - - await calendar_callback(update, context) - - query.answer.assert_awaited() - self.assertEqual(context.user_data["day"], 10) - query.edit_message_text.assert_awaited_once() - - async def test_month_selection(self): - # Test the selection of a month - update = MagicMock() - context = MagicMock() - query = AsyncMock() - update.callback_query = query - query.data = "month-Feb" - month_to_number = {"Feb": 2} - context.user_data = {} - context.bot_data = {"month_to_number": month_to_number} - - await calendar_callback(update, context) - - query.answer.assert_awaited() - self.assertEqual(context.user_data["month"], 2) - query.edit_message_text.assert_awaited_once() - - async def test_year_selection(self): - # Test the selection of a year - update = MagicMock() - context = MagicMock() - query = AsyncMock() - update.callback_query = query - query.data = "year-2025" - context.user_data = {} - - await calendar_callback(update, context) - - query.answer.assert_awaited() - self.assertEqual(context.user_data["year"], 2025) - query.edit_message_text.assert_awaited_once() - - async def test_hour_selection(self): - # Test the selection of an hour - update = MagicMock() - context = MagicMock() - query = AsyncMock() - update.callback_query = query - query.data = "hour-13" - context.user_data = {} - - await calendar_callback(update, context) - - query.answer.assert_awaited() - self.assertEqual(context.user_data["hour"], 13) - query.edit_message_text.assert_awaited_once() - - async def test_minute_selection(self): - # Test the selection of a minute - update = MagicMock() - context = MagicMock() - query = AsyncMock() - update.callback_query = query - query.data = "minute-45" - context.user_data = {} - - await calendar_callback(update, context) - - query.answer.assert_awaited() - self.assertEqual(context.user_data["minute"], 45) - query.edit_message_text.assert_awaited_once() - - async def test_interval_selection(self): - # Test the selection of an interval - update = MagicMock() - context = MagicMock() - query = AsyncMock() - update.callback_query = query - query.data = "interval-15" - context.user_data = {} - - await calendar_callback(update, context) - - query.answer.assert_awaited() - self.assertEqual(context.user_data["interval"], 15) - query.edit_message_text.assert_awaited_once() - - async def test_final_message_prompt(self): - # Test the prompt for the final message after selecting the interval - update = MagicMock() - context = MagicMock() - query = AsyncMock() - update.callback_query = query - query.data = "interval-20" - context.user_data = {} - - await calendar_callback(update, context) - - query.answer.assert_awaited() - query.edit_message_text.assert_awaited_with( - "Inserisci il messaggio del promemoria! :" - ) - - - @patch("bot.create_pagination_keyboard") - async def test_add(self, mock_create_pagination_keyboard): - # Configura il mock per la create_pagination_keyboard - mock_keyboard = AsyncMock() - mock_create_pagination_keyboard.return_value = mock_keyboard - - # Esegui la funzione - await add(self.update, self.context) - - # Verifica che create_pagination_keyboard sia stato chiamato correttamente - mock_create_pagination_keyboard.assert_called_once_with( - range(1, 32), 0, 8, "day" - ) - - # Verifica che reply_text sia stato chiamato correttamente - self.update.message.reply_text.assert_awaited_once_with( - "Scegli il giorno:", reply_markup=mock_keyboard - ) - -class TestMain(unittest.TestCase): - - @patch("bot.ApplicationBuilder") - def test_main(self, mock_ApplicationBuilder): - # Setup - mock_application = MagicMock() - mock_ApplicationBuilder.return_value.token.return_value.build.return_value = ( - mock_application - ) - - # Esecuzione - bot.main() - - # Verifica che l'istanza dell'applicazione sia stata creata correttamente - mock_ApplicationBuilder.assert_called_once() - mock_ApplicationBuilder.return_value.token.assert_called_once_with(bot.MIOTOKEN) - mock_ApplicationBuilder.return_value.token.return_value.build.assert_called_once() - - # Verifica che run_polling sia stato chiamato - mock_application.run_polling.assert_called_once() - - - - -if __name__ == "__main__": - unittest.main()