-
Notifications
You must be signed in to change notification settings - Fork 5
add async post_observation() to AsyncERClient #24
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
Open
JoshuaVulcan
wants to merge
12
commits into
main
Choose a base branch
from
ERA-12672/async-post-observation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+167
−0
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a7fd38a
add async post_observation() to AsyncERClient
JoshuaVulcan b6b733e
add test for set input type in async post_observation
JoshuaVulcan 1c3fc37
Update tests/async_client/test_post_observation.py
JoshuaVulcan 4d09c9e
Fix respx mock path: use 'observations' not '/observations'
JoshuaVulcan a4f9170
Fix respx mock: use 'observations' (no leading slash) in all post_obs…
JoshuaVulcan d5d0e4f
Fix respx mock URL for post_observation tests
JoshuaVulcan 30b150e
Fix respx: drop base_url so observations route matches request URL
JoshuaVulcan 7d0a1fb
Fix observations mock: use base_url + path '/observations' like other…
JoshuaVulcan 68b4971
Match test_post_report convention: mock path 'observations' (no leadi…
JoshuaVulcan 8c5cdaf
Merge branch 'main' into ERA-12672/async-post-observation
JoshuaVulcan 6d4096c
fix:duh. api root misinstrumentation.
JoshuaVulcan 9a32a8c
Merge main into ERA-12672/async-post-observation
JoshuaVulcan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import json | ||
| from datetime import datetime, timezone | ||
|
|
||
| import httpx | ||
| import pytest | ||
| import respx | ||
|
|
||
| from erclient import (ERClientException, ERClientNotFound, | ||
| ERClientPermissionDenied, ERClientServiceUnreachable) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_single_success(er_client, position, position_created_response): | ||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.return_value = httpx.Response( | ||
| httpx.codes.CREATED, json=position_created_response) | ||
| response = await er_client.post_observation(position) | ||
| assert route.called | ||
| assert response == {} | ||
| await er_client.close() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_list_success(er_client, position, position_created_response): | ||
| observations = [position, {**position, "manufacturer_id": "018910981"}] | ||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.return_value = httpx.Response( | ||
| httpx.codes.CREATED, json=position_created_response) | ||
| response = await er_client.post_observation(observations) | ||
| assert route.called | ||
| # verify we sent a list payload | ||
| request_body = json.loads(route.calls[0].request.content) | ||
| assert isinstance(request_body, list) | ||
| assert len(request_body) == 2 | ||
| await er_client.close() | ||
|
JoshuaVulcan marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_set_input(er_client, position_created_response): | ||
| """Verify that a set input is iterated and each element cleaned individually. | ||
|
|
||
| Note: in practice, observation dicts are unhashable so callers use lists. | ||
| This test uses a frozenset wrapper to exercise the isinstance(obs, (list, set)) | ||
| branch and confirm it produces a list payload. | ||
| """ | ||
| # Use simple hashable stand-ins to verify the set branch | ||
| obs_a = ("obs_a",) | ||
| obs_b = ("obs_b",) | ||
| observations = {obs_a, obs_b} # a real set | ||
|
|
||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.return_value = httpx.Response( | ||
| httpx.codes.CREATED, json=position_created_response) | ||
|
|
||
| # Patch _clean_observation to be a passthrough since tuples | ||
| # don't have 'recorded_at' key | ||
| with pytest.MonkeyPatch.context() as mp: | ||
| mp.setattr(er_client, '_clean_observation', lambda o: list(o)) | ||
| response = await er_client.post_observation(observations) | ||
|
|
||
| assert route.called | ||
| request_body = json.loads(route.calls[0].request.content) | ||
| assert isinstance(request_body, list) | ||
| assert len(request_body) == 2 | ||
| await er_client.close() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_cleans_datetime(er_client, position_created_response): | ||
| """Verify that datetime objects in recorded_at are converted to ISO strings.""" | ||
| dt = datetime(2024, 6, 15, 12, 0, 0, tzinfo=timezone.utc) | ||
| observation = { | ||
| "manufacturer_id": "018910980", | ||
| "source_type": "tracking-device", | ||
| "subject_name": "Test Truck", | ||
| "recorded_at": dt, | ||
| "location": {"lon": 35.43903, "lat": -1.59083}, | ||
| } | ||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.return_value = httpx.Response( | ||
| httpx.codes.CREATED, json=position_created_response) | ||
| await er_client.post_observation(observation) | ||
| request_body = json.loads(route.calls[0].request.content) | ||
| assert isinstance(request_body['recorded_at'], str) | ||
| assert request_body['recorded_at'] == dt.isoformat() | ||
| await er_client.close() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_connect_timeout(er_client, position): | ||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.side_effect = httpx.ConnectTimeout | ||
| with pytest.raises(ERClientException): | ||
| await er_client.post_observation(position) | ||
| assert route.called | ||
| await er_client.close() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_not_found(er_client, position, not_found_response): | ||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.return_value = httpx.Response( | ||
| httpx.codes.NOT_FOUND, json=not_found_response) | ||
| with pytest.raises(ERClientNotFound): | ||
| await er_client.post_observation(position) | ||
| assert route.called | ||
| await er_client.close() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_forbidden(er_client, position, forbidden_response): | ||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.return_value = httpx.Response( | ||
| httpx.codes.FORBIDDEN, json=forbidden_response) | ||
| with pytest.raises(ERClientPermissionDenied) as exc_info: | ||
| await er_client.post_observation(position) | ||
| assert exc_info.value.status_code == httpx.codes.FORBIDDEN | ||
| assert route.called | ||
| await er_client.close() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_post_observation_conflict(er_client, position, conflict_response): | ||
| async with respx.mock( | ||
| base_url=er_client._api_root("v1.0"), assert_all_called=False | ||
| ) as respx_mock: | ||
| route = respx_mock.post('observations') | ||
| route.return_value = httpx.Response( | ||
| httpx.codes.CONFLICT, json=conflict_response) | ||
| with pytest.raises(ERClientException) as exc_info: | ||
| await er_client.post_observation(position) | ||
| assert exc_info.value.status_code == httpx.codes.CONFLICT | ||
| assert route.called | ||
| await er_client.close() | ||
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.