From 71517bb8b2f72f05b408761248a85476c5928c8b Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 6 Jan 2021 21:46:29 +0100 Subject: [PATCH] Delete testing user afer unit test session --- database/crud.py | 10 ++++++---- tests/conftest.py | 14 ++++++++++++++ tests/queries_test.py | 12 ------------ tests/requests_test.py | 6 +++--- 4 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 tests/conftest.py diff --git a/database/crud.py b/database/crud.py index fd3373b..9c73510 100644 --- a/database/crud.py +++ b/database/crud.py @@ -2,6 +2,7 @@ from datetime import datetime from fastapi import HTTPException from passlib.context import CryptContext +from sqlalchemy.inspection import inspect from app.schemas import * from constants import SHA1_SALT @@ -31,11 +32,12 @@ def insert_data(model, data, db): return item -# FIXME db.id has to be replaced with the table's UID def delete_data(model, data, db): - item = instantiate_model(model=model, data=data) - result = db.query(item).filter(item.email == data.email).delete() - return result + table = eval(model) + primary_key_tuple = inspect(table).primary_key + primary_key = primary_key_tuple[0] + db.query(table).filter(primary_key == data).delete() + db.commit() def fetch_user_by_key(data, db): diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..b29bbdf --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,14 @@ +from pytest import fixture + +from database.crud import delete_data +from tests import TestingSessionLocal + + +@fixture(scope="session") +def get_test_db(): + try: + db = TestingSessionLocal() + yield db + finally: + delete_data(model="Users", data=2222, db=db) + db.close() diff --git a/tests/queries_test.py b/tests/queries_test.py index 3921a45..7477c0b 100644 --- a/tests/queries_test.py +++ b/tests/queries_test.py @@ -1,18 +1,6 @@ from datetime import datetime -from pytest import fixture - from database.models import * -from tests import TestingSessionLocal - - -@fixture -def get_test_db(): - try: - db = TestingSessionLocal() - yield db - finally: - db.close() def test_users(get_test_db): diff --git a/tests/requests_test.py b/tests/requests_test.py index adcacff..2f48567 100644 --- a/tests/requests_test.py +++ b/tests/requests_test.py @@ -1,10 +1,10 @@ from secrets import token_hex + from pytest import main from app.schemas import * from database.models import * from tests import client -from tests.queries_test import get_test_db def test_registration(): @@ -55,8 +55,8 @@ def test_forgot_password(): assert response.status_code == 200 -def test_reset_password(): - main(["-k" "test_otp_verification"]) +def test_reset_password(get_test_db): + test_otp_verification(get_test_db) data = {"email": "oyvey@hotmail.com", "password": "vivashviva"} response = client.post("/reset_password", json=data) assert response.status_code == 200