File size: 5,031 Bytes
310260a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Unit Tests for delete_task MCP Tool

Tests the delete_task tool functionality including:
- Removes task from database
- Error for non-existent task_id
- Task ownership validation
- Returns deleted task details
"""

import pytest

from src.tools.delete_task import delete_task_internal
from tests.utils.task_helpers import create_test_task, get_task_by_id, count_tasks


@pytest.mark.unit
@pytest.mark.asyncio
async def test_delete_task_removes_task_from_database(mock_mcp_context, test_session):
    """
    Test: delete_task removes task from database

    Verifies that delete_task permanently removes the task.
    """
    # Setup
    task = create_test_task(test_session, mock_mcp_context.user_id, title="Task to delete")
    task_id = task.id

    # Execute
    result = await delete_task_internal(
        ctx=mock_mcp_context,
        task_id=task_id
    )

    # Assert
    assert result["status"] == "success"
    assert result["task"]["id"] == task_id
    assert result["task"]["title"] == "Task to delete"

    # Verify task no longer exists
    deleted_task = get_task_by_id(test_session, task_id)
    assert deleted_task is None


@pytest.mark.unit
@pytest.mark.asyncio
async def test_delete_task_with_non_existent_task_id_returns_error(mock_mcp_context):
    """
    Test: delete_task with non-existent task_id returns error

    Verifies that delete_task returns error for non-existent task.
    """
    # Execute
    result = await delete_task_internal(
        ctx=mock_mcp_context,
        task_id=99999
    )

    # Assert
    assert result["status"] == "error"
    assert "error" in result
    assert "not found" in result["error"].lower()


@pytest.mark.unit
@pytest.mark.asyncio
async def test_delete_task_validates_task_ownership(mock_mcp_context, mock_mcp_context_user2, test_session):
    """
    Test: delete_task validates task ownership

    Verifies that delete_task returns error when trying to delete another user's task.
    """
    # Setup: Create task for user 2
    task = create_test_task(test_session, mock_mcp_context_user2.user_id, title="User 2 Task")

    # Execute with user 1 context
    result = await delete_task_internal(
        ctx=mock_mcp_context,
        task_id=task.id
    )

    # Assert - should fail
    assert result["status"] == "error"
    assert "not found" in result["error"].lower()

    # Verify task still exists
    unchanged_task = get_task_by_id(test_session, task.id)
    assert unchanged_task is not None
    assert unchanged_task.title == "User 2 Task"


@pytest.mark.unit
@pytest.mark.asyncio
async def test_delete_task_returns_deleted_task_details(mock_mcp_context, test_session):
    """
    Test: delete_task returns deleted task details

    Verifies that delete_task returns information about the deleted task.
    """
    # Setup
    task = create_test_task(test_session, mock_mcp_context.user_id, title="My Task")

    # Execute
    result = await delete_task_internal(
        ctx=mock_mcp_context,
        task_id=task.id
    )

    # Assert response contains task details
    assert result["status"] == "success"
    assert "task" in result
    assert result["task"]["id"] == task.id
    assert result["task"]["title"] == "My Task"


@pytest.mark.unit
@pytest.mark.asyncio
async def test_delete_task_decrements_task_count(mock_mcp_context, test_session):
    """
    Test: delete_task decrements task count

    Verifies that deleting a task reduces the total task count.
    """
    # Setup: Create 3 tasks
    task1 = create_test_task(test_session, mock_mcp_context.user_id, title="Task 1")
    task2 = create_test_task(test_session, mock_mcp_context.user_id, title="Task 2")
    task3 = create_test_task(test_session, mock_mcp_context.user_id, title="Task 3")

    # Verify initial count
    initial_count = count_tasks(test_session, mock_mcp_context.user_id)
    assert initial_count == 3

    # Delete one task
    result = await delete_task_internal(
        ctx=mock_mcp_context,
        task_id=task2.id
    )
    assert result["status"] == "success"

    # Verify count decreased
    final_count = count_tasks(test_session, mock_mcp_context.user_id)
    assert final_count == 2


@pytest.mark.unit
@pytest.mark.asyncio
async def test_delete_task_is_permanent(mock_mcp_context, test_session):
    """
    Test: delete_task is permanent (no soft delete)

    Verifies that deleted tasks cannot be recovered.
    """
    # Setup
    task = create_test_task(test_session, mock_mcp_context.user_id, title="Permanent Delete")
    task_id = task.id

    # Delete task
    result = await delete_task_internal(
        ctx=mock_mcp_context,
        task_id=task_id
    )
    assert result["status"] == "success"

    # Verify task is completely gone (not soft deleted)
    deleted_task = get_task_by_id(test_session, task_id)
    assert deleted_task is None

    # Attempting to delete again should fail
    result2 = await delete_task_internal(
        ctx=mock_mcp_context,
        task_id=task_id
    )
    assert result2["status"] == "error"