Skip to content

Commit

Permalink
adds db client object
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Sep 25, 2024
1 parent 9c9d510 commit 3bdef46
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
22 changes: 22 additions & 0 deletions aim/digifeeds/db_client.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions aim/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
24 changes: 22 additions & 2 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
47 changes: 47 additions & 0 deletions tests/digifeeds/test_db_client.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3bdef46

Please sign in to comment.