mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init unit-testing
This commit is contained in:
parent
0a2c5526bc
commit
1c86a829b0
6 changed files with 112 additions and 3 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
50
apps/api/src/tests/test_main.py
Normal file
50
apps/api/src/tests/test_main.py
Normal 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
|
||||
0
apps/api/src/tests/test_rbac.py
Normal file
0
apps/api/src/tests/test_rbac.py
Normal file
0
apps/api/src/tests/utils/__init__.py
Normal file
0
apps/api/src/tests/utils/__init__.py
Normal file
57
apps/api/src/tests/utils/init_data_for_tests.py
Normal file
57
apps/api/src/tests/utils/init_data_for_tests.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue