test: improved test coverage for user service till 82% from 63%
Merge Request
Overview
What does this MR do and why? This Merge Request increases the unit test coverage for the core service module app/services/user_service.py .
The motivation behind this change is to improve the reliability and maintainability of the EHRS Medical Camp backend by ensuring that critical business logic surrounding user profiles and role management is robustly validated. These additions help prevent regressions during future schema updates or refactoring of the user management system.
Approach The approach focused on maximizing coverage of functional logic paths while maintaining a focus on successful business outcomes:
Role State Analysis: Implemented tests to verify how the service handles different initial states of a user's role list (e.g., None vs. empty lists). Data Integrity: Added validation for multi-field updates to ensure the service correctly synchronizes multiple attributes in a single transaction. Resiliency Testing: Ensured the update logic gracefully ignores fields that are present in the update schema but absent from the database model. Changes Made Tests Enhanced tests/test_services/test_user_service.py . Role management logic (e.g., redundant role assignment, role removal). Full coverage for the role removal utility functions.
Type of Change
-
🐛 Bug fix (non-breaking change that fixes an issue) -
✨ New feature (non-breaking change that adds functionality) -
💥 Breaking change (fix or feature that would cause existing functionality to change) -
📝 Documentation update -
♻ ️ Refactor (no functional changes) -
⚡ Performance improvement -
🧪 Test update -
🔧 Configuration change -
🚨 Security fix -
🗑 ️ Deprecation (removing deprecated code)
Screenshots or Screen Recordings
Example: Run all tests
pytest
Example: Run specific test file
pytest tests/test_api_v1/test_patient_routes.py -v
Example: Run with coverage
pytest --cov=app
## Code Quality Checklist
<!-- Reviewer: Use this checklist to evaluate code quality -->
### Code Standards
- [x] Code follows project conventions (naming, structure, formatting)
- [ ] No debug statements or commented-out code left (unless necessary and intended)
- [ ] No unused imports, variables, or functions
- [x] No duplicate code (DRY principle followed)
- [x] Type hints are properly defined (no `Any` unless justified and no mypy type check errors)
- [ ] Ruff checks pass:
```bash
ruff check .
ruff format . --check
Known Limitations / Technical Debt
Additional Notes
MR Acceptance Checklist
Quality & Correctness
-
Code works as intended and solves the stated problem -
No bugs introduced (existing functionality not broken)
Maintainability
-
Code is readable and well-organized -
Code is testable and well-tested -
Follows project patterns and conventions
Acceptance Review
-
Reviewed by at least 1 teammate -
Reviewed by product owner

