Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from viam.media.video import CameraMimeType, NamedImage, ViamImage
from viam.proto.common import DoCommandRequest, DoCommandResponse, GetGeometriesRequest, GetGeometriesResponse, ResponseMetadata
from viam.proto.component.camera import (
Format,
CameraServiceStub,
DistortionParameters,
Format,
GetImageRequest,
GetImageResponse,
GetImagesRequest,
Expand Down
112 changes: 86 additions & 26 deletions tests/test_vision_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
),
]

VISION_IMAGE = ViamImage(bytes([0, 100]), CameraMimeType.JPEG)
# Use string value of CameraMimeType because ViamImage accepts a string mime type in the worst case
# and it may not have the expected CameraMimeType methods defined on it.
VISION_IMAGE = ViamImage(bytes([0, 100]), CameraMimeType.JPEG.value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still curious why we want to use a second VISION_IMAGE here when we already have an IMAGE variable (used in other tests) that could be used in the same way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's to make writing the mock easier? @njooma ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't have an answer to this question, so won't approve until then


PROPERTIES = Vision.Properties(
classifications_supported=True,
Expand Down Expand Up @@ -174,7 +176,9 @@ async def test_get_detections(self, vision: MockVision):

async def test_get_classifications_from_camera(self, vision: MockVision):
extra = {"foo": "get_classifications_from_camera"}
response = await vision.get_classifications_from_camera("fake-camera", 1, extra=extra)
response = await vision.get_classifications_from_camera(
"fake-camera", 1, extra=extra
)
assert response == CLASSIFICATIONS
assert vision.extra == extra

Expand All @@ -197,16 +201,25 @@ async def test_do(self, vision: MockVision):


class TestService:
async def test_capture_all_from_camera(self, vision: MockVision, service: VisionRPCService):
async def test_capture_all_from_camera(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionServiceStub(channel)
extra = {"foo": "capture_all_from_camera"}
request = CaptureAllFromCameraRequest(
name=vision.name, camera_name="fake-camera", return_image=True, return_classifications=True, extra=dict_to_struct(extra)
name=vision.name,
camera_name="fake-camera",
return_image=True,
return_classifications=True,
extra=dict_to_struct(extra),
)
response: CaptureAllFromCameraResponse = await client.CaptureAllFromCamera(
request
)
response: CaptureAllFromCameraResponse = await client.CaptureAllFromCamera(request)
assert response.image.image == VISION_IMAGE.data
assert response.image.mime_type == VISION_IMAGE.mime_type
# TODO(RSDK-11728): remove this once we deleted the format field
assert response.image.format == VISION_IMAGE.mime_type.to_proto()
assert response.classifications == CLASSIFICATIONS
assert response.detections == []
Expand All @@ -217,19 +230,33 @@ async def test_get_properties(self, vision: MockVision, service: VisionRPCServic
async with ChannelFor([service]) as channel:
client = VisionServiceStub(channel)
extra = {"foo": "get_properties"}
request = GetPropertiesRequest(name=vision.name, extra=dict_to_struct(extra))
request = GetPropertiesRequest(
name=vision.name, extra=dict_to_struct(extra)
)
response: GetPropertiesResponse = await client.GetProperties(request)
assert response.classifications_supported == PROPERTIES.classifications_supported
assert (
response.classifications_supported
== PROPERTIES.classifications_supported
)
assert response.detections_supported == PROPERTIES.detections_supported
assert response.object_point_clouds_supported == PROPERTIES.object_point_clouds_supported
assert (
response.object_point_clouds_supported
== PROPERTIES.object_point_clouds_supported
)
assert vision.extra == extra

async def test_get_detections_from_camera(self, vision: MockVision, service: VisionRPCService):
async def test_get_detections_from_camera(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionServiceStub(channel)
extra = {"foo": "get_detections_from_camera"}
request = GetDetectionsFromCameraRequest(name=vision.name, camera_name="fake-camera", extra=dict_to_struct(extra))
response: GetDetectionsFromCameraResponse = await client.GetDetectionsFromCamera(request)
request = GetDetectionsFromCameraRequest(
name=vision.name, camera_name="fake-camera", extra=dict_to_struct(extra)
)
response: GetDetectionsFromCameraResponse = (
await client.GetDetectionsFromCamera(request)
)
assert response.detections == DETECTIONS
assert vision.extra == extra

Expand All @@ -249,16 +276,27 @@ async def test_get_detections(self, vision: MockVision, service: VisionRPCServic
assert response.detections == DETECTIONS
assert vision.extra == extra

async def test_get_classifications_from_camera(self, vision: MockVision, service: VisionRPCService):
async def test_get_classifications_from_camera(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionServiceStub(channel)
extra = {"foo": "get_classifications_from_camera"}
request = GetClassificationsFromCameraRequest(name=vision.name, camera_name="fake-camera", n=1, extra=dict_to_struct(extra))
response: GetClassificationsFromCameraResponse = await client.GetClassificationsFromCamera(request)
request = GetClassificationsFromCameraRequest(
name=vision.name,
camera_name="fake-camera",
n=1,
extra=dict_to_struct(extra),
)
response: GetClassificationsFromCameraResponse = (
await client.GetClassificationsFromCamera(request)
)
assert response.classifications == CLASSIFICATIONS
assert vision.extra == extra

async def test_get_classifications(self, vision: MockVision, service: VisionRPCService):
async def test_get_classifications(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionServiceStub(channel)
extra = {"foo": "get_classifications"}
Expand All @@ -271,11 +309,15 @@ async def test_get_classifications(self, vision: MockVision, service: VisionRPCS
n=1,
extra=dict_to_struct(extra),
)
response: GetClassificationsResponse = await client.GetClassifications(request)
response: GetClassificationsResponse = await client.GetClassifications(
request
)
assert response.classifications == CLASSIFICATIONS
assert vision.extra == extra

async def test_get_object_point_clouds(self, vision: MockVision, service: VisionRPCService):
async def test_get_object_point_clouds(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionServiceStub(channel)
extra = {"foo": "get_object_point_clouds"}
Expand All @@ -285,15 +327,19 @@ async def test_get_object_point_clouds(self, vision: MockVision, service: Vision
mime_type=CameraMimeType.PCD,
extra=dict_to_struct(extra),
)
response: GetObjectPointCloudsResponse = await client.GetObjectPointClouds(request)
response: GetObjectPointCloudsResponse = await client.GetObjectPointClouds(
request
)
assert response.objects == POINT_CLOUDS
assert vision.extra == extra

async def test_do(self, vision: MockVision, service: VisionRPCService):
async with ChannelFor([service]) as channel:
client = VisionServiceStub(channel)
command = {"command": "args"}
request = DoCommandRequest(name=vision.name, command=dict_to_struct(command))
request = DoCommandRequest(
name=vision.name, command=dict_to_struct(command)
)
response: DoCommandResponse = await client.DoCommand(request)
assert struct_to_dict(response.result)["cmd"] == command

Expand All @@ -307,7 +353,9 @@ async def test_get_properties(self, vision: MockVision, service: VisionRPCServic
assert response == PROPERTIES
assert vision.extra == extra

async def test_capture_all_from_camera(self, vision: MockVision, service: VisionRPCService):
async def test_capture_all_from_camera(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionClient(VISION_SERVICE_NAME, channel)
extra = {"foo": "capture_all_from_camera"}
Expand All @@ -325,11 +373,15 @@ async def test_capture_all_from_camera(self, vision: MockVision, service: Vision
assert response.objects == POINT_CLOUDS
assert vision.extra == extra

async def test_get_detections_from_camera(self, vision: MockVision, service: VisionRPCService):
async def test_get_detections_from_camera(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionClient(VISION_SERVICE_NAME, channel)
extra = {"foo": "get_detections_from_camera"}
response = await client.get_detections_from_camera("fake-camera", extra=extra)
response = await client.get_detections_from_camera(
"fake-camera", extra=extra
)
assert response == DETECTIONS
assert vision.extra == extra

Expand All @@ -341,23 +393,31 @@ async def test_get_detections(self, vision: MockVision, service: VisionRPCServic
assert response == DETECTIONS
assert vision.extra == extra

async def test_get_classifications_from_camera(self, vision: MockVision, service: VisionRPCService):
async def test_get_classifications_from_camera(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionClient(VISION_SERVICE_NAME, channel)
extra = {"foo": "get_classifications_from_camera"}
response = await client.get_classifications_from_camera("fake-camera", 1, extra=extra)
response = await client.get_classifications_from_camera(
"fake-camera", 1, extra=extra
)
assert response == CLASSIFICATIONS
assert vision.extra == extra

async def test_get_classifications(self, vision: MockVision, service: VisionRPCService):
async def test_get_classifications(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionClient(VISION_SERVICE_NAME, channel)
extra = {"foo": "get_classifications"}
response = await client.get_classifications(IMAGE, 1, extra=extra)
assert response == CLASSIFICATIONS
assert vision.extra == extra

async def test_get_object_point_clouds(self, vision: MockVision, service: VisionRPCService):
async def test_get_object_point_clouds(
self, vision: MockVision, service: VisionRPCService
):
async with ChannelFor([service]) as channel:
client = VisionClient(VISION_SERVICE_NAME, channel)
extra = {"foo": "get_object_point_clouds"}
Expand Down
Loading