diff --git a/app/crud.py b/app/crud.py index 113ba25..0bacbe7 100644 --- a/app/crud.py +++ b/app/crud.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Optional from sqlalchemy.orm import Session @@ -7,12 +7,15 @@ from .schemas import School -def get_school(db: Session, school_id: str) -> School: +def get_school(db: Session, school_id: str) -> Optional[School]: school = db \ .query(models.School) \ .filter(models.School.id == school_id) \ .first() - return School.from_db(school) + if school: + return School.from_db(school) + return None + def get_schools(db: Session, skip: int = 0, limit: int = 100, filter_params=None) -> List[School]: diff --git a/test/test_api.py b/test/test_api.py index 7eb2d4c..269c9c2 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -329,3 +329,14 @@ def test_schools_by_bounding_box(self, client, db): # Assert assert response.status_code == 200 assert len(response.json()) == 2 + + def test_get_single_no_result(self, client, db): + # Arrange + self.__setup_schools(db) + + # Act + response = client.get("/schools/NI-12345") + + # Assert + assert response.status_code == 404 +