-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
71 lines (59 loc) · 2.71 KB
/
test_api.py
File metadata and controls
71 lines (59 loc) · 2.71 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
import unittest
import json
from app import create_app
class ChatAPITestCase(unittest.TestCase):
"""Test case for the chat API."""
def setUp(self):
"""Set up test client and other test variables."""
self.app = create_app('testing')
self.client = self.app.test_client
# Test message data
self.message = {
'sender': 'test_user',
'content': 'Hello, this is a test message!'
}
def test_message_creation(self):
"""Test API can create a message (POST request)."""
res = self.client().post('/api/messages',
data=json.dumps(self.message),
content_type='application/json')
self.assertEqual(res.status_code, 201)
self.assertIn('Message created successfully', str(res.data))
def test_get_all_messages(self):
"""Test API can get all messages (GET request)."""
# First create a test message
self.client().post('/api/messages',
data=json.dumps(self.message),
content_type='application/json')
# Then get all messages
res = self.client().get('/api/messages')
self.assertEqual(res.status_code, 200)
self.assertIn('success', str(res.data))
def test_get_message_by_id(self):
"""Test API can get a single message by ID (GET request)."""
# First create a test message
post_res = self.client().post('/api/messages',
data=json.dumps(self.message),
content_type='application/json')
post_data = json.loads(post_res.data)
message_id = post_data['data']['id']
# Then get the message by ID
res = self.client().get(f'/api/messages/{message_id}')
self.assertEqual(res.status_code, 200)
self.assertIn('success', str(res.data))
def test_delete_message(self):
"""Test API can delete a message (DELETE request)."""
# First create a test message
post_res = self.client().post('/api/messages',
data=json.dumps(self.message),
content_type='application/json')
post_data = json.loads(post_res.data)
message_id = post_data['data']['id']
# Then delete the message
res = self.client().delete(f'/api/messages/{message_id}')
self.assertEqual(res.status_code, 200)
# Try to get the deleted message
res = self.client().get(f'/api/messages/{message_id}')
self.assertEqual(res.status_code, 404)
if __name__ == '__main__':
unittest.main()