-
Notifications
You must be signed in to change notification settings - Fork 13
support for device custom property intervals #153
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
base: main
Are you sure you want to change the base?
Changes from all commits
6749864
b57e8af
99a4da6
e1916d6
8d106b5
15bccd2
1097414
977eb25
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 |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import json | ||
| from datetime import datetime | ||
| from urllib.parse import parse_qs, quote, urlparse | ||
|
|
||
| import pytest | ||
| import responses | ||
| from faker import Faker | ||
|
|
||
| from foxglove.client import Client | ||
|
|
||
| from .api_url import api_url | ||
|
|
||
| fake = Faker() | ||
|
|
||
|
|
||
| @responses.activate | ||
| def test_get_device_custom_property_time_interval_quotes_path_and_passes_project_id(): | ||
| device_name = "Device / Name" | ||
| time_interval_id = "dcph/id" | ||
| now = datetime.now() | ||
| responses.add( | ||
| responses.GET, | ||
| api_url( | ||
| f"/v1/devices/{quote(device_name, safe='')}/property-time-intervals/" | ||
| f"{quote(time_interval_id, safe='')}" | ||
| ), | ||
| json={ | ||
| "id": time_interval_id, | ||
| "deviceId": fake.uuid4(), | ||
| "key": "env", | ||
| "value": "prod", | ||
| "start": now.isoformat(), | ||
| "end": now.isoformat(), | ||
| }, | ||
| ) | ||
|
|
||
| client = Client("test") | ||
| response = client.get_device_custom_property_time_interval( | ||
| device_name=device_name, | ||
| project_id="project-id", | ||
| id=time_interval_id, | ||
| ) | ||
|
|
||
| assert response["id"] == time_interval_id | ||
| assert parse_qs(urlparse(responses.calls[0].request.url).query) == { | ||
| "projectId": ["project-id"] | ||
| } | ||
|
|
||
|
|
||
| def test_get_device_custom_property_time_interval_rejects_multiple_device_selectors(): | ||
| client = Client("test") | ||
|
|
||
| with pytest.raises(RuntimeError) as exception: | ||
| client.get_device_custom_property_time_interval( | ||
| device_id="device-id", | ||
| device_name="device-name", | ||
| id="time-interval-id", | ||
| ) | ||
|
|
||
| assert str(exception.value) == "device_id and device_name are mutually exclusive" | ||
|
|
||
|
|
||
| @responses.activate | ||
| def test_get_device_custom_property_time_intervals_uses_path_selector_only(): | ||
| device_name = "Device / Name" | ||
| responses.add( | ||
| responses.GET, | ||
| api_url(f"/v1/devices/{quote(device_name, safe='')}/property-time-intervals"), | ||
| json=[], | ||
| ) | ||
|
|
||
| client = Client("test") | ||
| client.get_device_custom_property_time_intervals( | ||
| device_name=device_name, | ||
| project_id="project-id", | ||
| key="env", | ||
| limit=5, | ||
| offset=10, | ||
| ) | ||
|
|
||
| assert parse_qs(urlparse(responses.calls[0].request.url).query) == { | ||
| "key": ["env"], | ||
| "limit": ["5"], | ||
| "offset": ["10"], | ||
| "projectId": ["project-id"], | ||
| } | ||
|
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. suggestion: The list test covers |
||
|
|
||
|
|
||
| @responses.activate | ||
| def test_get_device_custom_property_time_intervals_supports_multiple_keys(): | ||
| responses.add( | ||
| responses.GET, | ||
| api_url("/v1/devices/device-id/property-time-intervals"), | ||
| json=[], | ||
| ) | ||
|
|
||
| client = Client("test") | ||
| client.get_device_custom_property_time_intervals( | ||
| device_id="device-id", | ||
| project_id="project-id", | ||
| key=["env", "region"], | ||
| ) | ||
|
|
||
| assert parse_qs(urlparse(responses.calls[0].request.url).query) == { | ||
| "key": ["env,region"], | ||
| "projectId": ["project-id"], | ||
| } | ||
|
|
||
|
|
||
| @responses.activate | ||
| def test_update_device_custom_property_time_interval_omits_value_for_clear(): | ||
| device_name = "Device / Name" | ||
| responses.add( | ||
| responses.POST, | ||
| api_url( | ||
| f"/v1/actions/devices/{quote(device_name, safe='')}/update-property-time-interval" | ||
| ), | ||
| status=204, | ||
| body="", | ||
| ) | ||
|
|
||
| client = Client("test") | ||
| client.update_device_custom_property_time_interval( | ||
| device_name=device_name, | ||
| project_id="project-id", | ||
| key="env", | ||
| start=datetime.now(), | ||
| end=datetime.now(), | ||
| ) | ||
|
|
||
| request_body = json.loads(responses.calls[0].request.body) | ||
| assert request_body["projectId"] == "project-id" | ||
| assert request_body["key"] == "env" | ||
| assert "value" not in request_body | ||
| assert "deviceName" not in request_body | ||
| assert "deviceId" not in request_body | ||
|
|
||
|
|
||
| @responses.activate | ||
| def test_update_device_custom_property_time_interval_supports_array_values(): | ||
| responses.add( | ||
| responses.POST, | ||
| api_url("/v1/actions/devices/device-id/update-property-time-interval"), | ||
| status=204, | ||
| body="", | ||
| ) | ||
|
|
||
| client = Client("test") | ||
| client.update_device_custom_property_time_interval( | ||
| device_id="device-id", | ||
| key="labels", | ||
| value=["one", "two"], | ||
| start=datetime.now(), | ||
| end=datetime.now(), | ||
| ) | ||
|
|
||
| request_body = json.loads(responses.calls[0].request.body) | ||
| assert request_body["value"] == ["one", "two"] | ||
| assert "deviceName" not in request_body | ||
| assert "deviceId" not in request_body | ||
Uh oh!
There was an error while loading. Please reload this page.