-
Notifications
You must be signed in to change notification settings - Fork 1
Added endpoints to check status of multigrid controllers #636
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e140f35
Added backend server endpoint to check whether a multigrid controller…
tieneupin fb84819
Added instrument server endpoint to check whether a multigrid control…
tieneupin 310dc1e
Updated route manifest
tieneupin ab16492
Forgot to change the request method to be used
tieneupin 119ec67
Added debug log
tieneupin 0f8064d
Forgot to replace another 'post' with 'get'
tieneupin f527b5c
Merged recent changes from 'main' branch
tieneupin 239d7e9
Rearannged functions by purpose
tieneupin 03799a8
Added test for backend endpoint to check for existence of multigrid c…
tieneupin 9b9c738
Added test for instrument server endpoint to check for existence of m…
tieneupin fda4f56
Use 'pytest.mark' instead of just 'mark' for greater clarity in decor…
tieneupin 5993c23
Use MagicMock consistently in test module
tieneupin 99f216f
Switched to using 'mocker' within test function instead of stacking m…
tieneupin fe10c62
Use the FastAPI TestClient to test the 'upload_gain_reference' endpoi…
tieneupin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
| from typing import Literal | ||
| from unittest import mock | ||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| from fastapi import FastAPI | ||
| from fastapi.testclient import TestClient | ||
| from pytest_mock import MockerFixture | ||
|
|
||
| from murfey.server.api.auth import validate_frontend_session_access, validate_token | ||
| from murfey.server.api.instrument import router as backend_router | ||
| from murfey.server.murfey_db import murfey_db_session | ||
| from murfey.util.api import url_path_for | ||
|
|
||
|
|
||
| def mock_aiohttp_clientsession( | ||
| mocker: MockerFixture, | ||
| method: Literal["get", "post", "delete"] = "get", | ||
| json_data={}, | ||
| status=200, | ||
| ): | ||
| """ | ||
| Helper function to patch a aiohttp.ClientSession GET request. This returns a | ||
| mocked async context manager with a mocked response that, in turn, returns | ||
| the given JSON data and status. | ||
|
|
||
| Returns the mocked ClientSession, which can then be inspected to assert that | ||
| the expected calls were made. | ||
| """ | ||
|
|
||
| # Mock out the async response | ||
| mock_response = MagicMock() | ||
| mock_response.json = AsyncMock(return_value=json_data) | ||
| mock_response.status = status | ||
|
|
||
| # Mock out the context manager returned by clientsession.get() | ||
| mock_context_manager = MagicMock() | ||
| mock_context_manager.__aenter__ = AsyncMock(return_value=mock_response) | ||
| mock_context_manager.__aexit__ = AsyncMock(return_value=None) | ||
|
|
||
| # Mock the client session | ||
| mock_clientsession = MagicMock() | ||
| mock_clientsession.__aenter__ = AsyncMock(return_value=mock_clientsession) | ||
| mock_clientsession.__aexit__ = AsyncMock(return_value=None) | ||
|
|
||
| # Assign the context manager to the request method being tested | ||
| getattr(mock_clientsession, method.lower()).return_value = mock_context_manager | ||
|
|
||
| # Patch 'aiohttp.ClientSession' to return the mocked client session | ||
| mocker.patch("aiohttp.ClientSession", return_value=mock_clientsession) | ||
|
|
||
| return mock_clientsession, mock_response | ||
|
|
||
|
|
||
| def test_check_multigrid_controller_exists(mocker: MockerFixture): | ||
| # Set up the objects to mock | ||
| instrument_name = "test" | ||
| session_id = 1 | ||
| instrment_server_url = "https://murfey.instrument-server.test" | ||
|
|
||
| # Override the database session generator | ||
| mock_session = MagicMock() | ||
| mock_session.instrument_name = instrument_name | ||
| mock_query_result = MagicMock() | ||
| mock_query_result.one.return_value = mock_session | ||
| mock_db_session = MagicMock() | ||
| mock_db_session.exec.return_value = mock_query_result | ||
|
|
||
| def mock_get_db_session(): | ||
| yield mock_db_session | ||
|
|
||
| # Mock the machine config | ||
| mock_machine_config = MagicMock() | ||
| mock_machine_config.instrument_server_url = instrment_server_url | ||
| mock_get_machine_config = mocker.patch( | ||
| "murfey.server.api.instrument.get_machine_config" | ||
| ) | ||
| mock_get_machine_config.return_value = { | ||
| instrument_name: mock_machine_config, | ||
| } | ||
|
|
||
| # Mock the instrument server tokens dictionary | ||
| mock_tokens = mocker.patch( | ||
| "murfey.server.api.instrument.instrument_server_tokens", | ||
| {session_id: {"access_token": mock.sentinel}}, | ||
| ) | ||
|
|
||
| # Mock out the async GET request in the endpoint | ||
| mock_clientsession, _ = mock_aiohttp_clientsession( | ||
| mocker, | ||
| method="get", | ||
| json_data={"exists": True}, | ||
| status=200, | ||
| ) | ||
|
|
||
| # Set up the backend server | ||
| backend_app = FastAPI() | ||
|
|
||
| # Override validation and database dependencies | ||
| backend_app.dependency_overrides[validate_token] = lambda: None | ||
| backend_app.dependency_overrides[validate_frontend_session_access] = ( | ||
| lambda: session_id | ||
| ) | ||
| backend_app.dependency_overrides[murfey_db_session] = mock_get_db_session | ||
| backend_app.include_router(backend_router) | ||
| backend_server = TestClient(backend_app) | ||
|
|
||
| # Construct the URL paths for poking and sending to | ||
| backend_url_path = url_path_for( | ||
| "api.instrument.router", | ||
| "check_multigrid_controller_exists", | ||
| session_id=session_id, | ||
| ) | ||
| client_url_path = url_path_for( | ||
| "api.router", | ||
| "check_multigrid_controller_exists", | ||
| session_id=session_id, | ||
| ) | ||
|
|
||
| # Poke the backend | ||
| response = backend_server.get(backend_url_path) | ||
|
|
||
| # Check that the expected calls were made | ||
| mock_db_session.exec.assert_called_once() | ||
| mock_get_machine_config.assert_called_once_with(instrument_name=instrument_name) | ||
| mock_clientsession.get.assert_called_once_with( | ||
| f"{instrment_server_url}{client_url_path}", | ||
| headers={"Authorization": f"Bearer {mock_tokens[session_id]['access_token']}"}, | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json() == {"exists": True} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.