52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
import pytest
|
|
from datetime import datetime
|
|
from bson import ObjectId
|
|
from src.db.models.team import TeamModel, PyObjectId
|
|
|
|
class TestTeamModel:
|
|
def test_create_team(self):
|
|
"""Test creating a team with valid data"""
|
|
team_data = {
|
|
"name": "Test Team",
|
|
"description": "This is a test team"
|
|
}
|
|
team = TeamModel(**team_data)
|
|
|
|
assert team.name == "Test Team"
|
|
assert team.description == "This is a test team"
|
|
assert isinstance(team.id, PyObjectId)
|
|
assert isinstance(team.created_at, datetime)
|
|
assert team.updated_at is None
|
|
|
|
def test_create_team_with_id(self):
|
|
"""Test creating a team with a predefined ID"""
|
|
object_id = ObjectId()
|
|
team_data = {
|
|
"_id": object_id,
|
|
"name": "Test Team"
|
|
}
|
|
team = TeamModel(**team_data)
|
|
|
|
assert team.id == object_id
|
|
assert team.name == "Test Team"
|
|
assert team.description is None
|
|
|
|
def test_team_dict_conversion(self):
|
|
"""Test the team model can be converted to dict with correct field mapping"""
|
|
team = TeamModel(name="Test Team")
|
|
team_dict = team.model_dump(by_alias=True)
|
|
|
|
assert "_id" in team_dict
|
|
assert "name" in team_dict
|
|
assert team_dict["name"] == "Test Team"
|
|
|
|
def test_py_object_id_validation(self):
|
|
"""Test the PyObjectId validation logic"""
|
|
# Valid ObjectId
|
|
valid_id = str(ObjectId())
|
|
obj_id = PyObjectId.validate(valid_id)
|
|
assert isinstance(obj_id, ObjectId)
|
|
|
|
# Invalid ObjectId
|
|
with pytest.raises(ValueError, match='Invalid ObjectId'):
|
|
PyObjectId.validate("invalid-id") |