test: increase app/services test coverage to 100% for medical_camp, medicine,...
Merge Request
Overview
Increase test coverage for app/services/medical_camp_service.py, app/services/patient_consultation_service.py, and app/services/patient_service.py to 100% (99% for patient_consultation due to unreachable dead code). No source code changes — only test files modified.
What does this MR do and why?
Three service files had significant coverage gaps:
-
medical_camp_service.pyat 34% -
patient_consultation_service.pyat 29% -
patient_service.pyat 22%
This MR adds targeted tests to cover every reachable branch, edge case, and error path in all three files.
Changes Made
-
Modified:
tests/test_services/test_medical_camp_service.py— added ~480 lines of tests -
Modified:
tests/test_services/test_patient_consultation_service.py— added ~840 lines of tests -
Modified:
tests/test_services/test_patient_service.py— added ~1040 lines of tests - No source code changes
- No files added or removed
Technical Details
test_medical_camp_service.py:
-
get_medical_camp_by_date_location,get_all_medical_camps,get_medical_camps_by_date -
update_medical_camp/delete_medical_campnot found paths -
calculate_attended_patients— camp found with patients, camp not found -
update_attended_patients_count— camp found, camp not found -
signup_user_for_camp— no camp, restricted roles (doctor/patient), admin/coordinator/volunteer role mapping, already signed up, enum value branch -
update_user_attendance— invalid UUID, no camp, user not registered, success -
get_volunteer_camp_status— today camp exists, fallback to latest, no camp, user not registered
test_patient_consultation_service.py:
-
validate_consultation_status_for_prescription_access— visit details not found - CRUD functions:
get_patient_consultation_by_id,get_by_consultation_id,get_by_visit_details_id,get_by_doctor_id,update(not found),delete(found/not found) -
get_patient_consultations_by_book_no— all 5 branches (patient not found, no camp, no visit, no details, success) -
get_prescriptions_by_consultation_id— not found, with/without prescriptions -
update_prescription_by_id— all branches (not found, replacement fields, dispatch status variations, nonexistent attribute) -
delete_prescription_by_id— not found, already dispensed, success -
add_prescriptions_to_consultation— not found, overlap, missing medicines, success with/without visit details -
assign_patient_to_consultation— all 6 branches
test_patient_service.py:
-
_auto_transition_queue_to_know_your_patient— queue found, queue not found -
create_patient— with user mapping, without user -
update_patient— not found, patient fields, user fields (name/phone/age/sex), no user_id -
register_patient_for_camp— patient not found, no user_id, no camp, already registered, success -
search_patients— by name, phone, book_no, default, empty results -
get_patient_areas— with/without search filter -
create_vital— patient not found, no camp, not registered, update existing, create new, auto-transition failure -
get_vitals_with_camp_info— patient not found, no visits, full results with limit, null fields -
update_vital— patient not found, no camp, not registered, no details, success, auto-transition failure
Type of Change
-
🧪 Test update
Related Issues / References
- Closes #<issue_number>
Screenshots or Screen Recordings
N/A — test-only changes, no API modifications.
How to Validate Locally
-
Run the service test suite with coverage:
uv run pytest tests/test_services/test_medical_camp_service.py tests/test_services/test_patient_consultation_service.py tests/test_services/test_patient_service.py --cov=app.services.medical_camp_service --cov=app.services.patient_consultation_service --cov=app.services.patient_service --cov-report=term-missing -v -
Verify all tests pass
-
Verify coverage output
- Previous behaviour: 34%, 29%, 22% coverage respectively
- Changes made: Added comprehensive tests for all reachable branches and paths
- New behaviour: 100%, 99%, 100% coverage respectively
Testing Done
-
Unit tests added/updated -
API endpoint tests passing
Test Cases Covered:
| Scenario | Expected Result | Status |
|---|---|---|
medical_camp_service CRUD and query functions |
Correct returns for found/not found | |
signup_user_for_camp all role mappings and error paths |
Correct role mapping, proper errors | |
update_user_attendance and get_volunteer_camp_status
|
All branches covered | |
patient_consultation_service CRUD and prescription management |
All branches and edge cases | |
add_prescriptions_to_consultation and assign_patient_to_consultation
|
Multi-step flows with all error paths | |
patient_service create/update patient with user mapping |
User attributes mapped correctly | |
register_patient_for_camp full flow |
All 5 error/success paths | |
search_patients by name, phone, book_no, default |
Correct query filtering | |
create_vital and update_vital full flows |
Create/update paths, auto-transition | |
get_vitals_with_camp_info with camp data and null fields |
Correct schema construction |
Test Commands Run:
uv run pytest tests/test_services/test_medical_camp_service.py tests/test_services/test_patient_consultation_service.py tests/test_services/test_patient_service.py --cov=app.services.medical_camp_service --cov=app.services.patient_consultation_service --cov=app.services.patient_service --cov-report=term-missing -v
Code Quality Checklist
Code Standards
-
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 -
No duplicate code (DRY principle followed) -
Type hints are properly defined (no Anyunless justified and no mypy type check errors) -
Ruff checks pass: ruff check . ruff format . --check
Python & FastAPI Best Practices
N/A — no source code changes, test-only MR.
API Design
N/A — no API changes.
Database & Migrations
N/A — no database or schema changes.
Security
-
No sensitive data logged (passwords, tokens, PII) -
SQL injection prevention verified (ORM used)
Error Handling
N/A — no source code changes.
Documentation
N/A — no setup, env, or API changes.
Known Limitations / Technical Debt
patient_consultation_service.py is at 99% — lines 416-417 are unreachable dead code (a del block after a raise ValueError that always fires first). Removing those 2 lines would bring it to 100%, but that requires a source code change which is out of scope for this test-only MR.
Additional Notes
- All tests use mocks — no database or network dependencies required
- Functions with multiple sequential
db.query()calls useside_effectto return different mock chains per call - Auto-transition queue tests verify both success and graceful failure paths
MR Acceptance Checklist
Quality & Correctness
-
Code works as intended and solves the stated problem -
No bugs introduced (existing functionality not broken) -
Edge cases handled appropriately
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
Closes #69 (closed)