generated from mlibrary/python-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |