Skip to content

test: add 81 unit tests for secondary services (patient consultation, medical...

Add Comprehensive Unit Tests for Core Services (81 tests)

Closes #
Related to #

Overview

This MR adds 81 new unit tests covering five critical and complex services in the EHRS FastAPI backend:

  • Patient Consultation Service
  • Medical Camp Service
  • Doctor Service
  • Consultation Queue Service
  • User / Role Service

All tests are pure unit tests — they use heavy mocking (unittest.mock) and do not touch the real database, ensuring fast execution and complete isolation.

Services & Test Coverage

Service Tests Main Areas Covered
Patient Consultation Service 22 CRUD, prescription management (add/update/delete), non-existent cases, edge cases
Medical Camp Service 19 CRUD, date filtering, attendance calculation, past-date validation, duplicates
Doctor Service 9 Get all/by ID, full/partial profile updates, UUID validation, error handling
Consultation Queue Service 16 Status transitions, priority handling, queue lookup (by book no, doctor, global)
User / Role Service 15 User retrieval & updates, role assignment/removal, duplicate roles, UUID checks

Total: 81 tests

Goals of these changes

  • Significantly improve test coverage of business-critical logic
  • Prevent regressions in complex service-layer behavior
  • Provide safety net for future refactoring
  • Serve as living documentation of expected behavior
  • Keep test suite fast (no real DB operations)

Technical highlights

  • Mocking: Extensive use of unittest.mock for SQLAlchemy queries, sessions, and model methods
  • Structure: pytest class-based organization, grouped by service and functionality
  • Coverage style: 3–5 tests per important function (happy path + errors + edge cases)
  • Zero side effects: No changes were made to any service implementation files
  • Test-only MR: only added new files under tests/test_services/

Test Results

# Service tests only
✅ 81/81 tests passing (100%)

# Full test suite (including previous tests)
✅ 354/354 tests passing (100%)

#How to test / verify

# Run only the new service tests
uv run python -m pytest tests/test_services/ -v

# Run individual service test files
uv run python -m pytest tests/test_services/test_patient_consultation_service.py -v
uv run python -m pytest tests/test_services/test_medical_camp_service.py -v
uv run python -m pytest tests/test_services/test_doctor_service.py -v
uv run python -m pytest tests/test_services/test_consultation_queue_service.py -v
uv run python -m pytest tests/test_services/test_user_service.py -v

# Run complete test suite
uv run python -m pytest tests/ -v

Expected outcome: 100% pass rate on both partial and full runs.
# Checklist

 [ ]Tests are passing (81/81 new tests, 354/354 total)
 [ ]All tests are properly mocked (no real database)
 [ ]Tests cover success paths, failure scenarios, and edge cases
 [ ]No changes made to production code (only test files added)
 [ ]Test files follow existing project test structure & naming
 [ ]Code formatting & linting checks pass
 [ ](Optional) Coverage percentage increased meaningfully (can be checked locally)

Ready for review — thanks in advance!

Merge request reports

Loading