feat: init unit-testing

This commit is contained in:
swve 2023-12-22 16:27:36 +01:00
parent 0a2c5526bc
commit 1c86a829b0
6 changed files with 112 additions and 3 deletions

View file

@ -11,6 +11,8 @@ botocore
python-jose
passlib
fastapi-jwt-auth
pytest
httpx
faker
requests
pyyaml

View file

@ -93,7 +93,7 @@ async def update_install_instance(
# Install Default roles
async def install_default_elements(request: Request, data: dict, db_session: Session):
async def install_default_elements( data: dict, db_session: Session):
# remove all default roles
statement = select(Role).where(Role.role_type == RoleTypeEnum.TYPE_GLOBAL)
roles = db_session.exec(statement).all()
@ -279,7 +279,7 @@ async def install_default_elements(request: Request, data: dict, db_session: Ses
# Organization creation
async def install_create_organization(
request: Request, org_object: OrganizationCreate, db_session: Session
org_object: OrganizationCreate, db_session: Session
):
org = Organization.from_orm(org_object)
@ -296,7 +296,7 @@ async def install_create_organization(
async def install_create_organization_user(
request: Request, user_object: UserCreate, org_slug: str, db_session: Session
user_object: UserCreate, org_slug: str, db_session: Session
):
user = User.from_orm(user_object)

View file

@ -0,0 +1,50 @@
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool
from sqlmodel import SQLModel, Session
from src.tests.utils.init_data_for_tests import create_initial_data_for_tests
from src.core.events.database import get_db_session
import pytest
import asyncio
from app import app
client = TestClient(app)
# TODO : fix this later https://stackoverflow.com/questions/10253826/path-issue-with-pytest-importerror-no-module-named
@pytest.fixture(name="session", scope="session")
def session_fixture():
engine = create_engine(
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
yield session
@pytest.fixture(name="client")
def client_fixture(session: Session):
def get_session_override():
return session
app.dependency_overrides[get_db_session] = get_session_override
client = TestClient(app)
yield client
app.dependency_overrides.clear()
@pytest.fixture(scope="session", autouse=True)
def execute_before_all_tests(session: Session):
# This function will run once before all tests.
asyncio.run(create_initial_data_for_tests(session))
def test_create_default_elements(client: TestClient, session: Session):
response = client.get(
"/api/v1/orgs/slug/wayne",
)
assert response.status_code == 200

View file

View file

View file

@ -0,0 +1,57 @@
from sqlmodel import Session, select
from src.db.user_organizations import UserOrganization
from src.db.organizations import OrganizationCreate
from src.db.users import User, UserCreate
from src.services.install.install import (
install_create_organization,
install_create_organization_user,
install_default_elements,
)
async def create_initial_data_for_tests(db_session: Session):
# Install default elements
await install_default_elements({}, db_session)
# Initiate test Organization
test_org = OrganizationCreate(
name="Wayne Enterprises",
description=None,
slug="wayne",
email="hello@wayne.dev",
logo_image=None,
)
# Create test organization
await install_create_organization(test_org, db_session)
users = [
UserCreate(
username="batman",
first_name="Bruce",
last_name="Wayne",
email="bruce@wayne.com",
password="imbatman",
),
UserCreate(
username="robin",
first_name="Richard John",
last_name="Grayson",
email="robin@wayne.com",
password="secret",
),
]
# Create 2 users in that Organization
for user in users:
await install_create_organization_user(user, "wayne", db_session)
# Make robin a normal user
statement = select(UserOrganization).join(User).where(User.username == "robin")
user_org = db_session.exec(statement).first()
user_org.role_id = 3 # type: ignore
db_session.add(user_org)
db_session.commit()
return True