File size: 23,147 Bytes
cc036ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | """
Comprehensive core model tests covering CRUD, relationships, constraints, and cascade behaviors.
Goal: Achieve 80%+ coverage for core models (Workspace, Team, Tenant, UserAccount,
OAuthToken, ChatSession, ChatMessage) through comprehensive testing of:
- CRUD operations (Create, Read, Update, Delete)
- Relationship types (one-to-many, many-to-many, foreign keys)
- Constraints (unique, not null, foreign keys)
- Cascade behaviors (delete, nullify)
- JSON field serialization
- Timestamp auto-generation
Tests use:
- pytest fixtures for database sessions (db_session from conftest.py)
- Factory pattern for test data creation (factories in tests/factories/)
"""
import pytest
from datetime import datetime, timedelta
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
from tests.factories.user_factory import UserFactory, AdminUserFactory
from tests.factories.workspace_factory import WorkspaceFactory, TeamFactory
from tests.factories.core_factory import (
TenantFactory,
UserAccountFactory,
OAuthTokenFactory,
ChatMessageFactory,
)
from tests.factories.chat_session_factory import ChatSessionFactory
from core.models import (
Workspace,
Team,
Tenant,
UserAccount,
OAuthToken,
ChatSession,
ChatMessage,
User,
WorkspaceStatus,
PlanType,
)
# ============================================================================
# Task 2: Workspace Models Tests
# ============================================================================
class TestWorkspaceModels:
"""Test Workspace model CRUD, relationships, and properties."""
def test_workspace_create_with_defaults(self, db_session: Session):
"""Test Workspace creation with minimal required fields."""
workspace = Workspace(
name="Test Workspace",
)
db_session.add(workspace)
db_session.commit()
db_session.refresh(workspace)
assert workspace.id is not None
assert workspace.name == "Test Workspace"
assert workspace.status == WorkspaceStatus.ACTIVE.value
assert workspace.plan_tier == "standard"
assert workspace.is_startup is False
assert workspace.learning_phase_completed is False
assert workspace.metadata_json == {}
assert workspace.created_at is not None
# Note: updated_at is None on creation, only set on update
def test_workspace_create_with_all_fields(self, db_session: Session):
"""Test Workspace creation with all optional fields."""
workspace = Workspace(
name="Full Workspace",
description="A workspace with all fields",
status=WorkspaceStatus.ACTIVE.value,
plan_tier="enterprise",
is_startup=True,
learning_phase_completed=True,
metadata_json={"theme": "dark", "language": "en"},
internal_domains='["atom.ai", "example.com"]',
)
db_session.add(workspace)
db_session.commit()
db_session.refresh(workspace)
assert workspace.name == "Full Workspace"
assert workspace.description == "A workspace with all fields"
assert workspace.plan_tier == "enterprise"
assert workspace.is_startup is True
assert workspace.learning_phase_completed is True
assert workspace.metadata_json["theme"] == "dark"
assert workspace.internal_domains == '["atom.ai", "example.com"]'
def test_workspace_tenant_relationship(self, db_session: Session):
"""Test Workspace belongs to Tenant (via tenant_id)."""
tenant = TenantFactory(_session=db_session)
db_session.commit()
workspace = Workspace(
name="Tenant Workspace",
tenant_id=tenant.id,
)
db_session.add(workspace)
db_session.commit()
db_session.refresh(workspace)
assert workspace.tenant_id == tenant.id
def test_workspace_teams_relationship(self, db_session: Session):
"""Test Workspace has many Teams (one-to-many)."""
workspace = WorkspaceFactory(_session=db_session)
db_session.commit()
team1 = TeamFactory(workspace_id=workspace.id, _session=db_session)
team2 = TeamFactory(workspace_id=workspace.id, _session=db_session)
db_session.commit()
retrieved_workspace = db_session.query(Workspace).filter(
Workspace.id == workspace.id
).first()
assert len(retrieved_workspace.teams) == 2
team_ids = [t.id for t in retrieved_workspace.teams]
assert team1.id in team_ids
assert team2.id in team_ids
def test_workspace_users_relationship(self, db_session: Session):
"""Test Workspace has many Users (many-to-many via user_workspaces)."""
workspace = WorkspaceFactory(_session=db_session)
db_session.commit()
user1 = UserFactory(email="user1@test.com", _session=db_session)
user2 = UserFactory(email="user2@test.com", _session=db_session)
workspace.users.append(user1)
workspace.users.append(user2)
db_session.commit()
retrieved_workspace = db_session.query(Workspace).filter(
Workspace.id == workspace.id
).first()
assert len(retrieved_workspace.users) == 2
def test_workspace_is_startup_property(self, db_session: Session):
"""Test Workspace is_startup boolean property."""
workspace1 = Workspace(name="Startup Workspace", is_startup=True)
workspace2 = Workspace(name="Corporate Workspace", is_startup=False)
db_session.add(workspace1)
db_session.add(workspace2)
db_session.commit()
assert workspace1.is_startup is True
assert workspace2.is_startup is False
def test_workspace_metadata_json_field(self, db_session: Session):
"""Test Workspace metadata_json field serialization."""
metadata = {
"theme": "dark",
"language": "en",
"notifications": {"email": True, "push": False}
}
workspace = Workspace(
name="Metadata Workspace",
metadata_json=metadata,
)
db_session.add(workspace)
db_session.commit()
db_session.refresh(workspace)
assert isinstance(workspace.metadata_json, dict)
assert workspace.metadata_json["theme"] == "dark"
assert workspace.metadata_json["notifications"]["email"] is True
class TestTeamModels:
"""Test Team model CRUD, relationships, and constraints."""
def test_team_create_with_defaults(self, db_session: Session):
"""Test Team creation with workspace."""
workspace = WorkspaceFactory(_session=db_session)
db_session.commit()
team = Team(
name="Engineering Team",
workspace_id=workspace.id,
)
db_session.add(team)
db_session.commit()
db_session.refresh(team)
assert team.id is not None
assert team.name == "Engineering Team"
assert team.workspace_id == workspace.id
assert team.created_at is not None
# Note: updated_at is None on creation, only set on update
def test_team_members_relationship(self, db_session: Session):
"""Test Team has many Users (many-to-many via team_members)."""
workspace = WorkspaceFactory(_session=db_session)
team = TeamFactory(workspace_id=workspace.id, _session=db_session)
db_session.commit()
user1 = UserFactory(email="member1@test.com", _session=db_session)
user2 = UserFactory(email="member2@test.com", _session=db_session)
team.members.append(user1)
team.members.append(user2)
db_session.commit()
retrieved_team = db_session.query(Team).filter(Team.id == team.id).first()
assert len(retrieved_team.members) == 2
# Verify reverse relationship
retrieved_user1 = db_session.query(User).filter(User.id == user1.id).first()
assert len(retrieved_user1.teams) == 1
assert retrieved_user1.teams[0].id == team.id
def test_team_workspace_relationship(self, db_session: Session):
"""Test Team belongs to Workspace (many-to-one)."""
workspace = WorkspaceFactory(_session=db_session)
team = TeamFactory(workspace_id=workspace.id, _session=db_session)
db_session.commit()
retrieved_team = db_session.query(Team).filter(Team.id == team.id).first()
assert retrieved_team.workspace.id == workspace.id
assert retrieved_team.workspace.name == workspace.name
# ============================================================================
# Task 2: Tenant Models Tests
# ============================================================================
class TestTenantModels:
"""Test Tenant model CRUD, properties, and relationships."""
def test_tenant_create_with_defaults(self, db_session: Session):
"""Test Tenant creation with required fields."""
tenant = Tenant(
name="Test Tenant",
subdomain="testtenant",
)
db_session.add(tenant)
db_session.commit()
db_session.refresh(tenant)
assert tenant.id is not None
assert tenant.name == "Test Tenant"
assert tenant.subdomain == "testtenant"
assert tenant.plan_type == PlanType.FREE.value
assert tenant.edition == "personal"
assert tenant.memory_limit_mb == 50
assert tenant.is_active is True
assert tenant.created_at is not None
def test_tenant_edition_properties(self, db_session: Session):
"""Test Tenant edition properties (is_personal, is_enterprise)."""
personal_tenant = Tenant(
name="Personal Tenant",
subdomain="personal",
edition="personal",
)
enterprise_tenant = Tenant(
name="Enterprise Tenant",
subdomain="enterprise",
edition="enterprise",
)
db_session.add(personal_tenant)
db_session.add(enterprise_tenant)
db_session.commit()
assert personal_tenant.is_personal is True
assert personal_tenant.is_enterprise is False
assert enterprise_tenant.is_personal is False
assert enterprise_tenant.is_enterprise is True
def test_tenant_edition_display_name(self, db_session: Session):
"""Test Tenant edition_display_name property."""
personal_tenant = Tenant(
name="Personal",
subdomain="personal",
edition="personal",
)
enterprise_tenant = Tenant(
name="Enterprise",
subdomain="enterprise",
edition="enterprise",
)
db_session.add(personal_tenant)
db_session.add(enterprise_tenant)
db_session.commit()
assert personal_tenant.edition_display_name == "Personal Edition"
assert enterprise_tenant.edition_display_name == "Enterprise Edition"
def test_tenant_can_upgrade_method(self, db_session: Session):
"""Test Tenant can_upgrade_to_enterprise() method."""
personal_tenant = Tenant(
name="Personal",
subdomain="personal",
edition="personal",
)
enterprise_tenant = Tenant(
name="Enterprise",
subdomain="enterprise",
edition="enterprise",
)
db_session.add(personal_tenant)
db_session.add(enterprise_tenant)
db_session.commit()
assert personal_tenant.can_upgrade_to_enterprise() is True
assert enterprise_tenant.can_upgrade_to_enterprise() is False
def test_tenant_budget_fields(self, db_session: Session):
"""Test Tenant budget tracking fields."""
tenant = Tenant(
name="Budget Tenant",
subdomain="budget",
budget_limit_usd=500.0,
current_spend_usd=150.0,
total_spend_usd=1000.0,
)
db_session.add(tenant)
db_session.commit()
db_session.refresh(tenant)
assert tenant.budget_limit_usd == 500.0
assert tenant.current_spend_usd == 150.0
assert tenant.total_spend_usd == 1000.0
# ============================================================================
# Task 2: UserAccount Models Tests
# ============================================================================
class TestUserAccountModels:
"""Test UserAccount model for IM platform linking."""
def test_user_account_create(self, db_session: Session):
"""Test UserAccount creation for IM platform linking."""
user = UserFactory(_session=db_session)
tenant = TenantFactory(_session=db_session)
db_session.commit()
user_account = UserAccount(
user_id=user.id,
tenant_id=tenant.id,
platform="slack",
platform_user_id="U12345",
chat_id="C12345",
username="testuser",
)
db_session.add(user_account)
db_session.commit()
db_session.refresh(user_account)
assert user_account.id is not None
assert user_account.user_id == user.id
assert user_account.tenant_id == tenant.id
assert user_account.platform == "slack"
assert user_account.platform_user_id == "U12345"
assert user_account.chat_id == "C12345"
assert user_account.username == "testuser"
assert user_account.is_active is True
assert user_account.linked_at is not None
def test_user_account_unique_constraint(self, db_session: Session):
"""Test UserAccount unique constraint on (platform, platform_user_id)."""
user = UserFactory(_session=db_session)
tenant = TenantFactory(_session=db_session)
db_session.commit()
# Create first UserAccount
user_account1 = UserAccount(
user_id=user.id,
tenant_id=tenant.id,
platform="slack",
platform_user_id="U12345",
)
db_session.add(user_account1)
db_session.commit()
# Try to create duplicate (same platform + platform_user_id)
with pytest.raises(IntegrityError):
user_account2 = UserAccount(
user_id=user.id,
tenant_id=tenant.id,
platform="slack",
platform_user_id="U12345", # Same platform_user_id
)
db_session.add(user_account2)
db_session.commit()
db_session.rollback()
def test_user_account_user_relationship(self, db_session: Session):
"""Test UserAccount belongs to User."""
user = UserFactory(_session=db_session)
tenant = TenantFactory(_session=db_session)
db_session.commit()
user_account = UserAccount(
user_id=user.id,
tenant_id=tenant.id,
platform="discord",
)
db_session.add(user_account)
db_session.commit()
assert user_account.user.id == user.id
assert user_account.user.email == user.email
def test_user_account_tenant_relationship(self, db_session: Session):
"""Test UserAccount belongs to Tenant."""
user = UserFactory(_session=db_session)
tenant = TenantFactory(_session=db_session)
db_session.commit()
user_account = UserAccount(
user_id=user.id,
tenant_id=tenant.id,
platform="teams",
)
db_session.add(user_account)
db_session.commit()
assert user_account.tenant.id == tenant.id
assert user_account.tenant.name == tenant.name
# ============================================================================
# Task 2: OAuthToken Models Tests
# ============================================================================
class TestOAuthTokenModels:
"""Test OAuthToken model for OAuth token management."""
def test_oauth_token_create(self, db_session: Session):
"""Test OAuthToken creation with provider."""
user = UserFactory(_session=db_session)
tenant = TenantFactory(_session=db_session)
db_session.commit()
oauth_token = OAuthToken(
client_id="client_123",
user_id=user.id,
tenant_id=tenant.id,
access_token_hash="hash_" + "a" * 58,
scope="read write",
token_type="Bearer",
access_token_expires_at=datetime.utcnow() + timedelta(hours=1),
)
db_session.add(oauth_token)
db_session.commit()
db_session.refresh(oauth_token)
assert oauth_token.id is not None
assert oauth_token.user_id == user.id
assert oauth_token.tenant_id == tenant.id
assert oauth_token.access_token_hash.startswith("hash_")
assert oauth_token.scope == "read write"
assert oauth_token.token_type == "Bearer"
assert oauth_token.access_token_expires_at is not None
def test_oauth_token_user_relationship(self, db_session: Session):
"""Test OAuthToken belongs to User."""
user = UserFactory(_session=db_session)
tenant = TenantFactory(_session=db_session)
db_session.commit()
oauth_token = OAuthToken(
client_id="client_123",
user_id=user.id,
tenant_id=tenant.id,
access_token_hash="hash_" + "a" * 58,
scope="read",
token_type="Bearer",
access_token_expires_at=datetime.utcnow() + timedelta(hours=1),
)
db_session.add(oauth_token)
db_session.commit()
# Note: OAuthToken model may not have a 'user' relationship defined
# This test verifies the foreign key relationship works
assert oauth_token.user_id == user.id
def test_oauth_token_expires_at_field(self, db_session: Session):
"""Test OAuthToken datetime handling for expiration."""
user = UserFactory(_session=db_session)
tenant = TenantFactory(_session=db_session)
expires_at = datetime.utcnow() + timedelta(hours=2)
db_session.commit()
oauth_token = OAuthToken(
client_id="client_123",
user_id=user.id,
tenant_id=tenant.id,
access_token_hash="hash_" + "a" * 58,
scope="read",
token_type="Bearer",
access_token_expires_at=expires_at,
)
db_session.add(oauth_token)
db_session.commit()
db_session.refresh(oauth_token)
assert oauth_token.access_token_expires_at is not None
# Allow for small time differences during test execution
time_diff = abs((oauth_token.access_token_expires_at - expires_at).total_seconds())
assert time_diff < 5 # Less than 5 seconds difference
# ============================================================================
# Task 2: Chat Models Tests
# ============================================================================
class TestChatModels:
"""Test ChatSession and ChatMessage models."""
def test_chat_session_create(self, db_session: Session):
"""Test ChatSession creation."""
chat_session = ChatSessionFactory(_session=db_session)
db_session.commit()
db_session.refresh(chat_session)
assert chat_session.id is not None
assert chat_session.user_id is not None
assert chat_session.title is not None
assert chat_session.created_at is not None
assert chat_session.updated_at is not None
assert chat_session.message_count >= 0
def test_chat_session_anonymous(self, db_session: Session):
"""Test ChatSession can be created without user (anonymous)."""
# Note: ChatSession model requires user_id (nullable=False in schema)
# This test verifies the model constraint
with pytest.raises(IntegrityError):
chat_session = ChatSession(
id=None, # Required field
user_id=None, # Should fail based on model schema
)
db_session.add(chat_session)
db_session.commit()
db_session.rollback()
def test_chat_session_messages_relationship(self, db_session: Session):
"""Test ChatSession has many Messages (one-to-many)."""
chat_session = ChatSessionFactory(_session=db_session)
db_session.commit()
db_session.refresh(chat_session)
# Note: ChatMessage model uses conversation_id, not session_id
# This test verifies the relationship structure
message1 = ChatMessageFactory(
conversation_id=chat_session.id,
role="user",
content="Hello",
_session=db_session,
)
message2 = ChatMessageFactory(
conversation_id=chat_session.id,
role="assistant",
content="Hi there!",
_session=db_session,
)
db_session.commit()
# Verify messages were created with the conversation_id
messages = db_session.query(ChatMessage).filter(
ChatMessage.conversation_id == chat_session.id
).all()
assert len(messages) == 2
def test_chat_message_create(self, db_session: Session):
"""Test ChatMessage creation with role and content."""
message = ChatMessageFactory(
role="user",
content="Test message",
_session=db_session,
)
db_session.commit()
db_session.refresh(message)
assert message.id is not None
assert message.role == "user"
assert message.content == "Test message"
assert message.created_at is not None
assert message.conversation_id is not None
assert message.tenant_id is not None
def test_chat_message_conversation_relationship(self, db_session: Session):
"""Test ChatMessage belongs to conversation (via conversation_id)."""
chat_session = ChatSessionFactory(_session=db_session)
db_session.commit()
message = ChatMessageFactory(
conversation_id=chat_session.id,
role="assistant",
content="Response",
_session=db_session,
)
db_session.commit()
assert message.conversation_id == chat_session.id
def test_chat_message_metadata_json(self, db_session: Session):
"""Test ChatMessage metadata_json field handling."""
metadata = '{"tokens": 150, "model": "gpt-4", "latency_ms": 250}'
message = ChatMessage(
conversation_id="conversation_123",
tenant_id="tenant_123",
role="assistant",
content="Test",
metadata_json=metadata, # Stored as Text
)
db_session.add(message)
db_session.commit()
db_session.refresh(message)
assert message.metadata_json is not None
# Note: metadata_json is stored as Text, not JSON type
assert isinstance(message.metadata_json, str)
assert '"tokens": 150' in message.metadata_json
|