49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.core.database import Base
|
|
from app.modules.auth.models import User
|
|
|
|
|
|
@pytest.fixture
|
|
def db():
|
|
engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
|
|
Base.metadata.create_all(bind=engine)
|
|
Session = sessionmaker(bind=engine)
|
|
session = Session()
|
|
yield session
|
|
session.close()
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
def test_create_user(db):
|
|
user = User(username="testuser", full_name="Test User", pw_hash="hashed")
|
|
db.add(user)
|
|
db.commit()
|
|
db.refresh(user)
|
|
assert user.id is not None
|
|
assert user.username == "testuser"
|
|
assert user.is_active is True
|
|
assert user.is_admin is False
|
|
assert user.created_at is not None
|
|
|
|
|
|
def test_user_defaults(db):
|
|
user = User(username="alice", full_name="Alice")
|
|
db.add(user)
|
|
db.commit()
|
|
db.refresh(user)
|
|
assert user.pw_hash is None
|
|
assert user.email is None
|
|
assert user.department is None
|
|
assert user.last_login is None
|
|
|
|
|
|
def test_username_must_be_unique(db):
|
|
db.add(User(username="dup", full_name="First"))
|
|
db.commit()
|
|
db.add(User(username="dup", full_name="Second"))
|
|
with pytest.raises(Exception):
|
|
db.commit()
|