Skip to content

Commit

Permalink
Map versioned client names to versioned fixtures
Browse files Browse the repository at this point in the history
When mapping client objects (by name) to directories in the _testing
data modules, the current mapping inspects `service_name` and the
method name. As a result, it's not possible to have multiple versioned
clients with the same service name mapped to different fixture
directories.
To resolve, we need *some* solution which maps versioned clients to
versioned fixtures.

This change applies one particular solution, which will work in the
near-term for ComputeClientV2 and ComputeClientV3, but may be refined
further in the future (so long as the mapping of the V2 client to
known fixture data is preserved).

Rather than ignoring the possibility of structured information in the
name of a client, if it ends with `V<N>` for some integer `N`, we can
map this to a fixture dir named, similarly, `v<N>` (lowercased). This
allows the V2 and V3 clients to get their own fixture directories.

One downside of this approach is that it maps `ComputeClient` (which
inherits from the V2 client) differently from `ComputeClientV2`, and
there is no step here taken to rectify this. Effectively, this means
that `ComputeClient` will get no fixture data, but `ComputeClientV2`
will, even for identical usages.

Alternative approaches abound -- e.g., we could attach a class-level
attribute which declares a "test_fixture_subdir" or "api_version", and
use that attribute when present. The current solution was chosen as
- generic, in that it does not encode Compute-specific details into a
  helper buried in `_testing`
- easy to implement and reason about (it's just a quick suffix match
  and string conversion)
- easy to change or replace in the future, because it makes no
  changes to the classes which are fed into this mapping
  • Loading branch information
sirosen authored and rjmello committed Nov 5, 2024
1 parent 4fb7012 commit 7d63262
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/globus_sdk/_testing/registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import importlib
import re
import typing as t

import responses
Expand All @@ -9,6 +10,10 @@

from .models import RegisteredResponse, ResponseList, ResponseSet

# matches "V2", "V11", etc as a string suffix
# see usage in _resolve_qualname for details
_SUFFIX_VERSION_MATCH_PATTERN = re.compile(r"V\d+$")

_RESPONSE_SET_REGISTRY: dict[t.Any, ResponseSet] = {}


Expand Down Expand Up @@ -53,6 +58,16 @@ def _resolve_qualname(name: str) -> str:

assert issubclass(maybe_client, globus_sdk.BaseClient)
service_name = maybe_client.service_name

# TODO: Consider alternative strategies for mapping versioned clients
# to subdirs. For now, we do it by name matching.
#
# 'prefix' is the client name, and it may end in `V2`, `V3`, etc.
# in which case we want to map it to a subdir
suffix_version_match = _SUFFIX_VERSION_MATCH_PATTERN.search(prefix)
if suffix_version_match:
suffix = f"{suffix_version_match.group(0).lower()}.{suffix}"

return f"{service_name}.{suffix}"


Expand Down

0 comments on commit 7d63262

Please sign in to comment.