Skip to content

Commit

Permalink
Refactor get_pictures function to remove user dependency and update r…
Browse files Browse the repository at this point in the history
…elated tests
  • Loading branch information
snakedye committed Jan 2, 2025
1 parent f3a0e94 commit 1c00dfb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 47 deletions.
4 changes: 1 addition & 3 deletions app/controllers/inspections.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ async def read_all_inspections(cp: ConnectionPool, user: User):

return inspections

async def get_pictures(cp: ConnectionPool, user: User, id: UUID | str):
async def get_pictures(cp: ConnectionPool, id: UUID | str):
"""
Retrieves the pictures associated with a user by inspection ID.
"""
if not user.id:
raise MissingUserAttributeError("User ID is required for fetching inspections.")
if not id:
raise ValueError("Inspection ID is required for fetching inspection details.")
if not isinstance(id, UUID):
Expand Down
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ async def get_inspection(
status_code=HTTPStatus.NOT_FOUND, detail="Inspection not found"
)

@app.get("/inspections/{id}/pictures", tags=["Inspections"])
@app.get("/inspections/{id}/pictures", tags=["Inspections"]) # TODO: Could be a separate endpoint
async def get_inspection_pictures(
cp: Annotated[ConnectionPool, Depends(get_connection_pool)],
user: Annotated[User, Depends(fetch_user)],
id: UUID,
):
try:
return await get_pictures(cp, user, id)
return await get_pictures(cp, id)
except InspectionNotFoundError:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Inspection not found"
Expand Down
48 changes: 6 additions & 42 deletions tests/test_inspections.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ async def test_valid_inspection_id_calls_get_full_inspection_json(
self.assertIsInstance(inspection, Inspection)

@patch("app.controllers.inspections.get_pictures")
async def test_valid_inspection_id_calls_get_pictures(
self, mock_get_full_inspection_json
):
async def test_valid_inspection_id_calls_get_pictures(self, mock_get_pictures):
cp = MagicMock()
conn_mock = MagicMock()
cursor_mock = MagicMock()
Expand All @@ -190,46 +188,12 @@ async def test_valid_inspection_id_calls_get_pictures(
user = User(id=uuid.uuid4())

Check failure on line 188 in tests/test_inspections.py

View workflow job for this annotation

GitHub Actions / lint-test / lint-test

Ruff (F841)

tests/test_inspections.py:188:9: F841 Local variable `user` is assigned to but never used
inspection_id = uuid.uuid4()

sample_inspection = {
"inspection_id": str(inspection_id),
"inspection_comment": "string",
"verified": False,
"company": {},
"manufacturer": {},
"product": {
"name": "string",
"label_id": str(uuid.uuid4()),
"registration_number": "2224256A",
"lot_number": "string",
"metrics": {
"weight": [],
"volume": {"edited": False},
"density": {"edited": False},
},
"npk": "string",
"warranty": "string",
"n": 0,
"p": 0,
"k": 0,
},
"cautions": {"en": [], "fr": []},
"instructions": {"en": [], "fr": []},
"guaranteed_analysis": {
"title": {"en": "string", "fr": "string"},
"is_minimal": False,
"en": [],
"fr": [],
},
}

mock_get_full_inspection_json.return_value = json.dumps(sample_inspection)
mock_get_pictures.return_value = []
pictures = await get_pictures(cp, inspection_id)

pictures = await get_pictures(cp, user, inspection_id)

mock_get_full_inspection_json.assert_called_once_with(
cursor_mock, inspection_id, user.id
)
self.assertIsInstance(pictures, [])
mock_get_pictures.assert_called_with(cursor_mock, inspection_id)
self.assertIsInstance(pictures, list)
# self.assertGreaterEqual(mock_get_pictures.call_count, 1)

@patch("app.controllers.inspections.get_full_inspection_json")
async def test_inspection_not_found_raises_error(
Expand Down

0 comments on commit 1c00dfb

Please sign in to comment.