Skip to content

Commit

Permalink
Ex. of test executions w/ diff. configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp committed Jun 29, 2022
1 parent b907890 commit be692f4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
19 changes: 19 additions & 0 deletions sdk/tables/azure-data-tables/tests/preparers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ def wrapper(*args, **kwargs):
return wrapper


def tables_decorator_with_wraps(func, **kwargs):
@TablesPreparer()
@functools.wraps(func)
def wrapper(*args, **kwargs):
key = kwargs.pop("tables_primary_storage_account_key")
name = kwargs.pop("tables_storage_account_name")
key = AzureNamedKeyCredential(key=key, name=name)

kwargs["tables_primary_storage_account_key"] = key
kwargs["tables_storage_account_name"] = name

trimmed_kwargs = {k: v for k, v in kwargs.items()}
trim_kwargs_from_test_function(func, trimmed_kwargs)

func(*args, **trimmed_kwargs)

return wrapper


def cosmos_decorator(func, **kwargs):
@CosmosPreparer()
def wrapper(*args, **kwargs):
Expand Down
59 changes: 38 additions & 21 deletions sdk/tables/azure-data-tables/tests/test_table_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import os
import sys

from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy, set_custom_default_matcher
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy, recorded_test, set_custom_default_matcher

from azure.core import MatchConditions
from azure.core.pipeline.policies import HTTPPolicy
Expand All @@ -37,22 +37,26 @@
)

from _shared.testcase import TableTestCase
from preparers import tables_decorator
from preparers import tables_decorator, tables_decorator_with_wraps, TablesPreparer

#------------------------------------------------------------------------------
TEST_TABLE_PREFIX = 'table'
#------------------------------------------------------------------------------

class TestTableBatch(AzureRecordedTestCase, TableTestCase):
# This test executes correctly because:
# 1. Its decorator wraps the test function with functools.wraps
# 2. Other positional parameters have default values
# 3. Pytest markers like the one used don't interfere with fixture use
@pytest.mark.skipif(sys.version_info < (3, 0), reason="requires Python3")
@tables_decorator
@recorded_by_proxy
def test_batch_single_insert(self, tables_storage_account_name, tables_primary_storage_account_key):
@tables_decorator_with_wraps
def test_batch_single_insert(
self, recorded_test, tables_storage_account_name=None, tables_primary_storage_account_key=None
):
# this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
set_custom_default_matcher(
compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
)

# Arrange
self._set_up(tables_storage_account_name, tables_primary_storage_account_key)
try:
Expand Down Expand Up @@ -81,10 +85,12 @@ def test_batch_single_insert(self, tables_storage_account_name, tables_primary_s
finally:
self._tear_down()

@pytest.mark.skipif(sys.version_info < (3, 0), reason="requires Python3")
# This test DOESN'T execute correctly because:
# 1. Its decorator doesn't wrap the test function with functools.wraps
@tables_decorator
@recorded_by_proxy
def test_batch_single_update(self, tables_storage_account_name, tables_primary_storage_account_key):
def test_batch_single_update(
self, recorded_test, tables_storage_account_name=None, tables_primary_storage_account_key=None
):
# this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
set_custom_default_matcher(
compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
Expand Down Expand Up @@ -122,10 +128,14 @@ def test_batch_single_update(self, tables_storage_account_name, tables_primary_s
finally:
self._tear_down()

@pytest.mark.skipif(sys.version_info < (3, 0), reason="requires Python3")
@tables_decorator
@recorded_by_proxy
def test_batch_update(self, tables_storage_account_name, tables_primary_storage_account_key):
# This test executes correctly because:
# 1. Its decorator wraps the test function with functools.wraps
# 2. Test variables are passed in via kwargs
@tables_decorator_with_wraps
def test_batch_update(self, recorded_test, **kwargs):
tables_storage_account_name = kwargs.pop("tables_storage_account_name")
tables_primary_storage_account_key = kwargs.pop("tables_primary_storage_account_key")

# this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
set_custom_default_matcher(
compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
Expand Down Expand Up @@ -165,10 +175,14 @@ def test_batch_update(self, tables_storage_account_name, tables_primary_storage_
finally:
self._tear_down()

@pytest.mark.skipif(sys.version_info < (3, 0), reason="requires Python3")
@tables_decorator
@recorded_by_proxy
def test_batch_merge(self, tables_storage_account_name, tables_primary_storage_account_key):
# This test executes correctly because:
# 1. Its decorator -- a direct EnvironmentVariableLoader impl. -- wraps the test function with functools.wraps
# 2. Other positional parameters have default values
# (The test fails because setup from tables_decorator has been skipped)
@TablesPreparer()
def test_batch_merge(
self, recorded_test, tables_storage_account_name=None, tables_primary_storage_account_key=None
):
# this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
set_custom_default_matcher(
compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
Expand Down Expand Up @@ -210,10 +224,13 @@ def test_batch_merge(self, tables_storage_account_name, tables_primary_storage_a
finally:
self._tear_down()

@pytest.mark.skipif(sys.version_info < (3, 0), reason="requires Python3")
@tables_decorator
@recorded_by_proxy
def test_batch_update_if_match(self, tables_storage_account_name, tables_primary_storage_account_key):
# This test executes correctly because:
# 1. It has no decorator
# (The test fails because setup from tables_decorator has been skipped)
def test_batch_update_if_match(self, recorded_test):
tables_storage_account_name = None
tables_primary_storage_account_key = None

# this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
set_custom_default_matcher(
compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
Expand Down

0 comments on commit be692f4

Please sign in to comment.