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
60 changes: 60 additions & 0 deletions tests/mock_vws/fixtures/vuforia_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from tests.mock_vws.utils.model_target_retries import (
retrying_transient_real_backend_failures,
)
from tests.mock_vws.utils.recognition_counts import SeedRecognitionCounts
from tests.mock_vws.utils.retries import (
RETRY_ON_TARGET_STILL_PROCESSING,
RETRY_ON_TRANSIENT_VWS_FAILURE,
Expand Down Expand Up @@ -571,3 +572,62 @@ def add_flask_cloud_database(*, cloud_database: CloudDatabase) -> None:
response.raise_for_status()

return add_flask_cloud_database


@pytest.fixture(name="seed_recognition_counts")
def fixture_seed_recognition_counts(
*,
mock_only_vuforia: VuforiaBackend,
) -> SeedRecognitionCounts:
"""The recognition-count operation for the running mock backend."""
if mock_only_vuforia == VuforiaBackend.MOCK:

@beartype
def seed_in_memory_counts(
*,
vuforia_database: CloudDatabase,
target_id: str,
current_month_recos: int,
previous_month_recos: int,
total_recos: int,
) -> None:
"""Set recognition counts through the running in-memory
mock.
"""
del vuforia_database
running_in_memory_mock().set_target_recognition_counts(
target_id=target_id,
current_month_recos=current_month_recos,
previous_month_recos=previous_month_recos,
total_recos=total_recos,
)

return seed_in_memory_counts

@beartype
def seed_flask_counts(
*,
vuforia_database: CloudDatabase,
target_id: str,
current_month_recos: int,
previous_month_recos: int,
total_recos: int,
) -> None:
"""Set recognition counts through the target manager service."""
target_manager_base_url = os.environ["TARGET_MANAGER_BASE_URL"]
database_name = vuforia_database.database_name
response = requests.post(
url=(
f"{target_manager_base_url}/cloud_databases/"
f"{database_name}/targets/{target_id}/recognition_counts"
),
json={
"current_month_recos": current_month_recos,
"previous_month_recos": previous_month_recos,
"total_recos": total_recos,
},
timeout=30,
)
response.raise_for_status()

return seed_flask_counts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from mock_vws import MockVWS
from mock_vws.database import CloudDatabase
from tests.mock_vws.fixtures.vuforia_backends import VuforiaBackend
from tests.mock_vws.reco_counts_report.helpers import (
CSV_HEADER,
CURRENT_MONTH_RECOS,
Expand All @@ -32,7 +31,7 @@
report_key,
wait_for_report,
)
from tests.mock_vws.utils.recognition_counts import seed_recognition_counts
from tests.mock_vws.utils.recognition_counts import SeedRecognitionCounts


def test_not_ready(*, fresh_database: CloudDatabase) -> None:
Expand Down Expand Up @@ -85,7 +84,7 @@ def test_unknown_report(*, vuforia_database: CloudDatabase) -> None:
)
def test_seeded_recognition_counts(
*,
mock_only_vuforia: VuforiaBackend,
seed_recognition_counts: SeedRecognitionCounts,
fresh_database: CloudDatabase,
high_quality_image: io.BytesIO,
image_file_failed_state: io.BytesIO,
Expand All @@ -111,7 +110,6 @@ def test_seeded_recognition_counts(
application_metadata=None,
)
seed_recognition_counts(
backend=mock_only_vuforia,
vuforia_database=fresh_database,
target_id=recognized_target_id,
current_month_recos=CURRENT_MONTH_RECOS,
Expand All @@ -133,7 +131,7 @@ def test_seeded_recognition_counts(

def test_counts_set_after_first_request(
*,
mock_only_vuforia: VuforiaBackend,
seed_recognition_counts: SeedRecognitionCounts,
fresh_database: CloudDatabase,
high_quality_image: io.BytesIO,
) -> None:
Expand All @@ -154,7 +152,6 @@ def test_counts_set_after_first_request(
month=month,
)
seed_recognition_counts(
backend=mock_only_vuforia,
vuforia_database=fresh_database,
target_id=target_id,
current_month_recos=CURRENT_MONTH_RECOS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from vws.reports import TargetStatuses

from mock_vws.database import CloudDatabase
from tests.mock_vws.fixtures.vuforia_backends import VuforiaBackend
from tests.mock_vws.utils.recognition_counts import seed_recognition_counts
from tests.mock_vws.utils.recognition_counts import SeedRecognitionCounts

_CURRENT_MONTH_RECOS = 3
_PREVIOUS_MONTH_RECOS = 5
Expand All @@ -23,7 +22,7 @@

def test_seeded_counts(
*,
mock_only_vuforia: VuforiaBackend,
seed_recognition_counts: SeedRecognitionCounts,
vws_client: VWS,
vuforia_database: CloudDatabase,
high_quality_image: io.BytesIO,
Expand All @@ -39,7 +38,6 @@ def test_seeded_counts(
vws_client.wait_for_target_processed(target_id=target_id)

seed_recognition_counts(
backend=mock_only_vuforia,
vuforia_database=vuforia_database,
target_id=target_id,
current_month_recos=_CURRENT_MONTH_RECOS,
Expand Down
89 changes: 28 additions & 61 deletions tests/mock_vws/utils/recognition_counts.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,37 @@
"""Helpers for seeding recognition counts on a target.
"""The recognition-count operation supplied by a mock backend.

Neither mock counts recognitions, because real Vuforia's counts lag behind
its queries by longer than a test runs. Each mock instead has its own way of
being told what the counts are, and this module hides that difference from
the tests.
being told what the counts are.
"""

import os

import requests
from beartype import beartype
from typing import Protocol, runtime_checkable

from mock_vws.database import CloudDatabase
from tests.mock_vws.fixtures.vuforia_backends import (
VuforiaBackend,
running_in_memory_mock,
)


@beartype
def seed_recognition_counts(
*,
backend: VuforiaBackend,
vuforia_database: CloudDatabase,
target_id: str,
current_month_recos: int,
previous_month_recos: int,
total_recos: int,
) -> None:
"""Set the recognition counts of a target on a mock backend.

Args:
backend: The mock backend which the test is running against. Real
Vuforia's recognition counts cannot be set, so tests which seed
counts use ``mock_only_vuforia``.
vuforia_database: The database which the target is in.
target_id: The ID of the target to set recognition counts of.
current_month_recos: The number of recognitions of the target in the
current month.
previous_month_recos: The number of recognitions of the target in the
previous month.
total_recos: The total number of recognitions of the target.
"""
if backend == VuforiaBackend.MOCK:
running_in_memory_mock().set_target_recognition_counts(
target_id=target_id,
current_month_recos=current_month_recos,
previous_month_recos=previous_month_recos,
total_recos=total_recos,
)
return

# The Flask and Docker mock keeps its targets in the target manager
# service, which is where its recognition counts are set.
target_manager_base_url = os.environ["TARGET_MANAGER_BASE_URL"]
database_name = vuforia_database.database_name
response = requests.post(
url=(
f"{target_manager_base_url}/cloud_databases/"
f"{database_name}/targets/{target_id}/recognition_counts"
),
json={
"current_month_recos": current_month_recos,
"previous_month_recos": previous_month_recos,
"total_recos": total_recos,
},
timeout=30,
)
response.raise_for_status()
@runtime_checkable
class SeedRecognitionCounts(Protocol):
"""A backend operation which sets recognition counts on a target."""

def __call__(
self,
*,
vuforia_database: CloudDatabase,
target_id: str,
current_month_recos: int,
previous_month_recos: int,
total_recos: int,
) -> None:
"""Set the target's recognition counts on the running mock backend.

Args:
vuforia_database: The database which the target is in.
target_id: The ID of the target to set recognition counts of.
current_month_recos: The number of recognitions in the current
month.
previous_month_recos: The number of recognitions in the previous
month.
total_recos: The total number of recognitions of the target.
"""
...
Loading