Skip to content

Testing improvement for data model usage #30

@aduane

Description

@aduane

Tests covering methods which utilize Django ORM queries must include assertions about the difference in behavior when the filters are adjusted. If a method accepts a patient_id, and uses that value to look up data belonging to that patient, a negative test should show that the same data is not returned given a different patient_id.

Example bad test:

class TestLastVisit:
    """Test last visit retrieval."""

    @patch("provider_panel_dashboard.apps.dashboard.Appointment")
    @patch("provider_panel_dashboard.apps.dashboard.arrow")
    def test_get_last_visit_with_appointment(self, mock_arrow, mock_appointment_class, dashboard_app):
        """Test retrieving last completed appointment."""
        # Mock current time
        mock_now = Mock()
        mock_now.datetime = datetime(2025, 12, 16, 12, 0, 0)
        mock_arrow.now.return_value = mock_now

        # Create mock appointment
        mock_appointment = Mock()
        mock_appointment.start_time = datetime(2025, 12, 1, 10, 0, 0)

        # Mock the query chain
        mock_queryset = Mock()
        mock_queryset.__iter__ = Mock(return_value=iter([mock_appointment]))
        mock_queryset.__getitem__ = Mock(return_value=mock_appointment)
        mock_queryset.__bool__ = Mock(return_value=True)

        mock_appointment_class.objects.filter.return_value.order_by.return_value = mock_queryset

        last_visit = dashboard_app._get_last_visit("patient-123")

        assert last_visit["found"] is True
        assert last_visit["date"] == "12/01/2025"
        assert last_visit["datetime"] == datetime(2025, 12, 1, 10, 0, 0)

This test would not fail if the method it was testing neglected to use the patient_id it was called with. It should also be asserting that only the most recent appointment is returned by asserting that the queryset's order_by method was called once with "start_time" as a parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions