Skip to content

Commit 1af8630

Browse files
author
Eric Sales De Andrade
committed
Add unit tests
1 parent c8f259d commit 1af8630

7 files changed

Lines changed: 56 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,5 @@ dmypy.json
116116

117117
*.idea
118118

119-
*.pytest_cache
119+
*.pytest_cache
120+
/tests/unit/tmp_data/

async_application/core.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# Spec 1. Upload a large file via HTTP Request 2. Write to DB 3.???
2-
2+
import json
33
import logging
4+
import time
5+
import uuid
6+
from datetime import datetime
47
import requests
58
from pathlib import Path
9+
import asyncio
610

711
home = str(Path.home())
812

913
# Set Logging
1014
logging.basicConfig(level=logging.INFO)
1115

1216

13-
def upload_file(file_name: str = None) -> str:
17+
async def upload_file(file_name: str = None) -> dict:
1418
"""
1519
Function to call the API via the Requests Library
1620
:param file_name: Filename, Str
@@ -21,11 +25,15 @@ def upload_file(file_name: str = None) -> str:
2125
"file": open(file_name, 'rb'),
2226
}
2327
endpoint = "https://api.anonfiles.com/upload"
28+
logging.info(f"Uploading File `{file_name}` to Server...")
2429
response = requests.post(endpoint, files=files)
30+
await asyncio.sleep(3) # TODO REMOVE
2531
response_json = response.json()
26-
logging.info(f"Response from API: {response_json}")
32+
logging.debug(f"Response from API: {response_json}")
2733
if response.status_code in (200, 201) \
2834
and response_json["status"] is True:
35+
36+
logging.info(f"File `{file_name}` successfully uploaded to Server!!!")
2937
return response_json
3038
response.raise_for_status()
3139
except requests.exceptions.HTTPError as errh:
@@ -38,4 +46,25 @@ def upload_file(file_name: str = None) -> str:
3846
logging.error(err)
3947

4048

41-
upload_file(file_name=f"{home}/sample.jpeg")
49+
async def save_to_disk() -> None:
50+
sample = {
51+
"Value": str(uuid.uuid4()),
52+
"Time": str(datetime.utcnow())
53+
}
54+
with open(f"./tests/unit/tmp_data/{str(uuid.uuid4())}.json", 'w', encoding='utf-8') as f:
55+
json.dump(sample, f, ensure_ascii=False, indent=4)
56+
logging.info("Data Saved to Disk!!!")
57+
58+
59+
async def main():
60+
print("Started")
61+
task1 = asyncio.create_task(upload_file(file_name=f"{home}/sample.jpeg"))
62+
task2 = asyncio.create_task(save_to_disk())
63+
64+
value_task1 = await task1
65+
value_task2 = await task2
66+
print("Finished")
67+
68+
asyncio.run(main())
69+
70+
# save_to_disk()

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
timeout = 5
3+
log_cli=true
4+
log_level=INFO

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pytest-xdist~=3.1.0
33
coverage~=7.0.5
44
black~=22.12.0
55
pytest-timeout~=2.1.0
6-
requests~=2.28.2
6+
requests~=2.28.2
7+
pytest-asyncio~= 0.20.3

tests/__init__.py

Whitespace-only changes.

tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pathlib import Path
2+
import pytest
3+
from async_application.core import upload_file, main
4+
5+
home = str(Path.home())
6+
7+
@pytest.mark.asyncio
8+
async def test_upload_file():
9+
result_upload = await upload_file(file_name=f"{home}/sample.jpeg")
10+
assert result_upload["status"] is True
11+
12+
13+
# @pytest.mark.asyncio
14+
# async def test_upload_file():
15+

0 commit comments

Comments
 (0)