-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timetable_list.py
More file actions
162 lines (131 loc) · 5.27 KB
/
test_timetable_list.py
File metadata and controls
162 lines (131 loc) · 5.27 KB
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
import unittest
from unittest.mock import Mock, MagicMock
from campus_python.api.v1.timetable import Timetables
from campus_python.interface import ResourceRoot
import campus.model
class TestTimetablesList(unittest.TestCase):
"""Test Timetables.list() method."""
def setUp(self):
"""Set up test fixtures."""
# Create mock client
self.mock_client = Mock()
self.mock_client.base_url = "https://api.example.com"
# Create resource root
self.root = ResourceRoot(self.mock_client)
self.root.url_prefix = "api/v1"
# Create Timetables instance
self.timetables = Timetables(self.mock_client, root=self.root)
def test_list_without_filters(self):
"""Test list() returns timetables without filters."""
# Mock the response - backend returns direct list
mock_response = Mock()
mock_response.json.return_value = [
{
"id": "tt-123",
"filename": "schedule.xlsx",
"start_date": "2026-01-01T00:00:00Z",
"end_date": "2026-06-30T00:00:00Z",
"created_at": "2026-01-01T00:00:00Z"
},
{
"id": "tt-456",
"filename": "other.xlsx",
"start_date": "2026-07-01T00:00:00Z",
"end_date": "2026-12-31T00:00:00Z",
"created_at": "2026-07-01T00:00:00Z"
}
]
mock_response.raise_for_status = Mock()
self.mock_client.get.return_value = mock_response
# Call list()
result = self.timetables.list()
# Verify the client was called correctly
self.mock_client.get.assert_called_once_with(
"/api/v1/timetable/",
query=None
)
# Verify results
self.assertEqual(len(result), 2)
self.assertEqual(result[0].id, "tt-123")
self.assertEqual(result[0].filename, "schedule.xlsx")
self.assertEqual(result[1].id, "tt-456")
self.assertEqual(result[1].filename, "other.xlsx")
def test_list_with_filters(self):
"""Test list() with filter parameters."""
# Mock the response - backend returns direct list
mock_response = Mock()
mock_response.json.return_value = [
{
"id": "tt-123",
"filename": "schedule.xlsx",
"start_date": "2026-01-01T00:00:00Z",
"end_date": "2026-06-30T00:00:00Z",
"created_at": "2026-01-01T00:00:00Z"
}
]
mock_response.raise_for_status = Mock()
self.mock_client.get.return_value = mock_response
# Call list() with filters
result = self.timetables.list(filename="schedule.xlsx")
# Verify the client was called with correct filters
self.mock_client.get.assert_called_once_with(
"/api/v1/timetable/",
query={"filename": "schedule.xlsx"}
)
# Verify results
self.assertEqual(len(result), 1)
self.assertEqual(result[0].filename, "schedule.xlsx")
def test_list_empty_result(self):
"""Test list() returns empty list when no timetables found."""
# Mock the response - backend returns direct list
mock_response = Mock()
mock_response.json.return_value = []
mock_response.raise_for_status = Mock()
self.mock_client.get.return_value = mock_response
# Call list()
result = self.timetables.list()
# Verify results
self.assertEqual(len(result), 0)
def test_list_with_multiple_filters(self):
"""Test list() with multiple filter parameters."""
# Mock the response - backend returns direct list
mock_response = Mock()
mock_response.json.return_value = [
{
"id": "tt-789",
"filename": "test.xlsx",
"start_date": "2026-01-01T00:00:00Z",
"end_date": "2026-06-30T00:00:00Z",
"created_at": "2026-01-01T00:00:00Z"
}
]
mock_response.raise_for_status = Mock()
self.mock_client.get.return_value = mock_response
# Call list() with multiple filters
result = self.timetables.list(
filename="test.xlsx",
start_date="2026-01-01T00:00:00Z"
)
# Verify the client was called with correct filters
self.mock_client.get.assert_called_once_with(
"/api/v1/timetable/",
query={
"filename": "test.xlsx",
"start_date": "2026-01-01T00:00:00Z"
}
)
# Verify results
self.assertEqual(len(result), 1)
self.assertEqual(result[0].filename, "test.xlsx")
def test_list_propagates_client_errors(self):
"""Test list() propagates client errors."""
# Mock response with raise_for_status that raises exception
mock_response = Mock()
mock_response.raise_for_status.side_effect = Exception("Client error")
self.mock_client.get.return_value = mock_response
# Call list() should raise exception
with self.assertRaises(Exception) as context:
self.timetables.list()
self.assertEqual(str(context.exception), "Client error")
if __name__ == "__main__":
unittest.main()