Skip to content

Commit

Permalink
Add unit tests for patient highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
weikangg committed Jan 5, 2025
1 parent 7d708fa commit ede6eb3
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 16 deletions.
5 changes: 3 additions & 2 deletions tests/test_allergy_reaction_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from app.models.patient_doctor_note_model import PatientDoctorNote
from app.models.patient_photo_model import PatientPhoto
from app.models.patient_model import Patient
from app.models.patient_assigned_dementia_model import PatientAssignedDementia
from app.models.patient_assigned_dementia_list_model import PatientAssignedDementiaList
from app.models.patient_assigned_dementia_mapping_model import PatientAssignedDementiaMapping
from app.models.patient_mobility_model import PatientMobility
from app.models.patient_prescription_model import PatientPrescription
from app.models.patient_social_history_model import PatientSocialHistory
Expand Down Expand Up @@ -50,7 +51,7 @@ def test_get_all_reaction_types(db_session_mock):
"""Test case for getting all allergy reaction types."""

# Arrange
db_session_mock.query.return_value.all.return_value = get_mock_allergy_reaction_types()
db_session_mock.query.return_value.filter.return_value.all.return_value = get_mock_allergy_reaction_types()

# Act
result = get_all_reaction_types(db_session_mock)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_allergy_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from app.models.patient_doctor_note_model import PatientDoctorNote
from app.models.patient_photo_model import PatientPhoto
from app.models.patient_model import Patient
from app.models.patient_assigned_dementia_model import PatientAssignedDementia
from app.models.patient_assigned_dementia_list_model import PatientAssignedDementiaList
from app.models.patient_assigned_dementia_mapping_model import PatientAssignedDementiaMapping
from app.models.patient_mobility_model import PatientMobility
from app.models.patient_prescription_model import PatientPrescription
from app.models.patient_social_history_model import PatientSocialHistory
Expand All @@ -30,7 +31,6 @@

# Mocking the relevant models


def test_create_allergy_type(
db_session_mock,
allergy_type_create,
Expand All @@ -54,7 +54,7 @@ def test_create_allergy_type(
def test_get_all_allergy_types(db_session_mock):
"""Test case for retrieving all allergy types."""
# Arrange
db_session_mock.query.return_value.all.return_value = get_mock_allergy_types()
db_session_mock.query.return_value.filter.return_value.all.return_value = get_mock_allergy_types()

# Act
result = get_all_allergy_types(db_session_mock)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_patient_allergy_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from app.models.patient_doctor_note_model import PatientDoctorNote
from app.models.patient_photo_model import PatientPhoto
from app.models.patient_model import Patient
from app.models.patient_assigned_dementia_model import PatientAssignedDementia
from app.models.patient_assigned_dementia_list_model import PatientAssignedDementiaList
from app.models.patient_assigned_dementia_mapping_model import PatientAssignedDementiaMapping
from app.models.patient_mobility_model import PatientMobility
from app.models.patient_prescription_model import PatientPrescription
from app.models.patient_social_history_model import PatientSocialHistory
Expand Down
2 changes: 1 addition & 1 deletion tests/test_patient_assigned_dementia_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def get_mock_assigned_dementias():
@mock.patch("app.models.patient_guardian_relationship_mapping_model.PatientGuardianRelationshipMapping")
@mock.patch("app.models.patient_patient_guardian_model.PatientPatientGuardian")
@mock.patch("app.models.allergy_reaction_type_model.AllergyReactionType")
@mock.patch("app.models.patient_assigned_dementia_list_model.PatientAssignedDementiaList")
@mock.patch("app.models.allergy_type_model.AllergyType")
@mock.patch("app.models.patient_social_history_model.PatientSocialHistory")
@mock.patch("app.models.patient_highlight_model.PatientHighlight")
Expand All @@ -76,6 +75,7 @@ def get_mock_assigned_dementias():
@mock.patch("app.models.patient_photo_model.PatientPhoto")
@mock.patch("app.models.patient_doctor_note_model.PatientDoctorNote")
@mock.patch("app.models.patient_allergy_mapping_model.PatientAllergyMapping")
@mock.patch("app.models.patient_assigned_dementia_list_model.PatientAssignedDementiaList")
@mock.patch("app.models.patient_assigned_dementia_mapping_model.PatientAssignedDementiaMapping")
def test_create_assigned_dementia(
mock_patient_assigned_dementia_mapping,
Expand Down
191 changes: 191 additions & 0 deletions tests/test_patient_highlight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import pytest
from unittest.mock import MagicMock
from app.crud.patient_highlight_crud import (
get_all_highlights,
get_highlights_by_patient,
create_highlight,
update_highlight,
delete_highlight,
)
from app.schemas.patient_highlight import PatientHighlightCreate, PatientHighlightUpdate
from app.models.patient_highlight_model import PatientHighlight
from datetime import datetime
from tests.utils.mock_db import get_db_session_mock


def test_get_all_highlights(db_session_mock):
"""Test case for retrieving all patient highlights."""
# Arrange
mock_data = [
MagicMock(
Id=1,
PatientId=1,
Type="Allergy",
HighlightJSON='{"id":1,"value":"Shellfish"}',
StartDate=datetime(2024, 3, 3),
EndDate=datetime(2024, 3, 6),
IsDeleted="0",
CreatedDate=datetime.now(),
ModifiedDate=datetime.now(),
CreatedById=1,
ModifiedById=1,
),
MagicMock(
Id=2,
PatientId=2,
Type="Prescription",
HighlightJSON='{"id":2,"value":"Paracetamol"}',
StartDate=datetime(2024, 3, 11),
EndDate=datetime(2024, 3, 14),
IsDeleted="0",
CreatedDate=datetime.now(),
ModifiedDate=datetime.now(),
CreatedById=1,
ModifiedById=1,
),
]
db_session_mock.query.return_value.filter.return_value.all.return_value = mock_data

# Act
result = get_all_highlights(db_session_mock)

# Assert
assert len(result) == 2
assert result[0].Type == "Allergy"
assert result[1].Type == "Prescription"


def test_get_highlights_by_patient(db_session_mock):
"""Test case for retrieving highlights for a specific patient."""
# Arrange
patient_id = 1
mock_data = [
MagicMock(
Id=1,
PatientId=patient_id,
Type="Allergy",
HighlightJSON='{"id":1,"value":"Shellfish"}',
StartDate=datetime(2024, 3, 3),
EndDate=datetime(2024, 3, 6),
IsDeleted="0",
CreatedDate=datetime.now(),
ModifiedDate=datetime.now(),
CreatedById=1,
ModifiedById=1,
)
]
db_session_mock.query.return_value.filter.return_value.all.return_value = mock_data

# Act
result = get_highlights_by_patient(db_session_mock, patient_id)

# Assert
assert len(result) == 1
assert result[0].PatientId == patient_id
assert result[0].Type == "Allergy"


def test_create_highlight(db_session_mock, patient_highlight_create):
"""Test case for creating a new patient highlight."""
# Arrange
created_by = 1

# Act
result = create_highlight(db_session_mock, patient_highlight_create, created_by)

# Assert
db_session_mock.add.assert_called_once_with(result)
db_session_mock.commit.assert_called_once()
db_session_mock.refresh.assert_called_once_with(result)
assert result.PatientId == patient_highlight_create.PatientId
assert result.Type == patient_highlight_create.Type
assert result.HighlightJSON == patient_highlight_create.HighlightJSON


def test_update_highlight(db_session_mock, patient_highlight_update):
"""Test case for updating a patient highlight."""
# Arrange
modified_by = 2
mock_highlight = PatientHighlight(
Id=1,
PatientId=1,
Type="Allergy",
HighlightJSON='{"id":1,"value":"Shellfish"}',
StartDate=datetime(2024, 3, 3),
EndDate=datetime(2024, 3, 6),
IsDeleted="0",
CreatedDate=datetime.now(),
ModifiedDate=datetime.now(),
CreatedById=1,
ModifiedById=1,
)
db_session_mock.query.return_value.filter.return_value.first.return_value = mock_highlight

# Act
result = update_highlight(
db_session_mock, mock_highlight.Id, patient_highlight_update, modified_by
)

# Assert
db_session_mock.commit.assert_called_once()
db_session_mock.refresh.assert_called_once_with(mock_highlight)
assert result.Type == patient_highlight_update.Type
assert result.ModifiedById == modified_by


def test_delete_highlight(db_session_mock):
"""Test case for deleting (soft-deleting) a patient highlight."""
# Arrange
modified_by = 2
mock_highlight = PatientHighlight(
Id=1,
PatientId=1,
Type="Allergy",
HighlightJSON='{"id":1,"value":"Shellfish"}',
StartDate=datetime(2024, 3, 3),
EndDate=datetime(2024, 3, 6),
IsDeleted="0",
CreatedDate=datetime.now(),
ModifiedDate=datetime.now(),
CreatedById=1,
ModifiedById=1,
)
db_session_mock.query.return_value.filter.return_value.first.return_value = mock_highlight

# Act
result = delete_highlight(db_session_mock, mock_highlight.Id, modified_by)

# Assert
db_session_mock.commit.assert_called_once()
assert result.IsDeleted == "1"
assert result.ModifiedById == modified_by


@pytest.fixture
def db_session_mock():
"""Fixture to mock the database session."""
return get_db_session_mock()


@pytest.fixture
def patient_highlight_create():
"""Fixture to provide a mock PatientHighlightCreate object."""
return PatientHighlightCreate(
PatientId=1,
Type="Allergy",
HighlightJSON='{"id":1,"value":"Shellfish"}',
StartDate=datetime(2024, 3, 3),
EndDate=datetime(2024, 3, 6),
)


@pytest.fixture
def patient_highlight_update():
"""Fixture to provide a mock PatientHighlightUpdate object."""
return PatientHighlightUpdate(
PatientId=1,
Type="Updated Allergy",
HighlightJSON='{"id":1,"value":"Updated Shellfish"}',
StartDate=datetime(2024, 3, 3),
EndDate=datetime(2024, 3, 6),
)
3 changes: 2 additions & 1 deletion tests/test_patient_highlight_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from app.models.patient_doctor_note_model import PatientDoctorNote
from app.models.patient_photo_model import PatientPhoto
from app.models.patient_model import Patient
from app.models.patient_assigned_dementia_model import PatientAssignedDementia
from app.models.patient_assigned_dementia_list_model import PatientAssignedDementiaList
from app.models.patient_assigned_dementia_mapping_model import PatientAssignedDementiaMapping
from app.models.patient_mobility_model import PatientMobility
from app.models.patient_prescription_model import PatientPrescription
from app.models.patient_social_history_model import PatientSocialHistory
Expand Down
6 changes: 4 additions & 2 deletions tests/test_patient_prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def db_session_mock():
@mock.patch("app.models.allergy_reaction_type_model.AllergyReactionType") # Ensure AllergyReactionType is mocked
@mock.patch("app.models.patient_doctor_note_model.PatientDoctorNote")
@mock.patch("app.models.patient_photo_model.PatientPhoto")
@mock.patch("app.models.patient_assigned_dementia_model.PatientAssignedDementia")
@mock.patch("app.models.patient_assigned_dementia_list_model.PatientAssignedDementiaList")
@mock.patch("app.models.patient_assigned_dementia_mapping_model.PatientAssignedDementiaMapping")
@mock.patch("app.models.patient_mobility_model.PatientMobility")
@mock.patch("app.models.patient_prescription_list_model.PatientPrescriptionList")
@mock.patch("app.models.patient_prescription_model.PatientPrescription")
Expand All @@ -98,7 +99,8 @@ def test_create_prescription(
mock_allergy_reaction_type,
mock_patient_doctor_note,
mock_patient_photo,
mock_patient_assigned_dementia,
mock_patient_assigned_dementia_list,
mock_patient_assigned_dementia_mapping,
mock_patient_mobility,
mock_patient_prescription_list,
mock_patient_prescription,
Expand Down
13 changes: 9 additions & 4 deletions tests/test_patient_social_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@
@mock.patch("app.models.allergy_reaction_type_model.AllergyReactionType") # Ensure AllergyReactionType is mocked
@mock.patch("app.models.patient_doctor_note_model.PatientDoctorNote")
@mock.patch("app.models.patient_photo_model.PatientPhoto")
@mock.patch("app.models.patient_assigned_dementia_model.PatientAssignedDementia")
@mock.patch("app.models.patient_assigned_dementia_list_model.PatientAssignedDementiaList")
@mock.patch("app.models.patient_assigned_dementia_mapping_model.PatientAssignedDementiaMapping")
@mock.patch("app.models.patient_mobility_model.PatientMobility")
@mock.patch("app.models.patient_prescription_model.PatientPrescription")
@mock.patch("app.models.patient_social_history_model.PatientSocialHistory")
@mock.patch("app.models.patient_vital_model.PatientVital")
@mock.patch("app.models.patient_highlight_model.PatientHighlight")
@mock.patch("app.models.allergy_type_model.AllergyType")
@mock.patch("app.models.patient_guardian_relationship_mapping_model.PatientGuardianRelationshipMapping")

def test_create_social_history(
mock_patient,
mock_patient_guardian,
mock_patient_allergy_mapping,
mock_allergy_reaction_type, # Added mock for AllergyReactionType
mock_patient_doctor_note,
mock_patient_photo,
mock_patient_assigned_dementia,
mock_patient_assigned_dementia_list,
mock_patient_assigned_dementia_mapping,
mock_patient_mobility,
mock_patient_prescription,
mock_patient_vital,
Expand Down Expand Up @@ -65,7 +68,8 @@ def test_create_social_history(
@mock.patch("app.models.allergy_reaction_type_model.AllergyReactionType") # Mock AllergyReactionType
@mock.patch("app.models.patient_doctor_note_model.PatientDoctorNote") # Mock PatientDoctorNote
@mock.patch("app.models.patient_photo_model.PatientPhoto") # Mock PatientPhoto
@mock.patch("app.models.patient_assigned_dementia_model.PatientAssignedDementia") # Mock PatientAssignedDementia
@mock.patch("app.models.patient_assigned_dementia_list_model.PatientAssignedDementiaList")
@mock.patch("app.models.patient_assigned_dementia_mapping_model.PatientAssignedDementiaMapping")
@mock.patch("app.models.patient_mobility_model.PatientMobility") # Mock PatientMobility
@mock.patch("app.models.patient_prescription_model.PatientPrescription") # Mock PatientPrescription
@mock.patch("app.models.patient_vital_model.PatientVital")
Expand All @@ -78,7 +82,8 @@ def test_get_social_history(
mock_patient_allergy_mapping,
mock_patient_doctor_note,
mock_patient_photo,
mock_patient_assigned_dementia,
mock_patient_assigned_dementia_list,
mock_patient_assigned_dementia_mapping,
mock_patient_mobility,
mock_patient_prescription,
mock_patient_vital,
Expand Down
6 changes: 4 additions & 2 deletions tests/test_patient_vital.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def db_session_mock():
@mock.patch("app.models.allergy_reaction_type_model.AllergyReactionType") # Ensure AllergyReactionType is mocked
@mock.patch("app.models.patient_doctor_note_model.PatientDoctorNote")
@mock.patch("app.models.patient_photo_model.PatientPhoto")
@mock.patch("app.models.patient_assigned_dementia_model.PatientAssignedDementia")
@mock.patch("app.models.patient_assigned_dementia_list_model.PatientAssignedDementiaList")
@mock.patch("app.models.patient_assigned_dementia_mapping_model.PatientAssignedDementiaMapping")
@mock.patch("app.models.patient_mobility_model.PatientMobility")
@mock.patch("app.models.patient_prescription_list_model.PatientPrescriptionList")
@mock.patch("app.models.patient_prescription_model.PatientPrescription")
Expand All @@ -35,7 +36,8 @@ def test_create_patient_vital(
mock_allergy_reaction_type,
mock_patient_doctor_note,
mock_patient_photo,
mock_patient_assigned_dementia,
mock_patient_assigned_dementia_list,
mock_patient_assigned_dementia_mapping,
mock_patient_mobility,
mock_patient_prescription_list,
mock_patient_prescription,
Expand Down

0 comments on commit ede6eb3

Please sign in to comment.