import pytest from datetime import datetime from bson import ObjectId from pydantic import ValidationError from src.db.models.user import UserModel from src.db.models.team import PyObjectId class TestUserModel: def test_create_user(self): """Test creating a user with valid data""" team_id = ObjectId() user_data = { "email": "test@example.com", "name": "Test User", "team_id": team_id } user = UserModel(**user_data) assert user.email == "test@example.com" assert user.name == "Test User" assert user.team_id == team_id assert user.is_active is True assert user.is_admin is False assert isinstance(user.created_at, datetime) assert user.updated_at is None assert user.last_login is None def test_user_with_invalid_email(self): """Test that invalid email addresses are rejected""" with pytest.raises(ValidationError): UserModel( email="not-an-email", name="Test User", team_id=ObjectId() ) def test_user_with_missing_required_fields(self): """Test that required fields must be provided""" # Missing email with pytest.raises(ValidationError): UserModel(name="Test User", team_id=ObjectId()) # Missing name with pytest.raises(ValidationError): UserModel(email="test@example.com", team_id=ObjectId()) # Missing team_id with pytest.raises(ValidationError): UserModel(email="test@example.com", name="Test User") def test_user_dict_conversion(self): """Test the user model can be converted to dict with correct field mapping""" team_id = ObjectId() user = UserModel( email="test@example.com", name="Test User", team_id=team_id ) user_dict = user.model_dump(by_alias=True) assert "_id" in user_dict assert "email" in user_dict assert "name" in user_dict assert "team_id" in user_dict assert str(user_dict["team_id"]) == str(team_id)