diff --git a/aim/digifeeds/db_client.py b/aim/digifeeds/db_client.py new file mode 100644 index 0000000..85daa24 --- /dev/null +++ b/aim/digifeeds/db_client.py @@ -0,0 +1,22 @@ +import requests +from aim.services import S + + +class DBClient: + def get_item(self, barcode: str): + url = f"{S.digifeeds_api_url}/{barcode}" + print(url) + response = requests.get(url) + if response.status_code == 200: + return response.json() + elif response.status_code == 404: + return None + else: + response.raise_for_status() + + def add_item(self, barcode: str): + response = requests.post(f"{S.digifeeds_api_url}/{barcode}") + if response.status_code == 200: + return response.json() + else: + response.raise_for_status() diff --git a/aim/services.py b/aim/services.py index b3dffdc..0525bbb 100644 --- a/aim/services.py +++ b/aim/services.py @@ -12,3 +12,4 @@ ) S.test_database = "sqlite:///:memory:" S.ci_on = os.getenv("CI") +S.digifeeds_api_url = os.getenv("DIGIFEEDS_API_URL") or "http://api:8000" diff --git a/compose.yml b/compose.yml index fbdf30a..9a26327 100644 --- a/compose.yml +++ b/compose.yml @@ -9,8 +9,6 @@ services: GID: ${GID:-1000} DEV: ${DEV:-false} POETRY_VERSION: ${POETRY_VERSION:-1.8.3} - ports: - - 8000:8000 env_file: - env.development - .env @@ -31,5 +29,27 @@ services: - MARIADB_PASSWORD=test@123 - MARIADB_DATABASE=database + api: + build: + context: . + target: development + dockerfile: Dockerfile + args: + UID: ${UID:-1000} + GID: ${GID:-1000} + DEV: ${DEV:-false} + POETRY_VERSION: ${POETRY_VERSION:-1.8.3} + ports: + - 8000:8000 + env_file: + - env.development + - .env + volumes: + - .:/app + tty: true + stdin_open: true + command: "poetry run uvicorn aim.digifeeds.database.main:app --host 0.0.0.0 --reload" + + volumes: database: diff --git a/tests/digifeeds/test_db_client.py b/tests/digifeeds/test_db_client.py new file mode 100644 index 0000000..3530f36 --- /dev/null +++ b/tests/digifeeds/test_db_client.py @@ -0,0 +1,47 @@ +import responses +import pytest +from aim.services import S +from aim.digifeeds.db_client import DBClient +from requests.exceptions import HTTPError + + +@responses.activate +def test_get_item_success(): + url = f"{S.digifeeds_api_url}/my_barcode" + responses.get(url, json={"item": "my_item"}, status=200) + item = DBClient().get_item(barcode="my_barcode") + assert item == {"item": "my_item"} + + +@responses.activate +def test_get_item_not_found(): + url = f"{S.digifeeds_api_url}/my_barcode" + responses.get(url, json={"item": "my_item"}, status=404) + item = DBClient().get_item(barcode="my_barcode") + assert item == None + + +@responses.activate +def test_get_item_raises_error(): + url = f"{S.digifeeds_api_url}/my_barcode" + responses.get(url, json={"item": "my_item"}, status=500) + with pytest.raises(Exception) as exc_info: + DBClient().get_item(barcode="my_barcode") + assert exc_info.type is HTTPError + + +@responses.activate +def test_add_item_success(): + url = f"{S.digifeeds_api_url}/my_barcode" + responses.post(url, json={"item": "my_item"}, status=200) + item = DBClient().add_item(barcode="my_barcode") + assert item == {"item": "my_item"} + + +@responses.activate +def test_add_item_failure(): + url = f"{S.digifeeds_api_url}/my_barcode" + responses.post(url, json={"item": "my_item"}, status=404) + with pytest.raises(Exception) as exc_info: + DBClient().add_item(barcode="my_barcode") + assert exc_info.type is HTTPError