Skip to content

Commit

Permalink
added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
brucewzj99 committed May 5, 2024
1 parent 9d09d2e commit cc753e2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
5 changes: 4 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Release Notes
## Version 2.4 - Date 5 May 2024
## Version 2.4 - Date 6 May 2024
### Enhancement 🔥
- Clearer error messages for users

### For Developer 🧑‍💻
- Added more test case

## Version 2.3.7 - Date 3 May 2024
### For Developer 🧑‍💻
- Add number of error users for /notifyall command
Expand Down
21 changes: 19 additions & 2 deletions test/test_database.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from bot.database_service import firestore_service

from unittest import TestCase

from bot.error_handler import DatabaseError

class TestFirestoreService(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.db = firestore_service.FirestoreService()
cls.telegram_id = "123456"
cls.fake_telegram_id = "123"
cls.sheet_id = "sheet123"
cls.telegram_username = "test_user"

Expand All @@ -19,9 +19,26 @@ def test_check_if_user_exists(self):
# Assert
self.assertTrue(user_exists)


def test_check_if_user_dont_exists(self):
# Act
user_exists = self.db.check_if_user_exists(self.fake_telegram_id)

# Assert
self.assertFalse(user_exists)

def test_get_user_sheet_id(self):
# Act
sheet_id = self.db.get_user_sheet_id(self.telegram_id, self.telegram_username)

# Assert
self.assertEqual(sheet_id, self.sheet_id)


def test_get_dont_exist_user_sheet_id(self):
# Act & Assert
with self.assertRaises(DatabaseError) as context:
self.db.get_user_sheet_id(self.fake_telegram_id, self.telegram_username)

# Check if the error message is as expected
self.assertIn("User does not exist", context.exception.extra_info)
35 changes: 35 additions & 0 deletions test/test_errorhandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
from unittest.mock import patch
from bot.error_handler import GoogleSheetError, TelegramBotError, DatabaseError

class TestErrorHandling(unittest.TestCase):

def test_google_sheet_error_raises_correctly(self):
# Define the message and extra_info
message = "Test failure in Google Sheets"
extra_info = "Invalid Range"

# Check if the exception is raised with the correct message
with self.assertRaises(GoogleSheetError) as context:
raise GoogleSheetError(message, extra_info)

# Verify that the message in the exception is as expected
self.assertEqual(str(context.exception), f"GoogleSheetError: {message}")

def test_telegram_bot_error_raises_correctly(self):
message = "Failed to send message"
extra_info = "User not found"

with self.assertRaises(TelegramBotError) as context:
raise TelegramBotError(message, extra_info)

self.assertEqual(str(context.exception), f"TelegramBotError: {message}")

def test_database_error_raises_correctly(self):
message = "Database connection failed"
extra_info = "Timeout occurred"

with self.assertRaises(DatabaseError) as context:
raise DatabaseError(message, extra_info)

self.assertEqual(str(context.exception), f"DatabaseError: {message}")

0 comments on commit cc753e2

Please sign in to comment.