Created
December 10, 2025 13:44
-
-
Save sithumonline/444a28f5f0349e4080e75901846e9019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unittest | |
| from dataclasses import dataclass | |
| from abc import ABC, abstractmethod | |
| def isPalindrome(string): | |
| return string == string[::-1] | |
| test_users = { | |
| "alice": "SecurePass123!", | |
| "bob": "BobSecret456@", | |
| "charlie": "Charlie789#Pwd" | |
| } | |
| def login(username, password): | |
| if username == "" or password == "": | |
| return False | |
| if username in test_users and test_users[username] == password: | |
| return True | |
| return False | |
| @dataclass | |
| class Item: | |
| name: str | |
| price: float | |
| quantity: int | |
| def calculateTotalPrice(items: list[Item]) -> float: | |
| total = 0.0 | |
| for item in items: | |
| total += item.price * item.quantity | |
| return total | |
| class IDatabase(ABC): | |
| @abstractmethod | |
| def get_user_balance(self, user_id: str) -> float: | |
| pass | |
| class UserService: | |
| def __init__(self, database: IDatabase): | |
| self.database = database | |
| def getUserBalance(self, user_id: str) -> float: | |
| return self.database.get_user_balance(user_id) | |
| class IWeatherAPI(ABC): | |
| @abstractmethod | |
| def fetch_weather(self, city: str) -> dict: | |
| pass | |
| class WeatherService: | |
| def __init__(self, api: IWeatherAPI): | |
| self.api = api | |
| def get_weather(self, city: str) -> dict: | |
| return self.api.fetch_weather(city) | |
| class TestIsPalindrome(unittest.TestCase): | |
| def test_empty_string(self): | |
| self.assertTrue(isPalindrome("")) | |
| def test_single_character(self): | |
| self.assertTrue(isPalindrome("a")) | |
| def test_simple_palindrome_even_length(self): | |
| self.assertTrue(isPalindrome("noon")) | |
| def test_not_palindrome(self): | |
| self.assertFalse(isPalindrome("hello")) | |
| class TestLoginFunction(unittest.TestCase): | |
| def test_valid_login(self): | |
| self.assertTrue(login("alice", "SecurePass123!")) | |
| def test_invalid_username(self): | |
| self.assertFalse(login("dave", "SomePassword")) | |
| def test_invalid_password(self): | |
| self.assertFalse(login("bob", "WrongPassword")) | |
| def test_empty_username(self): | |
| self.assertFalse(login("", "SomePassword")) | |
| def test_empty_password(self): | |
| self.assertFalse(login("charlie", "")) | |
| class TestCalculateTotalPrice(unittest.TestCase): | |
| def test_empty_item_list(self): | |
| self.assertEqual(calculateTotalPrice([]), 0.0) | |
| def test_single_item(self): | |
| items = [Item(name="Apple", price=1.0, quantity=3)] | |
| self.assertEqual(calculateTotalPrice(items), 3.0) | |
| def test_multiple_items(self): | |
| items = [ | |
| Item(name="Banana", price=0.5, quantity=4), | |
| Item(name="Orange", price=0.75, quantity=2) | |
| ] | |
| self.assertEqual(calculateTotalPrice(items), 3.5) | |
| def test_items_with_zero_quantity(self): | |
| items = [ | |
| Item(name="Grapes", price=2.0, quantity=0), | |
| Item(name="Mango", price=1.5, quantity=2) | |
| ] | |
| self.assertEqual(calculateTotalPrice(items), 3.0) | |
| class MockDatabase(IDatabase): | |
| def __init__(self, balances): | |
| self.balances = balances | |
| def get_user_balance(self, user_id: str) -> float: | |
| return self.balances.get(user_id, 0.0) | |
| class TestGetUserBalance(unittest.TestCase): | |
| def setUp(self): | |
| balances = { | |
| "user1": 100.0, | |
| "user2": 250.5, | |
| "user3": 0.0 | |
| } | |
| self.mock_db = MockDatabase(balances) | |
| self.user_service = UserService(self.mock_db) | |
| def test_existing_user_balance(self): | |
| self.assertEqual(self.user_service.getUserBalance("user1"), 100.0) | |
| self.assertEqual(self.user_service.getUserBalance("user2"), 250.5) | |
| def test_non_existing_user_balance(self): | |
| self.assertEqual(self.user_service.getUserBalance("user4"), 0.0) | |
| def test_zero_balance_user(self): | |
| self.assertEqual(self.user_service.getUserBalance("user3"), 0.0) | |
| class MockWeatherAPI(IWeatherAPI): | |
| def fetch_weather(self, city: str) -> dict: | |
| mock_responses = { | |
| "New York": {"temperature": 22, "condition": "Sunny"}, | |
| "London": {"temperature": 15, "condition": "Cloudy"}, | |
| "Tokyo": {"temperature": 28, "condition": "Rainy"} | |
| } | |
| return mock_responses.get(city, {"temperature": None, "condition": "Unknown"}) | |
| class TestFetchWeather(unittest.TestCase): | |
| def setUp(self): | |
| self.mock_api = MockWeatherAPI() | |
| self.weather_service = WeatherService(self.mock_api) | |
| def test_existing_city_weather(self): | |
| self.assertEqual(self.weather_service.get_weather("New York"), {"temperature": 22, "condition": "Sunny"}) | |
| self.assertEqual(self.weather_service.get_weather("London"), {"temperature": 15, "condition": "Cloudy"}) | |
| def test_non_existing_city_weather(self): | |
| self.assertEqual(self.weather_service.get_weather("Mars"), {"temperature": None, "condition": "Unknown"}) | |
| if __name__ == '__main__': | |
| unittest.main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment