Skip to content

Commit

Permalink
fix: raise HTTPException when no items are found
Browse files Browse the repository at this point in the history
  • Loading branch information
barosch47 committed Apr 3, 2024
1 parent ee59def commit c8d2d97
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/controller/electricity_reading_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from http import HTTPStatus
from typing import List

from fastapi import APIRouter, Path
from fastapi import APIRouter, HTTPException, Path

from ..repository.electricity_reading_repository import ElectricityReadingRepository
from ..service.electricity_reading_service import ElectricityReadingService
Expand Down Expand Up @@ -31,6 +31,6 @@ def store(data: ElectricReading):
def read(smart_meter_id: str = Path(openapi_examples=OPENAPI_EXAMPLES)):
readings = service.retrieve_readings_for(smart_meter_id)
if len(readings) < 1:
return HTTPStatus.NOT_FOUND
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="No readings found")
else:
return [r.to_json() for r in readings]
4 changes: 2 additions & 2 deletions src/controller/price_plan_comparator_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from http import HTTPStatus
from typing import Dict, List

from fastapi import APIRouter, Path, Query
from fastapi import APIRouter, HTTPException, Path, Query

from ..service.account_service import AccountService
from ..service.price_plan_service import PricePlanService
Expand All @@ -25,7 +25,7 @@ def compare(smart_meter_id: str = Path(openapi_examples=OPENAPI_EXAMPLES)):
list_of_spend_against_price_plans = price_plan_service.get_list_of_spend_against_each_price_plan_for(smart_meter_id)

if len(list_of_spend_against_price_plans) < 1:
return HTTPStatus.NOT_FOUND
raise HTTPException(status_code=HTTPStatus.NOT_FOUND)
else:
return {
"pricePlanId": account_service.get_price_plan(smart_meter_id),
Expand Down
10 changes: 7 additions & 3 deletions tests/controller/test_electricityReadingController.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import http.client
import unittest
from http import HTTPStatus

from fastapi.testclient import TestClient

Expand Down Expand Up @@ -34,12 +34,16 @@ def test_successfully_add_the_reading_against_existing_smart_meter_id(self):
self.assertIn({"time": 1505825848, "reading": 0.65}, readings)
self.assertIn({"time": 1605825849, "reading": 0.7}, readings)

def test_respond_with_404_when_no_readings_found(self):
response = self.client.get("/readings/read/meter-100")
assert response.status_code == HTTPStatus.NOT_FOUND

def test_respond_with_error_if_smart_meter_id_not_set(self):
readingJson = {"electricityReadings": [{"time": 1505825838, "reading": 0.6}]}

assert self.client.post("/readings/store", json=readingJson).status_code == http.client.UNPROCESSABLE_ENTITY
assert self.client.post("/readings/store", json=readingJson).status_code == HTTPStatus.UNPROCESSABLE_ENTITY

def test_respond_with_error_if_electricity_readings_not_set(self):
readingJson = {"smartMeterId": "meter-11"}

assert self.client.post("/readings/store", json=readingJson).status_code == http.client.UNPROCESSABLE_ENTITY
assert self.client.post("/readings/store", json=readingJson).status_code == HTTPStatus.UNPROCESSABLE_ENTITY

0 comments on commit c8d2d97

Please sign in to comment.