Skip to content

test: add and fix unit tests for doctor, family, and medical camp services;...

Vandana reddy Balannagari requested to merge testcoverage/service-pages into develop

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_manager module
  • Certain branches and edge cases were not covered

This change:

  • Adds comprehensive unit tests for doctor_service.py, family_service.py, and medical_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.py
    • tests/unit/services/test_medical_camp_service.py
  • Modified:

    • tests/unit/services/test_doctor_service.py
  • Improvements:

    • Mocked missing app.websocket_manager module
    • Added tests for websocket-related flows
    • Added tests for async/event loop scenarios
    • Covered error handling and edge cases

Technical Details

  • Root Cause of Failures:

    • ModuleNotFoundError due to missing app.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.patch for:

      • emit_queue_update
      • asyncio event loop handling
    • Mocked SQLAlchemy query chains using MagicMock

    • Used SimpleNamespace for 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

  • Test execution shows all tests passing
  • Coverage report indicates improved service coverage after image

How to Validate Locally

  1. Pull the branch:
git pull origin <branch-name>
  1. Run tests:
pytest -v
  1. 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.example updated
  • 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
Edited by Vandana reddy Balannagari

Merge request reports

Loading