|
1 | | -import unittest |
2 | | -from app import app |
3 | | -from unittest.mock import patch, MagicMock |
4 | | -import json |
5 | | -from bson import ObjectId |
| 1 | +import pytest |
| 2 | +from flask import Flask, json |
| 3 | +from pymongo import MongoClient |
| 4 | +from app import app as flask_app # Assuming your Flask app is saved as app.py |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def client(): |
| 8 | + with flask_app.test_client() as client: |
| 9 | + yield client |
| 10 | + |
| 11 | + |
| 12 | +def test_get_nonexistent_student(client): |
| 13 | + student_id = '999' |
| 14 | + response = client.get(f'/students/{student_id}') |
| 15 | + assert response.status_code == 200 |
| 16 | + data = json.loads(response.data) |
| 17 | + assert data is None |
| 18 | + |
| 19 | + |
| 20 | +def test_update_nonexistent_student(client): |
| 21 | + student_id = '999' |
| 22 | + updated_student = { |
| 23 | + "name": "Nonexistent Student", |
| 24 | + "age": 30, |
| 25 | + "major": "Physics" |
| 26 | + } |
| 27 | + response = client.put(f'/students/{student_id}', json=updated_student) |
| 28 | + assert response.status_code == 200 |
| 29 | + data = json.loads(response.data) |
| 30 | + assert data["message"] == "Student updated successfully" |
6 | 31 |
|
7 | | -class TestTaskManager(unittest.TestCase): |
8 | 32 |
|
9 | | - def setUp(self): |
10 | | - self.app = app.test_client() |
11 | | - self.app.testing = True |
12 | | - |
13 | | - self.collection_mock = MagicMock() |
14 | | - self.patcher = patch('app.collection', self.collection_mock) |
15 | | - self.patcher.start() |
16 | | - |
17 | | - def tearDown(self): |
18 | | - self.patcher.stop() |
19 | | - |
20 | | - def test_create_task(self): |
21 | | - data = {'title': 'Test Task', 'description': 'This is a test task'} |
22 | | - response = self.app.post('/api/tasks', json=data) |
23 | | - self.assertEqual(response.status_code, 201) |
24 | | - self.assertIn(b'Task created successfully', response.data) |
25 | | - |
26 | | - def test_get_all_tasks(self): |
27 | | - self.collection_mock.find.return_value = [] |
28 | | - response = self.app.get('/api/tasks') |
29 | | - self.assertEqual(response.status_code, 200) |
30 | | - self.assertEqual(json.loads(response.data)['tasks'], []) |
31 | | - |
32 | | - def test_update_task(self): |
33 | | - with patch('app.ObjectId', return_value='mocked_id'): |
34 | | - response = self.app.put('/api/tasks/mock_id', json={'title': 'Updated Task', 'description': 'This is an updated task'}) |
35 | | - self.assertEqual(response.status_code, 200) |
36 | | - self.assertIn(b'Task updated successfully', response.data) |
37 | | - |
38 | | - def test_delete_task(self): |
39 | | - with patch('app.ObjectId', return_value=ObjectId()): |
40 | | - response = self.app.delete('/api/tasks/mock_id') |
41 | | - self.assertEqual(response.status_code, 200) |
42 | | - self.assertIn(b'Task deleted successfully', response.data) |
43 | | - |
44 | | -if __name__ == '__main__': |
45 | | - unittest.main() |
0 commit comments