sio-2425/delivery1/server/tests/test_db.py

136 lines
3.3 KiB
Python
Raw Normal View History

from pprint import PrettyPrinter
import database.setup_db
from database import SessionLocal
from services import OrganizationService, UserService
database.setup_db.setup_db()
db = SessionLocal()
org_service = OrganizationService(db)
user_service = UserService(db)
def test_create_organization():
org = org_service.create_organization(
name="Test Org",
username="testuser",
full_name="Test User",
email="test@mail.com",
public_key="abc123"
)
assert org.name == "Test Org"
assert org.owner.username == "testuser"
assert org.owner.full_name == "Test User"
assert org.owner.email == "test@mail.com"
assert org.owner.public_key == "abc123"
assert org.owner.orgs[0].name == "Test Org"
db.delete(org.owner)
db.delete(org)
db.commit()
def test_create_user():
user = user_service.create_user(
username="testuser",
full_name="Test User",
email="test@mail.com",
public_key="abc123"
)
assert user.username == "testuser"
assert user.full_name == "Test User"
assert user.email == "test@mail.com"
assert user.public_key == "abc123"
db.delete(user)
db.commit()
def test_get_organization():
org = org_service.create_organization(
name="Test Org",
username="testuser",
full_name="Test User",
email="test@mail.com",
public_key="abc123"
)
org2 = org_service.get_organization(org.id)
org3 = org_service.get_organization_by_name("Test Org")
assert org2.name == "Test Org"
assert org3.name == "Test Org"
assert org2.owner.username == "testuser"
assert org3.owner.username == "testuser"
db.delete(org.owner)
db.delete(org)
db.commit()
def test_list_organizations():
org = org_service.create_organization(
name="Test Org",
username="testuser",
full_name="Test User",
email="test@mail.com",
public_key="abc123"
)
org2 = org_service.create_organization(
name="Test Org2",
username="testuser2",
full_name="Test User2",
email="mail2@test.com",
public_key="def456"
)
orgs = org_service.list_organizations()
assert len(orgs) == 2
assert orgs[0].name == "Test Org"
assert orgs[1].name == "Test Org2"
assert orgs == [org, org2]
db.delete(org.owner)
db.delete(org)
db.delete(org2.owner)
db.delete(org2)
db.commit()
def test_get_user():
user = user_service.create_user(
username="testuser",
full_name="Test User",
email="test@mail.com",
public_key="abc123"
)
user2 = user_service.get_user(user.id)
user3 = user_service.get_user_by_username("testuser")
assert user2.username == "testuser"
assert user3.username == "testuser"
assert user2.full_name == "Test User"
assert user3.full_name == "Test User"
assert user2.email == "test@mail.com"
assert user3.email == "test@mail.com"
assert user2.public_key == "abc123"
assert user3.public_key == "abc123"
org = org_service.create_organization(
name="Test Org",
username="testuser",
full_name="Test User",
email="test@mail.com",
public_key="abc123"
)
user4 = user_service.get_user(user.id)
assert user4.orgs[0].name == "Test Org"
db.delete(org)
db.delete(user)
db.commit()