Spaces:
Running on Zero
Running on Zero
Download tests/unit/test_telegram_bot.py from abalanescu/flow2: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/spaces/abalanescu/flow2/resolve/main/tests/unit/test_telegram_bot.py
- Command line
-
hf download hf://spaces/abalanescu/flow2/tests/unit/test_telegram_bot.py
-
curl -L -o test_telegram_bot.py https://huggingface.co/spaces/abalanescu/flow2/resolve/main/tests/unit/test_telegram_bot.py
3.74 kB
| """Unit tests for the Telegram B2C bot: catalog rendering, buy flow, purchase log. | |
| Tests cover: | |
| - /catalog text contains both packages and prices | |
| - /buy with a valid package logs the purchase and returns a payment link | |
| - /buy with an unknown package returns an error and does not log | |
| - polling wrapper module imports cleanly and exposes handlers | |
| """ | |
| import importlib | |
| import json | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| # Ensure scripts dir is importable | |
| SCRIPTS = Path(__file__).resolve().parents[2] / "moldovan-qwen" / "scripts" | |
| if str(SCRIPTS) not in sys.path: | |
| sys.path.insert(0, str(SCRIPTS)) | |
| from telegram_bot import TelegramBeatsBot # noqa: E402 | |
| class TestTelegramBot(unittest.TestCase): | |
| def setUp(self): | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.catalog_path = Path(self.tmp.name) / "b2c_catalog.json" | |
| self.catalog_path.write_text( | |
| json.dumps( | |
| { | |
| "packages": [ | |
| { | |
| "name": "Starter", | |
| "price_usd": 19, | |
| "description": "Weekly beats", | |
| "features": ["MP3"], | |
| }, | |
| { | |
| "name": "Pro", | |
| "price_usd": 49, | |
| "description": "All access", | |
| "features": ["Stems"], | |
| }, | |
| ] | |
| } | |
| ), | |
| encoding="utf-8", | |
| ) | |
| # Redirect purchase log to tmp dir | |
| self.bot = TelegramBeatsBot(token="TEST", catalog_file=self.catalog_path) | |
| self.bot.purchases_log = Path(self.tmp.name) / "purchases_log.jsonl" | |
| def tearDown(self): | |
| self.tmp.cleanup() | |
| def test_catalog_text_lists_packages_and_prices(self): | |
| text = self.bot.get_catalog_text() | |
| self.assertIn("Starter", text) | |
| self.assertIn("$19", text) | |
| self.assertIn("Pro", text) | |
| self.assertIn("$49", text) | |
| def test_buy_valid_package_logs_purchase_and_returns_link(self): | |
| response = self.bot.handle_buy("Pro") | |
| self.assertIn("Pro", response) | |
| self.assertIn("https://", response) | |
| log_content = self.bot.purchases_log.read_text(encoding="utf-8") | |
| self.assertIn('"package": "Pro"', log_content) | |
| self.assertIn('"price_usd": 49', log_content) | |
| def test_buy_unknown_package_does_not_log(self): | |
| response = self.bot.handle_buy("Nonexistent") | |
| self.assertIn("nu a fost găsit", response) | |
| self.assertFalse(self.bot.purchases_log.exists()) | |
| class TestPollingWrapper(unittest.TestCase): | |
| def test_polling_module_imports(self): | |
| try: | |
| mod = importlib.import_module("telegram_bot_polling") | |
| except ModuleNotFoundError: | |
| self.skipTest("python-telegram-bot not installed in test env") | |
| return | |
| self.assertTrue(hasattr(mod, "start_handler")) | |
| self.assertTrue(hasattr(mod, "catalog_handler")) | |
| self.assertTrue(hasattr(mod, "buy_handler")) | |
| self.assertTrue(callable(mod._create_payment_url)) | |
| # Placeholder URL path (no STRIPE_SECRET_KEY set) | |
| url = mod._create_payment_url(19) | |
| self.assertTrue(url.startswith("https://")) | |
| def test_placeholder_url_contains_price(self): | |
| try: | |
| mod = importlib.import_module("telegram_bot_polling") | |
| except ModuleNotFoundError: | |
| self.skipTest("python-telegram-bot not installed in test env") | |
| return | |
| url = mod._create_payment_url(49) | |
| self.assertIn("49", url) | |
| if __name__ == "__main__": | |
| unittest.main() |