-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for patient highlight
- Loading branch information
Showing
19 changed files
with
338 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,51 @@ | ||
from sqlalchemy.orm import Session | ||
from ..models.patient_highlight_model import PatientHighlight | ||
from ..schemas.patient_highlight import HighlightCreate, HighlightUpdate | ||
from ..schemas.patient_highlight import PatientHighlightCreate, PatientHighlightUpdate | ||
from datetime import datetime | ||
from fastapi import HTTPException | ||
|
||
def get_highlights(db: Session, skip: int = 0, limit: int = 100): | ||
return db.query(PatientHighlight).order_by(PatientHighlight.id).offset(skip).limit(limit).all() | ||
def get_all_highlights(db: Session): | ||
return db.query(PatientHighlight).filter(PatientHighlight.IsDeleted == "0").all() | ||
|
||
def get_highlight(db: Session, highlight_id: int): | ||
return db.query(PatientHighlight).filter(PatientHighlight.id == highlight_id).first() | ||
def get_highlights_by_patient(db: Session, patient_id: int): | ||
return ( | ||
db.query(PatientHighlight) | ||
.filter(PatientHighlight.PatientId == patient_id, PatientHighlight.IsDeleted == "0") | ||
.all() | ||
) | ||
|
||
def get_highlights_grouped_by_patient(db: Session): | ||
return db.query(PatientHighlight).all() # Adjust this query as needed to group by patient_id | ||
|
||
def create_highlight(db: Session, highlight: HighlightCreate): | ||
db_highlight = PatientHighlight(**highlight.dict()) | ||
def create_highlight(db: Session, highlight_data: PatientHighlightCreate, created_by: int): | ||
db_highlight = PatientHighlight( | ||
**highlight_data.dict(), CreatedById=created_by, ModifiedById=created_by | ||
) | ||
db.add(db_highlight) | ||
db.commit() | ||
db.refresh(db_highlight) | ||
return db_highlight | ||
|
||
def update_highlight(db: Session, highlight_id: int, highlight: HighlightUpdate): | ||
db_highlight = db.query(PatientHighlight).filter(PatientHighlight.id == highlight_id).first() | ||
if db_highlight: | ||
for key, value in highlight.dict().items(): | ||
setattr(db_highlight, key, value) | ||
db.commit() | ||
db.refresh(db_highlight) | ||
def update_highlight(db: Session, highlight_id: int, highlight_data: PatientHighlightUpdate, modified_by: int): | ||
db_highlight = db.query(PatientHighlight).filter(PatientHighlight.Id == highlight_id).first() | ||
|
||
if not db_highlight or db_highlight.IsDeleted == "1": | ||
raise HTTPException(status_code=404, detail="Highlight not found") | ||
|
||
for key, value in highlight_data.dict(exclude_unset=True).items(): | ||
setattr(db_highlight, key, value) | ||
|
||
db_highlight.ModifiedDate = datetime.now() | ||
db_highlight.ModifiedById = modified_by | ||
db.commit() | ||
db.refresh(db_highlight) | ||
return db_highlight | ||
|
||
def delete_highlight(db: Session, highlight_id: int): | ||
db_highlight = db.query(PatientHighlight).filter(PatientHighlight.id == highlight_id).first() | ||
if db_highlight: | ||
db.delete(db_highlight) | ||
db.commit() | ||
def delete_highlight(db: Session, highlight_id: int, modified_by: int): | ||
db_highlight = db.query(PatientHighlight).filter(PatientHighlight.Id == highlight_id).first() | ||
|
||
if not db_highlight or db_highlight.IsDeleted == "1": | ||
raise HTTPException(status_code=404, detail="Highlight not found") | ||
|
||
db_highlight.IsDeleted = "1" | ||
db_highlight.ModifiedDate = datetime.now() | ||
db_highlight.ModifiedById = modified_by | ||
db.commit() | ||
return db_highlight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey | ||
from sqlalchemy import Column, Integer, String, DateTime,ForeignKey | ||
from sqlalchemy.orm import relationship | ||
from datetime import datetime, timezone | ||
|
||
from datetime import datetime | ||
from app.database import Base | ||
|
||
class PatientHighlight(Base): | ||
__tablename__ = "PATIENT_HIGHLIGHT" | ||
|
||
id = Column(Integer, primary_key=True, index=True) # Changed to Integer | ||
active = Column(String(1), default='Y', nullable=False) # used to check if record is active or not, substitute isDeleted column | ||
patientId = Column(Integer, ForeignKey('PATIENT.id')) # Changed to Integer | ||
type = Column(String(255)) | ||
highlightJSON = Column(String(255)) # Consider changing to Text if this is meant to store JSON data | ||
startDate = Column(DateTime) | ||
endDate = Column(DateTime) | ||
|
||
createdDate = Column(DateTime, nullable=False, default=DateTime) | ||
modifiedDate = Column(DateTime, nullable=False, default=DateTime) | ||
createdById = Column(Integer, nullable=False) # Changed to Integer | ||
modifiedById = Column(Integer, nullable=False) # Changed to Integer | ||
|
||
patient = relationship("Patient", back_populates="highlights") | ||
Id = Column(Integer, primary_key=True, index=True) | ||
IsDeleted = Column(String(1), default="0", nullable=False) | ||
PatientId = Column(Integer, ForeignKey("PATIENT.id"), nullable=False) | ||
Type = Column(String(255), nullable=False) | ||
HighlightJSON = Column(String(255), nullable=False) | ||
StartDate = Column(DateTime, nullable=False) | ||
EndDate = Column(DateTime, nullable=False) | ||
CreatedDate = Column(DateTime, nullable=False, default=datetime.now) | ||
ModifiedDate = Column(DateTime, nullable=False, default=datetime.now) | ||
CreatedById = Column(Integer, nullable=False, default=1) | ||
ModifiedById = Column(Integer, nullable=False, default=1) | ||
|
||
patient = relationship("Patient", back_populates="highlights") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,46 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from sqlalchemy.orm import Session | ||
from ..database import get_db | ||
from ..crud import patient_highlight_crud as crud_highlight | ||
from ..schemas import patient_highlight as schemas_highlight | ||
from ..crud.patient_highlight_crud import ( | ||
get_all_highlights, | ||
get_highlights_by_patient, | ||
create_highlight, | ||
update_highlight, | ||
delete_highlight, | ||
) | ||
from ..schemas.patient_highlight import ( | ||
PatientHighlight, | ||
PatientHighlightCreate, | ||
PatientHighlightUpdate, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
@router.get("/Highlight/list", response_model=list[schemas_highlight.Highlight]) | ||
def get_highlights_grouped_by_patient(db: Session = Depends(get_db)): | ||
return crud_highlight.get_highlights_grouped_by_patient(db) | ||
@router.get("/get_all_highlights", response_model=list[PatientHighlight], description="Get all highlights.") | ||
def get_all_patient_highlights(db: Session = Depends(get_db)): | ||
return get_all_highlights(db) | ||
|
||
@router.get("/Highlight", response_model=list[schemas_highlight.Highlight]) | ||
def get_highlights(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): | ||
return crud_highlight.get_highlights(db, skip=skip, limit=limit) | ||
@router.get("/get_highlights_by_patient/{patient_id}", response_model=list[PatientHighlight], description="Get highlights by patient ID.") | ||
def get_patient_highlights(patient_id: int, db: Session = Depends(get_db)): | ||
highlights = get_highlights_by_patient(db, patient_id) | ||
if not highlights: | ||
raise HTTPException(status_code=404, detail="No highlights found for the patient") | ||
return highlights | ||
|
||
@router.get("/Highlight/{highlight_id}", response_model=schemas_highlight.Highlight) | ||
def get_highlight(highlight_id: int, db: Session = Depends(get_db)): | ||
db_highlight = crud_highlight.get_highlight(db, highlight_id) | ||
if not db_highlight: | ||
raise HTTPException(status_code=404, detail="Highlight not found") | ||
return db_highlight | ||
@router.post("/create_highlight", response_model=PatientHighlight, description="Create a new highlight.") | ||
def create_patient_highlight(highlight_data: PatientHighlightCreate, db: Session = Depends(get_db)): | ||
# Replace with actual user ID in a real-world scenario | ||
created_by = 1 | ||
return create_highlight(db, highlight_data, created_by) | ||
|
||
@router.post("/Highlight/add", response_model=schemas_highlight.Highlight) | ||
def create_highlight(highlight: schemas_highlight.HighlightCreate, db: Session = Depends(get_db)): | ||
return crud_highlight.create_highlight(db, highlight) | ||
@router.put("/update_highlight/{highlight_id}", response_model=PatientHighlight, description="Update an existing highlight.") | ||
def update_patient_highlight(highlight_id: int, highlight_data: PatientHighlightUpdate, db: Session = Depends(get_db)): | ||
# Replace with actual user ID in a real-world scenario | ||
modified_by = 1 | ||
return update_highlight(db, highlight_id, highlight_data, modified_by) | ||
|
||
@router.put("/Highlight/update", response_model=schemas_highlight.Highlight) | ||
def update_highlight(highlight_id: int, highlight: schemas_highlight.HighlightUpdate, db: Session = Depends(get_db)): | ||
db_highlight = crud_highlight.update_highlight(db, highlight_id, highlight) | ||
if not db_highlight: | ||
raise HTTPException(status_code=404, detail="Highlight not found") | ||
return db_highlight | ||
|
||
@router.put("/Highlight/delete", response_model=schemas_highlight.Highlight) | ||
def delete_highlight(highlight_id: int, db: Session = Depends(get_db)): | ||
db_highlight = crud_highlight.delete_highlight(db, highlight_id) | ||
if not db_highlight: | ||
raise HTTPException(status_code=404, detail="Highlight not found") | ||
return db_highlight | ||
@router.delete("/delete_highlight/{highlight_id}", response_model=PatientHighlight, description="Soft delete a highlight by ID.") | ||
def delete_patient_highlight(highlight_id: int, db: Session = Depends(get_db)): | ||
# Replace with actual user ID in a real-world scenario | ||
modified_by = 1 | ||
return delete_highlight(db, highlight_id, modified_by) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
class PatientHighlightBase(BaseModel): | ||
PatientId: int = Field(..., example=1) | ||
Type: str = Field(..., example="Allergy") | ||
HighlightJSON: str = Field(..., example='{"id":1,"value":"Shellfish"}') | ||
StartDate: datetime = Field(..., example="2024-03-03T00:00:00") | ||
EndDate: datetime = Field(..., example="2024-03-06T00:00:00") | ||
IsDeleted: Optional[str] = Field(default="0", example="0") | ||
|
||
class HighlightBase(BaseModel): | ||
patientId: int | ||
description: str | ||
createdDate: datetime | ||
modifiedDate: datetime | ||
createdById: int | ||
modifiedById: int | ||
|
||
|
||
class HighlightCreate(HighlightBase): | ||
class PatientHighlightCreate(PatientHighlightBase): | ||
pass | ||
|
||
class PatientHighlightUpdate(PatientHighlightBase): | ||
pass | ||
|
||
class HighlightUpdate(BaseModel): | ||
description: Optional[str] = None | ||
modifiedDate: datetime | ||
modifiedById: int | ||
|
||
class PatientHighlight(PatientHighlightBase): | ||
Id: int = Field(..., example=1) | ||
CreatedDate: datetime = Field(..., example="2025-01-04T23:13:59.107") | ||
ModifiedDate: datetime = Field(..., example="2025-01-04T23:13:59.107") | ||
CreatedById: int = Field(..., example=1) | ||
ModifiedById: int = Field(..., example=1) | ||
|
||
class Highlight(HighlightBase): | ||
id: int | ||
|
||
model_config = {"from_attributes": True} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.