2022-11-06 20:09:44 +08:00
|
|
|
from typing import Any, Generator
|
2022-11-20 21:20:41 +08:00
|
|
|
import logging
|
|
|
|
import asyncio
|
2022-03-30 08:04:44 +08:00
|
|
|
import pytest_asyncio
|
2022-11-06 20:09:44 +08:00
|
|
|
from httpx import AsyncClient
|
|
|
|
from sqlalchemy import create_engine
|
2022-11-20 21:20:41 +08:00
|
|
|
from faker import Faker
|
|
|
|
import random
|
2022-03-30 08:04:44 +08:00
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
from felicity.apps.job.sched import felicity_workforce_init
|
|
|
|
from felicity.main import flims
|
|
|
|
from felicity.core.config import settings
|
|
|
|
from felicity.database.base_class import DBModel
|
|
|
|
from felicity.init.setup.create_superuser import create_super_user
|
2022-03-30 08:04:44 +08:00
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
fake_engine = Faker()
|
2022-03-30 08:04:44 +08:00
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
sync_engine = create_engine(settings.SQLALCHEMY_TEST_DATABASE_URI)
|
2022-03-30 08:04:44 +08:00
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
logger.info(f"pytest_configure ...")
|
|
|
|
if settings.RETAIN_TESTING_DB_DATA:
|
|
|
|
DBModel.metadata.drop_all(bind=sync_engine)
|
|
|
|
DBModel.metadata.create_all(bind=sync_engine)
|
|
|
|
# start scheduler
|
|
|
|
felicity_workforce_init()
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
def pytest_unconfigure(config):
|
|
|
|
logger.info(f"pytest_un_configure ...")
|
|
|
|
if not settings.RETAIN_TESTING_DB_DATA:
|
|
|
|
DBModel.metadata.drop_all(bind=sync_engine)
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
|
|
|
def event_loop():
|
|
|
|
"""Overrides pytest default function scoped event loop"""
|
|
|
|
# return asyncio.get_event_loop()
|
|
|
|
policy = asyncio.get_event_loop_policy()
|
|
|
|
loop = policy.new_event_loop()
|
|
|
|
yield loop
|
|
|
|
loop.close()
|
2022-03-30 08:04:44 +08:00
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="session", autouse=True)
|
|
|
|
async def initialise():
|
|
|
|
logger.info(f"init_db_add_super_user start")
|
|
|
|
await create_super_user()
|
|
|
|
yield
|
|
|
|
logger.info(f"init_db_add_super_user teardown")
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
2022-11-20 21:20:41 +08:00
|
|
|
async def client() -> Generator[AsyncClient, Any, None]:
|
|
|
|
async with AsyncClient(app=flims, base_url="http://localhost:8080/api/v1") as clt:
|
|
|
|
yield clt
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
|
2022-11-20 21:20:41 +08:00
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
|
|
async def gql_client() -> Generator[AsyncClient, Any, None]:
|
|
|
|
async with AsyncClient(app=flims, base_url="http://localhost:8080") as clt:
|
|
|
|
yield clt
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(autouse=True)
|
|
|
|
async def auth_data(client):
|
|
|
|
superuser = {"username": settings.FIRST_SEPERUSER_USERNAME, "password": settings.FIRST_SUPERUSER_PASSWORD}
|
|
|
|
response = await client.post("/login/access-token", data=superuser)
|
|
|
|
return {
|
|
|
|
"token": response.json()["access_token"],
|
|
|
|
"headers": {
|
|
|
|
"Authorization": f"bearer {response.json()['access_token']}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(autouse=True)
|
|
|
|
async def users():
|
|
|
|
return [
|
|
|
|
{'firstName': "Daniel", 'lastName': "Diesel", 'email': f"daniel@felcity.com", 'openReg': False},
|
|
|
|
{'firstName': "Brian", 'lastName': "Moyo", 'email': f"brian@felcity.com", 'openReg': False},
|
|
|
|
{'firstName': "Teddy", 'lastName': "Estat", 'email': f"teddy@felcity.com", 'openReg': False},
|
|
|
|
{'firstName': "Samantha", 'lastName': "Mapako", 'email': f"samantha@felcity.com", 'openReg': False},
|
|
|
|
{'firstName': "Peter", 'lastName': "Tosh", 'email': f"peter@felcity.com", 'openReg': False}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(autouse=True)
|
|
|
|
async def patients():
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'payload': {
|
|
|
|
'clientPatientId': fake_engine.ssn(),
|
|
|
|
'firstName': fake_engine.first_name(),
|
|
|
|
'middleName': fake_engine.first_name(),
|
|
|
|
'lastName': fake_engine.last_name(),
|
|
|
|
'age': random.randint(1, 90),
|
|
|
|
'gender': random.choice([1, 2, 3]),
|
|
|
|
'dateOfBirth': str(fake_engine.date_time()),
|
|
|
|
'ageDobEstimated': fake_engine.boolean(),
|
|
|
|
'clientUid': random.randint(1, 2),
|
|
|
|
'phoneMobile': fake_engine.phone_number(),
|
|
|
|
'phoneHome': fake_engine.phone_number(),
|
|
|
|
'consentSms': fake_engine.boolean(),
|
|
|
|
}
|
|
|
|
} for i in range(1)
|
|
|
|
]
|