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
25 changes: 15 additions & 10 deletions src/mock_vws/_flask_server/vws.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
)
from mock_vws._database_matchers import get_database_matching_server_keys
from mock_vws._matching import matching_targets
from mock_vws._mock_common import RequestData, json_dump, sorted_targets
from mock_vws._mock_common import (
RequestData,
json_dump,
sorted_targets,
)
from mock_vws._model_target_web_api import (
create_model_target_dataset,
delete_model_target_dataset,
Expand Down Expand Up @@ -77,6 +81,7 @@
StructuralSimilarityMatcher,
)
from mock_vws.model_target import (
JSONValue,
ModelTargetDataset,
ModelTargetDatasetType,
OAuth2ClientCredential,
Expand Down Expand Up @@ -855,7 +860,7 @@ def get_target(target_id: str) -> Response:
width = target.width
tracking_rating = target.tracking_rating
reco_rating = target.reco_rating
target_record = {
target_record: dict[str, JSONValue] = {
"target_id": target.target_id,
"active_flag": target.active_flag,
"name": target.name,
Expand All @@ -875,7 +880,7 @@ def get_target(target_id: str) -> Response:
"x-aws-region": "us-east-2, us-west-2",
"x-content-type-options": "nosniff",
}
body = {
body: dict[str, JSONValue] = {
"result_code": ResultCodes.SUCCESS.value,
"transaction_id": uuid.uuid4().hex,
"target_record": target_record,
Expand Down Expand Up @@ -922,7 +927,7 @@ def delete_target(target_id: str) -> Response:
timeout=30,
)

body = {
body: dict[str, JSONValue] = {
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.SUCCESS.value,
}
Expand Down Expand Up @@ -1030,7 +1035,7 @@ def database_summary() -> Response:
databases=databases,
)

body = {
body: dict[str, JSONValue] = {
"result_code": ResultCodes.SUCCESS.value,
"transaction_id": uuid.uuid4().hex,
"name": database.database_name,
Expand Down Expand Up @@ -1093,7 +1098,7 @@ def target_summary(target_id: str) -> Response:
total_recos = target.total_recos
current_month_recos = target.current_month_recos
previous_month_recos = target.previous_month_recos
body = {
body: dict[str, JSONValue] = {
"status": target.status,
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.SUCCESS.value,
Expand Down Expand Up @@ -1157,7 +1162,7 @@ def get_duplicates(target_id: str) -> Response:
and other.active_flag
}

similar_targets = [
similar_targets: list[JSONValue] = [
other.target_id
for other in matching_targets(
matcher=image_match_checker,
Expand All @@ -1166,7 +1171,7 @@ def get_duplicates(target_id: str) -> Response:
)
]

body = {
body: dict[str, JSONValue] = {
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.SUCCESS.value,
"similar_targets": similar_targets,
Expand Down Expand Up @@ -1206,12 +1211,12 @@ def target_list() -> Response:
request_path=request.path,
databases=databases,
)
results = [
results: list[JSONValue] = [
target.target_id
for target in sorted_targets(targets=database.not_deleted_targets)
]

body = {
body: dict[str, JSONValue] = {
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.SUCCESS.value,
"results": results,
Expand Down
5 changes: 3 additions & 2 deletions src/mock_vws/_mock_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from collections.abc import Callable, Iterable, Mapping
from dataclasses import dataclass
from functools import partial
from typing import Any, Final, override
from typing import Final, override

from beartype import beartype

from mock_vws._constants import ResultCodes
from mock_vws.model_target import JSONValue
from mock_vws.target import ImageTarget

# A database ID as it appears in the path of a reco counts report request.
Expand Down Expand Up @@ -174,7 +175,7 @@ def http_date() -> str:


@beartype
def json_dump(*, body: dict[str, Any]) -> str: # pyrefly: ignore [explicit-any]
def json_dump(*, body: Mapping[str, JSONValue]) -> str:
"""
Returns:
JSON dump of data in the same way that Vuforia dumps data.
Expand Down
20 changes: 8 additions & 12 deletions src/mock_vws/_query_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import base64
import uuid
from typing import Any

from beartype import beartype

Expand All @@ -12,6 +11,7 @@
from mock_vws._mock_common import json_dump
from mock_vws._query_validators import ValidatedQuery
from mock_vws.image_matchers import ImageMatcher
from mock_vws.model_target import JSONValue


@beartype
Expand Down Expand Up @@ -61,7 +61,7 @@ def get_query_match_response_text(
if match.tracking_rating > minimum_rating
]

results: list[dict[str, Any]] = [] # pyrefly: ignore [explicit-any]
results: list[JSONValue] = []
for target in matches:
target_timestamp = target.last_modified_date.timestamp()
if target.application_metadata is None:
Expand All @@ -70,28 +70,24 @@ def get_query_match_response_text(
application_metadata = base64.b64encode(
s=decode_base64(encoded_data=target.application_metadata),
).decode(encoding="ascii")
target_data = {
target_data: dict[str, JSONValue] = {
"target_timestamp": int(target_timestamp),
"name": target.name,
"application_metadata": application_metadata,
}

result: dict[str, JSONValue] = {
"target_id": target.target_id,
}
if include_target_data == "all" or (
include_target_data == "top" and not bool(results)
):
result = {
"target_id": target.target_id,
"target_data": target_data,
}
else:
result = {
"target_id": target.target_id,
}
result["target_data"] = target_data

results.append(result)

results = results[: int(max_num_results)]
body = {
body: dict[str, JSONValue] = {
"result_code": ResultCodes.SUCCESS.value,
"results": results,
"query_id": uuid.uuid4().hex,
Expand Down
19 changes: 10 additions & 9 deletions src/mock_vws/_requests_mock_server/mock_web_services_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
from mock_vws.database import VuMarkDatabase
from mock_vws.image_matchers import ImageMatcher
from mock_vws.model_target import (
JSONValue,
ModelTargetDatasetType,
ModelTargetFailureResponse,
ModelTargetGenerationFailure,
Expand Down Expand Up @@ -598,7 +599,7 @@ def add_target(self, request: RequestData) -> _ResponseType:
usegmt=True,
)
status_code = HTTPStatus.CREATED
body = {
body: dict[str, JSONValue] = {
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.TARGET_CREATED.value,
"target_id": new_target.target_id,
Expand Down Expand Up @@ -784,7 +785,7 @@ def database_summary(self, request: RequestData) -> _ResponseType:
localtime=False,
usegmt=True,
)
body = {
body: dict[str, JSONValue] = {
"result_code": ResultCodes.SUCCESS.value,
"transaction_id": uuid.uuid4().hex,
"name": database.database_name,
Expand Down Expand Up @@ -839,11 +840,11 @@ def target_list(self, request: RequestData) -> _ResponseType:
usegmt=True,
)

response_results = [
response_results: list[JSONValue] = [
target.target_id
for target in sorted_targets(targets=database.not_deleted_targets)
]
body = {
body: dict[str, JSONValue] = {
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.SUCCESS.value,
"results": response_results,
Expand Down Expand Up @@ -889,7 +890,7 @@ def get_target(self, request: RequestData) -> _ResponseType:
width = target.width
tracking_rating = target.tracking_rating
reco_rating = target.reco_rating
target_record = {
target_record: dict[str, JSONValue] = {
"target_id": target.target_id,
"active_flag": target.active_flag,
"name": target.name,
Expand All @@ -903,7 +904,7 @@ def get_target(self, request: RequestData) -> _ResponseType:
usegmt=True,
)

body = {
body: dict[str, JSONValue] = {
"result_code": ResultCodes.SUCCESS.value,
"transaction_id": uuid.uuid4().hex,
"target_record": target_record,
Expand Down Expand Up @@ -956,7 +957,7 @@ def get_duplicates(self, request: RequestData) -> _ResponseType:
and other.active_flag
}

similar_targets = [
similar_targets: list[JSONValue] = [
other.target_id
for other in matching_targets(
matcher=self._duplicate_match_checker,
Expand All @@ -970,7 +971,7 @@ def get_duplicates(self, request: RequestData) -> _ResponseType:
localtime=False,
usegmt=True,
)
body = {
body: dict[str, JSONValue] = {
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.SUCCESS.value,
"similar_targets": similar_targets,
Expand Down Expand Up @@ -1132,7 +1133,7 @@ def target_summary(self, request: RequestData) -> _ResponseType:
total_recos = target.total_recos
current_month_recos = target.current_month_recos
previous_month_recos = target.previous_month_recos
body = {
body: dict[str, JSONValue] = {
"status": target.status,
"transaction_id": uuid.uuid4().hex,
"result_code": ResultCodes.SUCCESS.value,
Expand Down
Loading