-
Notifications
You must be signed in to change notification settings - Fork 5
Fix database error handling in project creation #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,9 @@ | |
| 3. REFACTOR: Clean up while keeping tests green | ||
| """ | ||
|
|
||
| import sqlite3 | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
|
|
@@ -212,13 +215,119 @@ def test_create_project_via_api_then_get_status(self, api_client): | |
| class TestProjectCreationErrorHandling: | ||
| """Test error handling for project creation API.""" | ||
|
|
||
| @pytest.mark.skip( | ||
| reason="Database close() creates ungraceful crashes, not 500 errors. This test design is flawed." | ||
| ) | ||
| def test_create_project_handles_database_errors(self, api_client): | ||
| """Test that database errors are handled gracefully (500 Internal Server Error).""" | ||
| # This test is skipped - see reason above | ||
| pass | ||
| def test_create_project_database_locked_error(self, api_client): | ||
| """Test that database locked error returns 500 Internal Server Error.""" | ||
| from codeframe.ui import server | ||
|
|
||
| with patch.object( | ||
| server.app.state.db, | ||
| "create_project", | ||
| side_effect=sqlite3.OperationalError("database is locked"), | ||
| ): | ||
| response = api_client.post( | ||
| "/api/projects", | ||
| json={"name": "test-db-locked", "description": "Test project"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 500 | ||
| data = response.json() | ||
| assert "detail" in data | ||
| assert "database" in data["detail"].lower() | ||
|
|
||
|
Comment on lines
+218
to
+236
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add type hints to test methods. The test method lacks type hints for the Apply this diff: - def test_create_project_database_locked_error(self, api_client):
+ def test_create_project_database_locked_error(self, api_client: TestClient) -> None:
"""Test that database locked error returns 500 Internal Server Error."""You'll need to add this import at the top: from starlette.testclient import TestClient</review_comment_end> 🤖 Prompt for AI Agents |
||
| def test_create_project_disk_full_error(self, api_client): | ||
| """Test that disk I/O error returns 500 Internal Server Error.""" | ||
| from codeframe.ui import server | ||
|
|
||
| with patch.object( | ||
| server.app.state.db, | ||
| "create_project", | ||
| side_effect=sqlite3.OperationalError("disk I/O error"), | ||
| ): | ||
| response = api_client.post( | ||
| "/api/projects", | ||
| json={"name": "test-disk-full", "description": "Test project"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 500 | ||
| data = response.json() | ||
| assert "detail" in data | ||
| assert "database" in data["detail"].lower() or "i/o" in data["detail"].lower() | ||
|
|
||
|
Comment on lines
+237
to
+255
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add type hints to test methods. Missing type hints for the Apply this diff: - def test_create_project_disk_full_error(self, api_client):
+ def test_create_project_disk_full_error(self, api_client: TestClient) -> None:
"""Test that disk I/O error returns 500 Internal Server Error."""</review_comment_end> 🤖 Prompt for AI Agents |
||
| def test_create_project_integrity_error(self, api_client): | ||
| """Test that constraint violation error returns 500 Internal Server Error.""" | ||
| from codeframe.ui import server | ||
|
|
||
| with patch.object( | ||
| server.app.state.db, | ||
| "create_project", | ||
| side_effect=sqlite3.IntegrityError("UNIQUE constraint failed"), | ||
| ): | ||
| response = api_client.post( | ||
| "/api/projects", | ||
| json={"name": "test-integrity", "description": "Test project"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 500 | ||
| data = response.json() | ||
| assert "detail" in data | ||
| assert "database" in data["detail"].lower() or "constraint" in data["detail"].lower() | ||
|
|
||
|
Comment on lines
+256
to
+274
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarify IntegrityError test scenario and add type hints. This test simulates an IntegrityError during Also missing type hints for the Apply this diff: - def test_create_project_integrity_error(self, api_client):
- """Test that constraint violation error returns 500 Internal Server Error."""
+ def test_create_project_integrity_error(self, api_client: TestClient) -> None:
+ """Test that constraint violation error returns 500 Internal Server Error.
+
+ This tests edge cases where IntegrityError occurs despite duplicate name checks,
+ such as race conditions or other constraint violations.
+ """</review_comment_end> 🤖 Prompt for AI Agents |
||
| def test_create_project_list_projects_database_error(self, api_client): | ||
| """Test that database error during list_projects returns 500 Internal Server Error.""" | ||
| from codeframe.ui import server | ||
|
|
||
| with patch.object( | ||
| server.app.state.db, | ||
| "list_projects", | ||
| side_effect=sqlite3.OperationalError("database is locked"), | ||
| ): | ||
| response = api_client.post( | ||
| "/api/projects", | ||
| json={"name": "test-list-error", "description": "Test project"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 500 | ||
| data = response.json() | ||
| assert "detail" in data | ||
| assert "database" in data["detail"].lower() | ||
|
|
||
|
Comment on lines
+275
to
293
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add type hints to test methods. Missing type hints for the Apply this diff: - def test_create_project_list_projects_database_error(self, api_client):
+ def test_create_project_list_projects_database_error(self, api_client: TestClient) -> None:
"""Test that database error during list_projects returns 500 Internal Server Error."""</review_comment_end> 🤖 Prompt for AI Agents |
||
| def test_create_project_update_project_database_error(self, api_client): | ||
| """Test that database error during update_project returns 500 Internal Server Error.""" | ||
| from codeframe.ui import server | ||
|
|
||
| with patch.object( | ||
| server.app.state.db, | ||
| "update_project", | ||
| side_effect=sqlite3.OperationalError("database is locked"), | ||
| ): | ||
| response = api_client.post( | ||
| "/api/projects", | ||
| json={"name": "test-update-error", "description": "Test project"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 500 | ||
| data = response.json() | ||
| assert "detail" in data | ||
| assert "database" in data["detail"].lower() | ||
|
|
||
| def test_create_project_get_project_database_error(self, api_client): | ||
| """Test that database error during get_project returns 500 Internal Server Error.""" | ||
| from codeframe.ui import server | ||
|
|
||
| with patch.object( | ||
| server.app.state.db, | ||
| "get_project", | ||
| side_effect=sqlite3.OperationalError("database is locked"), | ||
| ): | ||
| response = api_client.post( | ||
| "/api/projects", | ||
| json={"name": "test-get-error", "description": "Test project"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 500 | ||
| data = response.json() | ||
| assert "detail" in data | ||
| assert "database" in data["detail"].lower() | ||
|
|
||
| def test_create_project_with_extra_fields(self, api_client): | ||
| """Test that extra fields in request are ignored.""" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.