File size: 2,428 Bytes
14e0494 | 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 | import pytest
from datetime import datetime
from backend.src.models.task import Task, TaskCreate, TaskRead, TaskUpdate
def test_task_creation():
"""Test creating a task with valid data."""
task_data = {
"title": "Test Task",
"description": "This is a test task",
"completed": False,
"user_id": 1
}
task_create = TaskCreate(**task_data)
assert task_create.title == "Test Task"
assert task_create.description == "This is a test task"
assert task_create.completed is False
assert task_create.user_id == 1
def test_task_creation_required_fields():
"""Test that required fields are enforced."""
with pytest.raises(ValueError):
TaskCreate(title="", user_id=1) # Empty title should fail
with pytest.raises(ValueError):
TaskCreate(title="Test Task") # Missing user_id should fail
def test_task_creation_title_length_validation():
"""Test title length validation."""
# Title too long should fail
long_title = "t" * 256
with pytest.raises(ValueError):
TaskCreate(title=long_title, user_id=1)
# Title at max length should pass
max_title = "t" * 255
task = TaskCreate(title=max_title, user_id=1)
assert len(task.title) == 255
def test_task_update_partial():
"""Test that TaskUpdate allows partial updates."""
# All fields should be optional in TaskUpdate
update_data = {}
task_update = TaskUpdate(**update_data)
assert task_update.title is None
assert task_update.description is None
assert task_update.completed is None
def test_task_update_with_values():
"""Test updating with specific values."""
update_data = {
"title": "Updated Title",
"completed": True
}
task_update = TaskUpdate(**update_data)
assert task_update.title == "Updated Title"
assert task_update.completed is True
assert task_update.description is None
def test_task_read_serialization():
"""Test TaskRead model serialization."""
task_read = TaskRead(
id=1,
title="Test Task",
description="Description",
completed=False,
user_id=1,
created_at=datetime.now(),
updated_at=datetime.now()
)
assert task_read.id == 1
assert task_read.title == "Test Task"
assert task_read.description == "Description"
assert task_read.completed is False
assert task_read.user_id == 1 |