test: add and fix unit tests for doctor, family, and medical camp services;...
Overview
This MR improves unit test coverage for the service layer by adding comprehensive test cases for doctor, family, and medical camp services. It also resolves failing tests caused by missing websocket module imports by introducing proper mocking.
What does this MR do and why?
This MR focuses on strengthening backend reliability by improving test coverage and fixing test failures.
Previously:
- Service layer coverage was low
- Some test cases were failing due to missing
websocket_managermodule - Certain branches and edge cases were not covered
This change:
- Adds comprehensive unit tests for
doctor_service.py,family_service.py, andmedical_camp_service.py - Fixes websocket-related test failures using mocking
- Covers edge cases, exception paths, and conditional branches
- Improves overall test coverage significantly
No changes were made to business logic or functionality.
Changes Made
-
Added:
tests/unit/services/test_family_service.pytests/unit/services/test_medical_camp_service.py
-
Modified:
tests/unit/services/test_doctor_service.py
-
Improvements:
- Mocked missing
app.websocket_managermodule - Added tests for websocket-related flows
- Added tests for async/event loop scenarios
- Covered error handling and edge cases
- Mocked missing
Technical Details
-
Root Cause of Failures:
-
ModuleNotFoundErrordue to missingapp.websocket_manager - Complex async/websocket logic not properly mocked
- Uncovered branches in service methods
-
-
Fix Approach:
-
Injected mock module using
sys.modules -
Used
unittest.mock.patchfor:emit_queue_update- asyncio event loop handling
-
Mocked SQLAlchemy query chains using
MagicMock -
Used
SimpleNamespacefor model-like objects
-
-
Coverage Improvement:
- Covered previously missing lines and branches
- Ensured success, failure, and edge cases are tested
Type of Change
-
🐛 Bug fix -
✨ New feature -
💥 Breaking change -
📝 Documentation update -
♻ ️ Refactor -
⚡ Performance improvement -
🧪 Test update -
🔧 Configuration change -
🚨 Security fix -
🗑 ️ Deprecation
Related Issues / References
Closes #<70>
Screenshots or Screen Recordings
How to Validate Locally
- Pull the branch:
git pull origin <branch-name>
- Run tests:
pytest -v
- Run with coverage:
pytest --cov=app/services --cov-report=term-missing
Expected:
- All tests should pass
- Coverage should be significantly improved
Testing Done
-
Unit tests added/updated -
API endpoint tests passing
Test Cases Covered:
| Scenario | Expected Result | Status |
|---|---|---|
| Doctor creation/update/delete | Successful execution | |
| Patient assignment with websocket | Handled correctly | |
| Websocket failure handling | No crash, handled gracefully | |
| Async/event loop edge cases | Proper fallback handling | |
| Family service operations | All flows covered | |
| Medical camp service flows | All scenarios covered |
Test Commands Run:
pytest
pytest tests/unit/services/test_doctor_service.py -v
pytest --cov=app/services --cov-report=term-missing
Code Quality Checklist
Code Standards
-
Code follows project conventions -
No debug statements or unused code -
No duplicate code -
Type hints maintained -
Ruff checks pass
Python & FastAPI Best Practices
-
No changes to business logic -
Proper mocking used -
Error handling tested
API Design
-
No API changes
Database & Migrations
-
No schema changes
Security
-
No sensitive data exposed
Error Handling
-
Exception paths tested -
Graceful handling verified
Documentation
-
README.md updated -
.env.exampleupdated -
API documentation updated
(No documentation changes required)
Known Limitations / Technical Debt
- Websocket functionality is mocked in tests (not integration-tested)
- Complex query mocking may require updates if implementation changes
Additional Notes
- This MR strictly focuses on test coverage improvement
- No production code changes were made
- Helps improve maintainability and reliability of service layer
MR Acceptance Checklist
Quality & Correctness
-
Code works as intended -
No bugs introduced -
Edge cases handled
Maintainability
-
Code is readable and structured -
Tests are comprehensive -
Follows project conventions
Acceptance Review
-
Reviewed by at least 1 teammate -
Reviewed by product owner
