|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# pylint: disable=wrong-import-position |
| 3 | +# pylint: disable=R0801 |
| 4 | +"""Test Stackademy application.""" |
| 5 | + |
| 6 | +# python stuff |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | +from pathlib import Path |
| 11 | +from unittest.mock import Mock, patch |
| 12 | + |
| 13 | +from app.exceptions import ConfigurationException |
| 14 | +from app.logging_config import get_logger |
| 15 | +from app.stackademy import Stackademy |
| 16 | + |
| 17 | + |
| 18 | +HERE = os.path.abspath(os.path.dirname(__file__)) |
| 19 | +PROJECT_ROOT = str(Path(HERE).parent.parent) |
| 20 | +PYTHON_ROOT = str(Path(PROJECT_ROOT).parent) |
| 21 | +if PYTHON_ROOT not in sys.path: |
| 22 | + sys.path.append(PYTHON_ROOT) # noqa: E402 |
| 23 | + |
| 24 | + |
| 25 | +logger = get_logger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class TestStackademy(unittest.TestCase): |
| 29 | + """Test Stackademy application.""" |
| 30 | + |
| 31 | + def setUp(self): |
| 32 | + """Set up test fixtures before each test method.""" |
| 33 | + self.app = Stackademy() |
| 34 | + |
| 35 | + def test_stackademy_initialization(self): |
| 36 | + """Test that the Stackademy application initializes successfully.""" |
| 37 | + self.assertIsNotNone(self.app) |
| 38 | + self.assertIsNotNone(self.app.db) |
| 39 | + |
| 40 | + def test_database_connection_success(self): |
| 41 | + """Test successful database connection.""" |
| 42 | + # Mock the database connection to return True |
| 43 | + with patch.object(self.app.db, "test_connection", return_value=True): |
| 44 | + result = self.app.test_database_connection() |
| 45 | + self.assertTrue(result) |
| 46 | + logger.info("Database connection test passed successfully") |
| 47 | + |
| 48 | + def test_database_connection_failure(self): |
| 49 | + """Test database connection failure.""" |
| 50 | + # Mock the database connection to raise an exception |
| 51 | + with patch.object(self.app.db, "test_connection", side_effect=Exception("Connection failed")): |
| 52 | + result = self.app.test_database_connection() |
| 53 | + self.assertFalse(result) |
| 54 | + logger.info("Database connection failure test passed") |
| 55 | + |
| 56 | + def test_get_courses_with_description_filter(self): |
| 57 | + """Test retrieving courses with description filter.""" |
| 58 | + # Mock course data |
| 59 | + mock_courses = [ |
| 60 | + { |
| 61 | + "course_code": "PY101", |
| 62 | + "course_name": "Python Fundamentals", |
| 63 | + "description": "Learn Python programming basics", |
| 64 | + "cost": 299.99, |
| 65 | + "prerequisite_course_code": None, |
| 66 | + "prerequisite_course_name": None, |
| 67 | + }, |
| 68 | + { |
| 69 | + "course_code": "PY201", |
| 70 | + "course_name": "Advanced Python", |
| 71 | + "description": "Advanced Python programming techniques", |
| 72 | + "cost": 399.99, |
| 73 | + "prerequisite_course_code": "PY101", |
| 74 | + "prerequisite_course_name": "Python Fundamentals", |
| 75 | + }, |
| 76 | + ] |
| 77 | + |
| 78 | + # Mock the database query |
| 79 | + with patch.object(self.app.db, "execute_query", return_value=mock_courses): |
| 80 | + courses = self.app.get_courses(description="python") |
| 81 | + |
| 82 | + self.assertEqual(len(courses), 2) |
| 83 | + self.assertEqual(courses[0]["course_code"], "PY101") |
| 84 | + self.assertEqual(courses[1]["course_code"], "PY201") |
| 85 | + |
| 86 | + # Log course information as in the original code |
| 87 | + logger.info("Retrieved %d courses with python description", len(courses)) |
| 88 | + for course in courses: |
| 89 | + logger.info( |
| 90 | + " - %s (%s) - %s - $%s", |
| 91 | + course["course_name"], |
| 92 | + course["course_code"], |
| 93 | + course["description"], |
| 94 | + course["cost"], |
| 95 | + ) |
| 96 | + |
| 97 | + def test_get_courses_with_cost_filter(self): |
| 98 | + """Test retrieving courses with maximum cost filter.""" |
| 99 | + mock_courses = [ |
| 100 | + { |
| 101 | + "course_code": "WEB101", |
| 102 | + "course_name": "Web Development Basics", |
| 103 | + "description": "Introduction to web development", |
| 104 | + "cost": 199.99, |
| 105 | + "prerequisite_course_code": None, |
| 106 | + "prerequisite_course_name": None, |
| 107 | + } |
| 108 | + ] |
| 109 | + |
| 110 | + with patch.object(self.app.db, "execute_query", return_value=mock_courses): |
| 111 | + courses = self.app.get_courses(max_cost=250.0) |
| 112 | + |
| 113 | + self.assertEqual(len(courses), 1) |
| 114 | + self.assertLessEqual(courses[0]["cost"], 250.0) |
| 115 | + logger.info("Retrieved courses under $250: %d", len(courses)) |
| 116 | + |
| 117 | + def test_get_courses_database_error(self): |
| 118 | + """Test get_courses when database error occurs.""" |
| 119 | + with patch.object(self.app.db, "execute_query", side_effect=Exception("Database error")): |
| 120 | + courses = self.app.get_courses(description="python") |
| 121 | + |
| 122 | + self.assertEqual(len(courses), 0) |
| 123 | + logger.info("Database error handling test passed") |
| 124 | + |
| 125 | + def test_get_courses_no_results(self): |
| 126 | + """Test get_courses when no courses match criteria.""" |
| 127 | + with patch.object(self.app.db, "execute_query", return_value=[]): |
| 128 | + courses = self.app.get_courses(description="nonexistent") |
| 129 | + |
| 130 | + self.assertEqual(len(courses), 0) |
| 131 | + logger.info("No results test passed") |
| 132 | + |
| 133 | + def test_application_workflow_with_configuration_exception(self): |
| 134 | + """Test application workflow that raises ConfigurationException.""" |
| 135 | + # pylint: disable=broad-exception-caught |
| 136 | + try: |
| 137 | + # Simulate a configuration error |
| 138 | + raise ConfigurationException("Invalid configuration setting") |
| 139 | + except ConfigurationException as e: |
| 140 | + logger.error("Configuration error: %s", e) |
| 141 | + self.assertIsInstance(e, ConfigurationException) |
| 142 | + |
| 143 | + def test_application_workflow_with_general_exception(self): |
| 144 | + """Test application workflow that raises general exception.""" |
| 145 | + # pylint: disable=broad-exception-caught,broad-except |
| 146 | + try: |
| 147 | + # Simulate a general application error |
| 148 | + raise RuntimeError("General application error") |
| 149 | + except Exception as e: |
| 150 | + logger.error("Application error: %s", e) |
| 151 | + self.assertIsInstance(e, Exception) |
| 152 | + |
| 153 | + def test_full_application_workflow(self): |
| 154 | + """Test the complete application workflow as shown in the example.""" |
| 155 | + mock_courses = [ |
| 156 | + { |
| 157 | + "course_code": "PY101", |
| 158 | + "course_name": "Python Fundamentals", |
| 159 | + "description": "Learn Python programming from scratch", |
| 160 | + "cost": 299.99, |
| 161 | + "prerequisite_course_code": None, |
| 162 | + "prerequisite_course_name": None, |
| 163 | + }, |
| 164 | + { |
| 165 | + "course_code": "AI201", |
| 166 | + "course_name": "Python for AI", |
| 167 | + "description": "Python programming for artificial intelligence", |
| 168 | + "cost": 499.99, |
| 169 | + "prerequisite_course_code": "PY101", |
| 170 | + "prerequisite_course_name": "Python Fundamentals", |
| 171 | + }, |
| 172 | + ] |
| 173 | + |
| 174 | + # pylint: disable=broad-exception-caught |
| 175 | + try: |
| 176 | + # Initialize the application |
| 177 | + app = Stackademy() |
| 178 | + self.assertIsNotNone(app) |
| 179 | + |
| 180 | + # Test database connection |
| 181 | + logger.info("Testing database connection...") |
| 182 | + with patch.object(app.db, "test_connection", return_value=True): |
| 183 | + if not app.test_database_connection(): |
| 184 | + logger.error("Database connection failed. Please check your configuration.") |
| 185 | + self.fail("Database connection should have succeeded") |
| 186 | + logger.info("Database connection successful!") |
| 187 | + |
| 188 | + # Get courses |
| 189 | + logger.info("Retrieving courses...") |
| 190 | + with patch.object(app.db, "execute_query", return_value=mock_courses): |
| 191 | + courses = app.get_courses(description="python") |
| 192 | + |
| 193 | + self.assertEqual(len(courses), 2) |
| 194 | + |
| 195 | + for course in courses: |
| 196 | + logger.info( |
| 197 | + " - %s (%s) - %s - $%s", |
| 198 | + course["course_name"], |
| 199 | + course["course_code"], |
| 200 | + course["description"], |
| 201 | + course["cost"], |
| 202 | + ) |
| 203 | + |
| 204 | + except ConfigurationException as e: |
| 205 | + logger.error("Configuration error: %s", e) |
| 206 | + self.fail(f"Unexpected ConfigurationException: {e}") |
| 207 | + except Exception as e: |
| 208 | + logger.error("Application error: %s", e) |
| 209 | + self.fail(f"Unexpected application error: {e}") |
| 210 | + |
| 211 | + |
| 212 | +if __name__ == "__main__": |
| 213 | + unittest.main() |
0 commit comments