Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit c4f01be

Browse files
yugangw-msfttroydai
authored andcommitted
address review feedback
1 parent b737522 commit c4f01be

File tree

5 files changed

+45
-44
lines changed

5 files changed

+45
-44
lines changed

src/azure_devtools/scenario_tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
from .base import IntegrationTestBase, ReplayableTest, LiveTest
77
from .exceptions import AzureTestError
8-
from .decorators import live_only, record_only
8+
from .decorators import live_only, record_only, AllowLargeResponse
99
from .patches import mock_in_unit_test, patch_time_sleep_api, patch_long_run_operation_delay
10-
from .preparers import AbstractPreparer, SingleValueReplacer, AllowLargeResponse
10+
from .preparers import AbstractPreparer, SingleValueReplacer
1111
from .recording_processors import (
1212
RecordingProcessor, SubscriptionRecordingProcessor,
1313
LargeRequestBodyProcessor, LargeResponseBodyProcessor, LargeResponseBodyReplacer,

src/azure_devtools/scenario_tests/decorators.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# --------------------------------------------------------------------------------------------
55

66
import os
7+
import functools
78
import unittest
89
from .const import ENV_LIVE_TEST
10+
from .utilities import trim_kwargs_from_test_function
911

1012

1113
def live_only():
@@ -18,3 +20,25 @@ def record_only():
1820
return unittest.skipUnless(
1921
not os.environ.get(ENV_LIVE_TEST, False),
2022
'This test is excluded from being run live. To force a recording, please remove the recording file.')
23+
24+
25+
class AllowLargeResponse(object): # pylint: disable=too-few-public-methods
26+
27+
def __init__(self, size_kb=1024):
28+
self.size_kb = size_kb
29+
30+
def __call__(self, fn):
31+
def _preparer_wrapper(test_class_instance, **kwargs):
32+
from azure_devtools.scenario_tests import LargeResponseBodyProcessor
33+
large_resp_body = next((r for r in test_class_instance.recording_processors
34+
if isinstance(r, LargeResponseBodyProcessor)), None)
35+
if large_resp_body:
36+
large_resp_body._max_response_body = self.size_kb # pylint: disable=protected-access
37+
38+
trim_kwargs_from_test_function(fn, kwargs)
39+
40+
fn(test_class_instance, **kwargs)
41+
42+
setattr(_preparer_wrapper, '__is_preparer', True)
43+
functools.update_wrapper(_preparer_wrapper, fn)
44+
return _preparer_wrapper

src/azure_devtools/scenario_tests/preparers.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
# --------------------------------------------------------------------------------------------
55

66
import contextlib
7-
import inspect
87
import functools
98

109
from .base import ReplayableTest
11-
from .utilities import create_random_name, is_text_payload
10+
from .utilities import create_random_name, is_text_payload, trim_kwargs_from_test_function
1211
from .recording_processors import RecordingProcessor
1312

1413

@@ -48,7 +47,7 @@ def _preparer_wrapper(test_class_instance, **kwargs):
4847
if parameter_update:
4948
kwargs.update(parameter_update)
5049

51-
_trim_kwargs_from_test_function(fn, kwargs)
50+
trim_kwargs_from_test_function(fn, kwargs)
5251

5352
fn(test_class_instance, **kwargs)
5453

@@ -120,41 +119,3 @@ def process_response(self, response):
120119
self.replace_header(response, 'azure-asyncoperation', self.random_name, self.moniker)
121120

122121
return response
123-
124-
125-
class AllowLargeResponse(object): # pylint: disable=too-few-public-methods
126-
127-
def __init__(self, size_kb=1024):
128-
self.size_kb = size_kb
129-
130-
def __call__(self, fn):
131-
def _preparer_wrapper(test_class_instance, **kwargs):
132-
from azure_devtools.scenario_tests import LargeResponseBodyProcessor
133-
large_resp_body = next((r for r in test_class_instance.recording_processors
134-
if isinstance(r, LargeResponseBodyProcessor)), None)
135-
if large_resp_body:
136-
large_resp_body._max_response_body = self.size_kb # pylint: disable=protected-access
137-
138-
_trim_kwargs_from_test_function(fn, kwargs)
139-
140-
fn(test_class_instance, **kwargs)
141-
142-
setattr(_preparer_wrapper, '__is_preparer', True)
143-
functools.update_wrapper(_preparer_wrapper, fn)
144-
return _preparer_wrapper
145-
146-
# Utility
147-
148-
def _trim_kwargs_from_test_function(fn, kwargs):
149-
# the next function is the actual test function. the kwargs need to be trimmed so
150-
# that parameters which are not required will not be passed to it.
151-
if not is_preparer_func(fn):
152-
args, _, kw, _ = inspect.getargspec(fn) # pylint: disable=deprecated-method
153-
if kw is None:
154-
args = set(args)
155-
for key in [k for k in kwargs if k not in args]:
156-
del kwargs[key]
157-
158-
159-
def is_preparer_func(fn):
160-
return getattr(fn, '__is_preparer', False)

src/azure_devtools/scenario_tests/recording_processors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def process_response(self, response):
8787
if length > self._max_response_body * 1024:
8888

8989
if is_json_payload(response):
90-
from .preparers import AllowLargeResponse # pylint: disable=cyclic-import
90+
from .decorators import AllowLargeResponse # pylint: disable=cyclic-import
9191
raise ValueError("The json response body exceeds the default limit of {}kb. Use '@{}' "
9292
"on your test method to increase the limit or update test logics to avoid "
9393
"big payloads".format(self._max_response_body, AllowLargeResponse.__name__))

src/azure_devtools/scenario_tests/utilities.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import math
88
import os
99
import base64
10+
import inspect
1011

1112

1213
def create_random_name(prefix='aztest', length=24):
@@ -63,3 +64,18 @@ def is_text_payload(entity):
6364

6465
def is_json_payload(entity):
6566
return _get_content_type(entity) == 'application/json'
67+
68+
69+
def trim_kwargs_from_test_function(fn, kwargs):
70+
# the next function is the actual test function. the kwargs need to be trimmed so
71+
# that parameters which are not required will not be passed to it.
72+
if not is_preparer_func(fn):
73+
args, _, kw, _ = inspect.getargspec(fn) # pylint: disable=deprecated-method
74+
if kw is None:
75+
args = set(args)
76+
for key in [k for k in kwargs if k not in args]:
77+
del kwargs[key]
78+
79+
80+
def is_preparer_func(fn):
81+
return getattr(fn, '__is_preparer', False)

0 commit comments

Comments
 (0)