Skip to content

Commit

Permalink
Variable recording class
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp committed Jul 20, 2022
1 parent 32816d7 commit bb3a48c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
3 changes: 2 additions & 1 deletion tools/azure-sdk-tools/devtools_testutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .envvariable_loader import EnvironmentVariableLoader
PowerShellPreparer = EnvironmentVariableLoader # Backward compat
from .proxy_startup import start_test_proxy, stop_test_proxy, test_proxy
from .proxy_testcase import recorded_by_proxy, recorded_test, variable_recorder
from .proxy_testcase import recorded_by_proxy, recorded_test
from .sanitizers import (
add_body_key_sanitizer,
add_body_regex_sanitizer,
Expand All @@ -34,6 +34,7 @@
set_custom_default_matcher,
set_default_settings,
)
from .variable_recorder import variable_recorder
from .helpers import ResponseCallback, RetryCounter
from .fake_credentials import FakeTokenCredential

Expand Down
15 changes: 0 additions & 15 deletions tools/azure-sdk-tools/devtools_testutils/proxy_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,3 @@ def restore_traffic(original_transport_func: "Callable", request: "FixtureReques
message = error_body.get("message") or error_body.get("Message")
logger = logging.getLogger()
logger.error(f"\n\n-----Test proxy playback error:-----\n\n{message}")


@pytest.fixture
def variable_recorder(recorded_test: "Dict[str, Any]") -> "Dict[str, str]":
"""Fixture that invokes the `recorded_test` fixture and returns a dictionary of recorded test variables.
:param recorded_test: The fixture responsible for redirecting network traffic to target the test proxy.
This should return a dictionary containing information about the current test -- in particular, the variables
that were recorded with the test.
:type recorded_test: Dict[str, Any]
:returns: A dictionary that maps test variables to string values. If no variable dictionary was stored when the test
was recorded, this returns an empty dictionary.
"""
return recorded_test["variables"]
49 changes: 49 additions & 0 deletions tools/azure-sdk-tools/devtools_testutils/variable_recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from typing import TYPE_CHECKING

import pytest

from .proxy_testcase import recorded_test

if TYPE_CHECKING:
from typing import Any, Dict


class VariableRecorder():
def __init__(self, variables: "Dict[str, str]") -> None:
self.variables = variables

def get_or_record(self, variable: str, default: str) -> str:
"""Returns the recorded value of `variable`, or records and returns `default` as the value for `variable`.
In recording mode, `get_or_record("a", "b")` will record "b" for the value of the variable `a` and return "b".
In playback, it will return the recorded value of `a`. This is an analogue of a Python dictionary's `setdefault`
method: https://docs.python.org/library/stdtypes.html#dict.setdefault.
:param str variable: The name of the variable to search the value of, or record a value for.
:param str default: The variable value to record.
:returns: str
"""
if not isinstance(default, str):
raise ValueError('"default" must be a string. The test proxy cannot record non-string variable values.')
return self.variables.setdefault(variable, default)


@pytest.fixture
def variable_recorder(recorded_test: "Dict[str, Any]") -> "Dict[str, str]":
"""Fixture that invokes the `recorded_test` fixture and returns a dictionary of recorded test variables.
:param recorded_test: The fixture responsible for redirecting network traffic to target the test proxy.
This should return a dictionary containing information about the current test -- in particular, the variables
that were recorded with the test.
:type recorded_test: Dict[str, Any]
:returns: A dictionary that maps test variables to string values. If no variable dictionary was stored when the test
was recorded, this returns an empty dictionary.
"""
return recorded_test["variables"]

0 comments on commit bb3a48c

Please sign in to comment.