|
| 1 | +""" |
| 2 | +Tests for continents API endpoints. |
| 3 | +""" |
| 4 | +import json |
| 5 | +import pytest |
| 6 | +from unittest.mock import patch |
| 7 | +from http import HTTPStatus |
| 8 | +from server.app import create_app |
| 9 | +from data import continents |
| 10 | + |
| 11 | + |
| 12 | +class TestContinentsEndpoints: |
| 13 | + """Test class for continents API endpoints.""" |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def client(self): |
| 17 | + """Create a test client.""" |
| 18 | + app = create_app() |
| 19 | + app.config['TESTING'] = True |
| 20 | + with app.test_client() as client: |
| 21 | + yield client |
| 22 | + |
| 23 | + @pytest.fixture |
| 24 | + def sample_continent(self): |
| 25 | + return {'continent_name': 'Asia'} |
| 26 | + |
| 27 | + # --- GET /continents --- |
| 28 | + |
| 29 | + def test_get_all_continents_success(self, client): |
| 30 | + """GET /continents returns list of all continents.""" |
| 31 | + with patch('data.continents.get_continents') as mock_get: |
| 32 | + mock_get.return_value = [continents.TEST_CONTINENT] |
| 33 | + response = client.get('/continents') |
| 34 | + assert response.status_code == HTTPStatus.OK |
| 35 | + data = response.get_json() |
| 36 | + assert isinstance(data, list) |
| 37 | + assert len(data) == 1 |
| 38 | + mock_get.assert_called_once() |
| 39 | + |
| 40 | + def test_get_all_continents_with_pagination(self, client): |
| 41 | + """GET /continents supports limit and offset query parameters.""" |
| 42 | + two_continents = [ |
| 43 | + {'continent_name': 'Africa'}, |
| 44 | + {'continent_name': 'Asia'}, |
| 45 | + ] |
| 46 | + with patch('data.continents.get_continents') as mock_get: |
| 47 | + mock_get.return_value = two_continents |
| 48 | + response = client.get('/continents?limit=1&offset=1') |
| 49 | + assert response.status_code == HTTPStatus.OK |
| 50 | + data = response.get_json() |
| 51 | + assert len(data) == 1 |
| 52 | + assert data[0]['continent_name'] == 'Asia' |
| 53 | + |
| 54 | + def test_get_all_continents_invalid_limit(self, client): |
| 55 | + """GET /continents?limit=-1 returns 400.""" |
| 56 | + response = client.get('/continents?limit=-1') |
| 57 | + assert response.status_code == HTTPStatus.BAD_REQUEST |
| 58 | + |
| 59 | + def test_get_all_continents_db_error(self, client): |
| 60 | + """GET /continents returns 500 on database error.""" |
| 61 | + with patch('data.continents.get_continents') as mock_get: |
| 62 | + mock_get.side_effect = Exception('DB connection failed') |
| 63 | + response = client.get('/continents') |
| 64 | + assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR |
| 65 | + |
| 66 | + # --- POST /continents --- |
| 67 | + |
| 68 | + def test_create_continent_success(self, client, sample_continent): |
| 69 | + """POST /continents creates a new continent and returns 201.""" |
| 70 | + with patch('data.continents.add_continent') as mock_add: |
| 71 | + mock_add.return_value = True |
| 72 | + response = client.post( |
| 73 | + '/continents', |
| 74 | + data=json.dumps(sample_continent), |
| 75 | + content_type='application/json' |
| 76 | + ) |
| 77 | + assert response.status_code == HTTPStatus.CREATED |
| 78 | + data = response.get_json() |
| 79 | + assert data['continent_name'] == 'Asia' |
| 80 | + mock_add.assert_called_once_with(sample_continent) |
| 81 | + |
| 82 | + def test_create_continent_invalid_name(self, client): |
| 83 | + """POST /continents with invalid continent_name returns 400.""" |
| 84 | + response = client.post( |
| 85 | + '/continents', |
| 86 | + data=json.dumps({'continent_name': 'Atlantis'}), |
| 87 | + content_type='application/json' |
| 88 | + ) |
| 89 | + assert response.status_code == HTTPStatus.BAD_REQUEST |
| 90 | + |
| 91 | + def test_create_continent_already_exists(self, client, sample_continent): |
| 92 | + """POST /continents with duplicate name returns 409.""" |
| 93 | + with patch('data.continents.add_continent') as mock_add: |
| 94 | + mock_add.side_effect = ValueError("Continent 'Asia' already exists") |
| 95 | + response = client.post( |
| 96 | + '/continents', |
| 97 | + data=json.dumps(sample_continent), |
| 98 | + content_type='application/json' |
| 99 | + ) |
| 100 | + assert response.status_code == HTTPStatus.CONFLICT |
| 101 | + |
| 102 | + def test_create_continent_db_error(self, client, sample_continent): |
| 103 | + """POST /continents returns 500 on unexpected database error.""" |
| 104 | + with patch('data.continents.add_continent') as mock_add: |
| 105 | + mock_add.side_effect = Exception('DB write failed') |
| 106 | + response = client.post( |
| 107 | + '/continents', |
| 108 | + data=json.dumps(sample_continent), |
| 109 | + content_type='application/json' |
| 110 | + ) |
| 111 | + assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR |
| 112 | + |
| 113 | + # --- GET /continents/<name> --- |
| 114 | + |
| 115 | + def test_get_continent_success(self, client): |
| 116 | + """GET /continents/<name> returns the continent.""" |
| 117 | + with patch('data.continents.get_continent_by_name') as mock_get: |
| 118 | + mock_get.return_value = continents.TEST_CONTINENT |
| 119 | + response = client.get('/continents/North America') |
| 120 | + assert response.status_code == HTTPStatus.OK |
| 121 | + data = response.get_json() |
| 122 | + assert data['continent_name'] == continents.TEST_CONTINENT['continent_name'] |
| 123 | + |
| 124 | + def test_get_continent_not_found(self, client): |
| 125 | + """GET /continents/<name> returns 404 when not found.""" |
| 126 | + with patch('data.continents.get_continent_by_name') as mock_get: |
| 127 | + mock_get.return_value = None |
| 128 | + response = client.get('/continents/Atlantis') |
| 129 | + assert response.status_code == HTTPStatus.NOT_FOUND |
| 130 | + |
| 131 | + def test_get_continent_db_error(self, client): |
| 132 | + """GET /continents/<name> returns 500 on database error.""" |
| 133 | + with patch('data.continents.get_continent_by_name') as mock_get: |
| 134 | + mock_get.side_effect = Exception('DB error') |
| 135 | + response = client.get('/continents/Asia') |
| 136 | + assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR |
| 137 | + |
| 138 | + # --- PUT /continents/<name> --- |
| 139 | + |
| 140 | + def test_update_continent_success(self, client): |
| 141 | + """PUT /continents/<name> returns 204 on success.""" |
| 142 | + with patch('data.continents.update_continent') as mock_update: |
| 143 | + mock_update.return_value = True |
| 144 | + response = client.put('/continents/Asia') |
| 145 | + assert response.status_code == HTTPStatus.NO_CONTENT |
| 146 | + |
| 147 | + def test_update_continent_not_found(self, client): |
| 148 | + """PUT /continents/<name> returns 404 when continent does not exist.""" |
| 149 | + with patch('data.continents.update_continent') as mock_update: |
| 150 | + mock_update.return_value = False |
| 151 | + response = client.put('/continents/Atlantis') |
| 152 | + assert response.status_code == HTTPStatus.NOT_FOUND |
| 153 | + |
| 154 | + def test_update_continent_db_error(self, client): |
| 155 | + """PUT /continents/<name> returns 500 on database error.""" |
| 156 | + with patch('data.continents.update_continent') as mock_update: |
| 157 | + mock_update.side_effect = Exception('DB error') |
| 158 | + response = client.put('/continents/Asia') |
| 159 | + assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR |
| 160 | + |
| 161 | + # --- DELETE /continents/<name> --- |
| 162 | + |
| 163 | + def test_delete_continent_success(self, client): |
| 164 | + """DELETE /continents/<name> returns 204 on success.""" |
| 165 | + with patch('data.continents.delete_continent') as mock_delete: |
| 166 | + mock_delete.return_value = True |
| 167 | + response = client.delete('/continents/Antarctica') |
| 168 | + assert response.status_code == HTTPStatus.NO_CONTENT |
| 169 | + |
| 170 | + def test_delete_continent_not_found(self, client): |
| 171 | + """DELETE /continents/<name> returns 404 when not found.""" |
| 172 | + with patch('data.continents.delete_continent') as mock_delete: |
| 173 | + mock_delete.return_value = False |
| 174 | + response = client.delete('/continents/Atlantis') |
| 175 | + assert response.status_code == HTTPStatus.NOT_FOUND |
| 176 | + |
| 177 | + def test_delete_continent_with_countries(self, client): |
| 178 | + """DELETE /continents/<name> returns 409 when countries reference it.""" |
| 179 | + with patch('data.continents.delete_continent') as mock_delete: |
| 180 | + mock_delete.side_effect = ValueError( |
| 181 | + 'Cannot delete: 3 country/countries reference this continent' |
| 182 | + ) |
| 183 | + response = client.delete('/continents/Africa') |
| 184 | + assert response.status_code == HTTPStatus.CONFLICT |
| 185 | + data = response.get_json() |
| 186 | + assert 'Cannot delete' in data['message'] |
| 187 | + |
| 188 | + def test_delete_continent_db_error(self, client): |
| 189 | + """DELETE /continents/<name> returns 500 on unexpected error.""" |
| 190 | + with patch('data.continents.delete_continent') as mock_delete: |
| 191 | + mock_delete.side_effect = Exception('DB error') |
| 192 | + response = client.delete('/continents/Asia') |
| 193 | + assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR |
0 commit comments