Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/vws/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io
import json
from http import HTTPMethod, HTTPStatus
from typing import Any
from typing import Any, BinaryIO
from urllib.parse import urljoin

import requests
Expand All @@ -28,7 +28,7 @@
from vws.include_target_data import CloudRecoIncludeTargetData
from vws.reports import QueryResult, TargetData

_ImageType = io.BytesIO | io.BufferedRandom
_ImageType = io.BytesIO | BinaryIO


def _get_image_data(image: _ImageType) -> bytes:
Expand Down
3 changes: 2 additions & 1 deletion src/vws/vws.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
from datetime import date
from http import HTTPMethod, HTTPStatus
from typing import BinaryIO
from urllib.parse import urljoin

import requests
Expand Down Expand Up @@ -47,7 +48,7 @@

from .exceptions.response import Response

_ImageType = io.BytesIO | io.BufferedRandom
_ImageType = io.BytesIO | BinaryIO


def _get_image_data(image: _ImageType) -> bytes:
Expand Down
13 changes: 8 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io
from collections.abc import Generator
from pathlib import Path
from typing import BinaryIO, Literal

import pytest
from mock_vws import MockVWS
Expand Down Expand Up @@ -47,25 +48,27 @@ def cloud_reco_client(_mock_database: VuforiaDatabase) -> CloudRecoService:
)


@pytest.fixture(name="image_file")
@pytest.fixture(name="image_file", params=["r+b", "rb"])
def image_file_fixture(
high_quality_image: io.BytesIO,
tmp_path: Path,
) -> Generator[io.BufferedRandom, None, None]:
request: pytest.FixtureRequest,
) -> Generator[BinaryIO, None, None]:
"""An image file object."""
file = tmp_path / "image.jpg"
buffer = high_quality_image.getvalue()
file.write_bytes(data=buffer)
with file.open(mode="r+b") as file_obj:
mode: Literal["r+b", "rb"] = request.param
with file.open(mode=mode) as file_obj:
yield file_obj


@pytest.fixture(params=["high_quality_image", "image_file"])
def image(
request: pytest.FixtureRequest,
high_quality_image: io.BytesIO,
image_file: io.BufferedRandom,
) -> io.BytesIO | io.BufferedRandom:
image_file: BinaryIO,
) -> io.BytesIO | BinaryIO:
"""An image in any of the types that the API accepts."""
if request.param == "high_quality_image":
return high_quality_image
Expand Down
19 changes: 10 additions & 9 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io
import uuid
from typing import BinaryIO

from mock_vws import MockVWS
from mock_vws.database import VuforiaDatabase
Expand All @@ -20,7 +21,7 @@ class TestQuery:
@staticmethod
def test_no_matches(
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
An empty list is returned if there are no matches.
Expand All @@ -32,7 +33,7 @@ def test_no_matches(
def test_match(
vws_client: VWS,
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
Details of matching targets are returned.
Expand All @@ -55,7 +56,7 @@ class TestCustomBaseVWQURL:
"""

@staticmethod
def test_custom_base_url(image: io.BytesIO | io.BufferedRandom) -> None:
def test_custom_base_url(image: io.BytesIO | BinaryIO) -> None:
"""
It is possible to use query a target to a database under a custom VWQ
URL.
Expand Down Expand Up @@ -100,7 +101,7 @@ class TestMaxNumResults:
def test_default(
vws_client: VWS,
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
By default the maximum number of results is 1.
Expand Down Expand Up @@ -128,7 +129,7 @@ def test_default(
def test_custom(
vws_client: VWS,
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
It is possible to set a custom ``max_num_results``.
Expand Down Expand Up @@ -174,7 +175,7 @@ class TestIncludeTargetData:
def test_default(
vws_client: VWS,
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
By default, target data is only returned in the top match.
Expand Down Expand Up @@ -206,7 +207,7 @@ def test_default(
def test_top(
vws_client: VWS,
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
When ``CloudRecoIncludeTargetData.TOP`` is given, target data is only
Expand Down Expand Up @@ -240,7 +241,7 @@ def test_top(
def test_none(
vws_client: VWS,
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
When ``CloudRecoIncludeTargetData.NONE`` is given, target data is not
Expand Down Expand Up @@ -274,7 +275,7 @@ def test_none(
def test_all(
vws_client: VWS,
cloud_reco_client: CloudRecoService,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
When ``CloudRecoIncludeTargetData.ALL`` is given, target data is
Expand Down
29 changes: 15 additions & 14 deletions tests/test_vws.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io
import secrets
import uuid
from typing import BinaryIO

import pytest
from freezegun import freeze_time
Expand All @@ -33,7 +34,7 @@ class TestAddTarget:
@pytest.mark.parametrize("active_flag", [True, False])
def test_add_target(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
application_metadata: bytes | None,
cloud_reco_client: CloudRecoService,
*,
Expand Down Expand Up @@ -77,7 +78,7 @@ def test_add_target(
@staticmethod
def test_add_two_targets(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
No exception is raised when adding two targets with different names.
Expand All @@ -100,7 +101,7 @@ class TestCustomBaseVWSURL:
"""

@staticmethod
def test_custom_base_url(image: io.BytesIO | io.BufferedRandom) -> None:
def test_custom_base_url(image: io.BytesIO | BinaryIO) -> None:
"""
It is possible to use add a target to a database under a custom VWS
URL.
Expand Down Expand Up @@ -132,7 +133,7 @@ class TestListTargets:
@staticmethod
def test_list_targets(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
It is possible to get a list of target IDs.
Expand Down Expand Up @@ -162,7 +163,7 @@ class TestDelete:
@staticmethod
def test_delete_target(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
It is possible to delete a target.
Expand All @@ -189,7 +190,7 @@ class TestGetTargetSummaryReport:
@staticmethod
def test_get_target_summary_report(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
Details of a target are returned by ``get_target_summary_report``.
Expand Down Expand Up @@ -258,7 +259,7 @@ class TestGetTargetRecord:
@staticmethod
def test_get_target_record(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
Details of a target are returned by ``get_target_record``.
Expand Down Expand Up @@ -293,7 +294,7 @@ class TestWaitForTargetProcessed:
@staticmethod
def test_wait_for_target_processed(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
It is possible to wait until a target is processed.
Expand All @@ -313,7 +314,7 @@ def test_wait_for_target_processed(

@staticmethod
def test_default_seconds_between_requests(
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
By default, 0.2 seconds are waited between polling requests.
Expand Down Expand Up @@ -365,7 +366,7 @@ def test_default_seconds_between_requests(

@staticmethod
def test_custom_seconds_between_requests(
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
It is possible to customize the time waited between polling requests.
Expand Down Expand Up @@ -416,7 +417,7 @@ def test_custom_seconds_between_requests(
assert report.request_usage == expected_requests

@staticmethod
def test_custom_timeout(image: io.BytesIO | io.BufferedRandom) -> None:
def test_custom_timeout(image: io.BytesIO | BinaryIO) -> None:
"""
It is possible to set a maximum timeout.
"""
Expand Down Expand Up @@ -460,7 +461,7 @@ class TestGetDuplicateTargets:
@staticmethod
def test_get_duplicate_targets(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
It is possible to get the IDs of similar targets.
Expand Down Expand Up @@ -494,7 +495,7 @@ class TestUpdateTarget:
@staticmethod
def test_update_target(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
different_high_quality_image: io.BytesIO,
cloud_reco_client: CloudRecoService,
) -> None:
Expand Down Expand Up @@ -553,7 +554,7 @@ def test_update_target(
@staticmethod
def test_no_fields_given(
vws_client: VWS,
image: io.BytesIO | io.BufferedRandom,
image: io.BytesIO | BinaryIO,
) -> None:
"""
It is possible to give no update fields.
Expand Down