From 1006baa212b915f0fd288c392adfe1b575c1904f Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 6 Jun 2022 19:26:48 +0300 Subject: [PATCH] sql_app: tests: Add basic unit tests (User Create) Signed-off-by: HeshamTB --- reqs.txt | 4 +++ sql_app/tests/__init__.py | 0 sql_app/tests/test_user.py | 59 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 sql_app/tests/__init__.py create mode 100644 sql_app/tests/test_user.py diff --git a/reqs.txt b/reqs.txt index afba101..84e67b1 100644 --- a/reqs.txt +++ b/reqs.txt @@ -4,7 +4,9 @@ asgiref==3.5.0 attrs==21.4.0 bitlist==0.6.2 cbor2==5.4.2.post1 +certifi==2022.5.18.1 cffi==1.15.0 +charset-normalizer==2.0.12 click==8.0.4 cryptography==36.0.1 Cython==0.29.28 @@ -34,6 +36,7 @@ python-decouple==3.6 python-dotenv==0.19.2 python-multipart==0.0.5 PyYAML==6.0 +requests==2.27.1 six==1.16.0 sniffio==1.2.0 SQLAlchemy==1.4.31 @@ -41,6 +44,7 @@ starlette==0.17.1 termcolor==1.1.0 tomli==2.0.1 typing_extensions==4.1.1 +urllib3==1.26.9 uvicorn==0.17.5 uvloop==0.16.0 watchgod==0.7 diff --git a/sql_app/tests/__init__.py b/sql_app/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sql_app/tests/test_user.py b/sql_app/tests/test_user.py new file mode 100644 index 0000000..073aa01 --- /dev/null +++ b/sql_app/tests/test_user.py @@ -0,0 +1,59 @@ + +from sys import path +import random +import string +from fastapi.testclient import TestClient + +from ..main import app +from ..schemas import UserCreate + + +MAX_ASCII = 255 +RAND_CHAR_SET = string.ascii_letters + +client = TestClient(app) + + +def gen_rand_str(size: int) -> str: + return ''.join(random.choice(RAND_CHAR_SET) for x in range(size)) + +def gen_new_user_dict() -> UserCreate: + noune = gen_rand_str(16) + new_user = UserCreate(email=f"testuser{noune}@mail.none", + username=f"testuser{noune}", + password=noune) + return new_user + +test_user : UserCreate = gen_new_user_dict() + +common_headres = { + 'accept': 'application/json', + 'Content-Type': 'application/json' +} + +def test_create_user(): + + new_user_json = { + "email" : test_user.email, + "username": test_user.username, + "password": test_user.password + } + response = client.request("POST", "/users/reg", + json=new_user_json, + headers=common_headres) + assert response.status_code == 200 + +def test_create_user_duplicate_fields(): + # Assumed that this test runs after test_create_user() + new_user_json = { + "email" : test_user.email, + "username": test_user.username, + "password": test_user.password + } + response = client.request("POST", "/users/reg", + json=new_user_json, + headers=common_headres) + assert response.status_code == 400 + +def test_create_iot_entity(): + pass