-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add change status endpoint * Add tests for change job status endpoint * Add back accidentally deleted test file * Add general exception to catch things other than AssertionError * Update test to test non assertion error case and that it returns 500 status code
- Loading branch information
1 parent
e2ac388
commit fd183c3
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import json | ||
|
||
from unittest.mock import patch, Mock, AsyncMock | ||
from fastapi.responses import JSONResponse | ||
|
||
from florist.api.routes.server.job import change_job_status | ||
from florist.api.db.entities import JobStatus | ||
|
||
@patch("florist.api.db.entities.Job.find_by_id") | ||
async def test_change_job_status_success(mock_find_by_id: Mock) -> None: | ||
mock_job = Mock() | ||
mock_job.set_status = AsyncMock() | ||
|
||
mock_find_by_id.return_value = mock_job | ||
|
||
mock_request = Mock() | ||
mock_request.app.database = Mock() | ||
|
||
test_id = "test_id" | ||
test_status = JobStatus.NOT_STARTED | ||
|
||
response = await change_job_status(test_id, test_status, mock_request) | ||
|
||
mock_find_by_id.assert_called_once_with(test_id, mock_request.app.database) | ||
mock_job.set_status.assert_called_once_with(test_status, mock_request.app.database) | ||
|
||
assert isinstance(response, JSONResponse) | ||
assert response.status_code == 200 | ||
assert json.loads(response.body.decode("utf-8")) == {"status": "success"} | ||
|
||
@patch("florist.api.db.entities.Job.find_by_id") | ||
async def test_change_job_status_failure_in_find_by_id(mock_find_by_id: Mock) -> None: | ||
mock_find_by_id.return_value = None | ||
|
||
mock_request = Mock() | ||
mock_request.app.database = Mock() | ||
|
||
test_id = "test_id" | ||
test_status = JobStatus.NOT_STARTED | ||
|
||
response = await change_job_status(test_id, test_status, mock_request) | ||
|
||
mock_find_by_id.assert_called_once_with(test_id, mock_request.app.database) | ||
|
||
assert isinstance(response, JSONResponse) | ||
assert response.status_code == 400 | ||
assert json.loads(response.body.decode("utf-8")) == {"error": "Job test_id not found"} | ||
|
||
|
||
@patch("florist.api.db.entities.Job.find_by_id") | ||
async def test_change_job_status_failure_in_set_status(mock_find_by_id: Mock) -> None: | ||
mock_job = Mock() | ||
mock_job.set_status = AsyncMock() | ||
mock_job.set_status.side_effect = ValueError("Test Error") | ||
|
||
mock_find_by_id.return_value = mock_job | ||
|
||
mock_request = Mock() | ||
mock_request.app.database = Mock() | ||
|
||
test_id = "test_id" | ||
test_status = JobStatus.NOT_STARTED | ||
|
||
response = await change_job_status(test_id, test_status, mock_request) | ||
|
||
mock_find_by_id.assert_called_once_with(test_id, mock_request.app.database) | ||
mock_job.set_status.assert_called_once_with(test_status, mock_request.app.database) | ||
|
||
assert isinstance(response, JSONResponse) | ||
assert response.status_code == 500 | ||
assert json.loads(response.body.decode("utf-8")) == {"error": "Test Error"} |