Skip to content

Commit d569269

Browse files
Fix the code
1 parent 21b3715 commit d569269

13 files changed

+109
-203
lines changed

async_application/cat_fact.py

-39
This file was deleted.

async_application/core.py

-55
This file was deleted.
File renamed without changes.

async_examples/basic.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import asyncio
2+
import aiohttp
3+
4+
5+
async def async_add(a, b):
6+
await asyncio.sleep(1) # Simulate an async operation
7+
return a + b

async_examples/cat_fact.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import logging
2+
import aiohttp
3+
import asyncio
4+
5+
6+
class CatFact:
7+
def __init__(self):
8+
self.base_url = "https://meowfacts.herokuapp.com/"
9+
10+
async def get_cat_fact(self):
11+
"""
12+
Asynchronously get a Cat Fact from Rest API and return a dict with status and fact.
13+
"""
14+
try:
15+
async with aiohttp.ClientSession() as session:
16+
async with session.get(self.base_url) as response:
17+
if response.status in (200, 201):
18+
json_response = await response.json()
19+
return {"status": response.status, "result": json_response}
20+
else:
21+
return {
22+
"status": response.status,
23+
"error": "Cat Fact Not Available",
24+
}
25+
except aiohttp.ClientError as err:
26+
logging.error(f"Client error: {err}")
27+
return {"status": 500, "error": "Failed to fetch Cat Fact"}
28+
except Exception as e:
29+
logging.error(f"Unexpected error: {e}")
30+
return {"status": 500, "error": "Failed to fetch Cat Fact"}
31+
32+
33+
async def main():
34+
cat_fact = CatFact()
35+
result = await cat_fact.get_cat_fact()
36+
print(result)
37+
38+
39+
if __name__ == "__main__":
40+
asyncio.run(main())

pytest.ini

-4
This file was deleted.

requirements.txt

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
pytest~=7.2.0
2-
pytest-xdist~=3.1.0
3-
coverage~=7.0.5
4-
black~=22.12.0
5-
pytest-timeout~=2.1.0
6-
requests~=2.28.2
7-
pytest-asyncio~= 0.20.3
1+
pytest~=8.1.1
2+
pytest-asyncio~=0.23.6
3+
aiohttp~=3.9.3
4+
pytest-mock~=3.14.0
85
asyncmock~=0.4.2

tests/test_basic.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
import asyncio
3+
import pytest_asyncio
4+
from async_examples.basic import async_add
5+
6+
7+
# Basic Async test function
8+
@pytest.mark.asyncio
9+
async def test_async_add():
10+
result = await async_add(1, 2)
11+
assert result == 3
12+
13+
14+
# Async fixtures
15+
@pytest_asyncio.fixture
16+
async def loaded_data():
17+
await asyncio.sleep(1) # Simulate loading data asynchronously
18+
return {"key": "value"}
19+
20+
21+
# Async test functions with fixtures
22+
@pytest.mark.asyncio
23+
async def test_fetch_data(loaded_data):
24+
assert loaded_data["key"] == "value"

tests/test_cat_fact.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
import asyncio
3+
import pytest_asyncio
4+
from asyncmock import AsyncMock
5+
from async_examples.cat_fact import CatFact
6+
7+
8+
@pytest_asyncio.fixture
9+
async def cat_fact():
10+
return CatFact()
11+
12+
13+
# Test with Real API call (Not Recommended)
14+
@pytest.mark.asyncio
15+
async def test_get_cat_fact(cat_fact):
16+
result = await cat_fact.get_cat_fact()
17+
print(result)
18+
assert result["status"] == 200
19+
assert "data" in result["result"]
20+
21+
22+
# Test with Mocked API call (Recommended when interacting with external services)
23+
@pytest.mark.asyncio
24+
async def test_get_cat_fact_mocked(mocker):
25+
# Mock the get_cat_fact method of CatFact
26+
mock_response = {"status": 200, "result": {"data": "Cats are awesome!"}}
27+
mocker.patch.object(CatFact, "get_cat_fact", AsyncMock(return_value=mock_response))
28+
29+
cat_fact_instance = CatFact()
30+
result = await cat_fact_instance.get_cat_fact()
31+
32+
assert result["status"] == 200
33+
assert "data" in result["result"]
34+
assert result["result"]["data"] == "Cats are awesome!"

tests/unit/__init__.py

Whitespace-only changes.

tests/unit/conftest.py

-8
This file was deleted.

tests/unit/test_async_application.py

-42
This file was deleted.

tests/unit/test_async_cat_fact.py

-48
This file was deleted.

0 commit comments

Comments
 (0)