From 17d9646c8aeb35f56b3d0ea67e75896693a351c8 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Sun, 21 Feb 2021 00:31:34 -0800 Subject: [PATCH 1/6] tests: Add some missing code coverage * test(DictWrapper): Add check for not isinstance * test(tracing): Add mock to test aiohttp_trace_config * test(appconfig): Add test for get_app_config() without a default provider --- .../functional/test_lambda_trigger_events.py | 3 ++ tests/functional/test_tracing.py | 14 ++++++++++ tests/functional/test_utilities_parameters.py | 28 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/tests/functional/test_lambda_trigger_events.py b/tests/functional/test_lambda_trigger_events.py index d6d225bf530..20f4eb5a827 100644 --- a/tests/functional/test_lambda_trigger_events.py +++ b/tests/functional/test_lambda_trigger_events.py @@ -54,6 +54,9 @@ def message(self) -> str: assert DataClassSample(data1) == DataClassSample(data1) assert DataClassSample(data1) != DataClassSample(data2) + # Comparing against a dict should not be equals + assert DataClassSample(data1) != data1 + assert data1 != DataClassSample(data1) assert DataClassSample(data1) is not data1 assert data1 is not DataClassSample(data1) diff --git a/tests/functional/test_tracing.py b/tests/functional/test_tracing.py index 617bf816f86..36fc6d4be64 100644 --- a/tests/functional/test_tracing.py +++ b/tests/functional/test_tracing.py @@ -1,4 +1,6 @@ import contextlib +import sys +from unittest.mock import Mock import pytest @@ -212,3 +214,15 @@ def handler(event, context): result = handler({}, {}) assert "testresult" in result assert "testresult2" in result + + +def test_aiohttp_trace_config(): + aiohttp_mock = Mock() + sys.modules["aiohttp"] = aiohttp_mock + from aws_lambda_powertools.tracing import aiohttp_trace_config + + aiohttp_trace_config() + + from aws_xray_sdk.ext.aiohttp.client import aws_xray_trace_config + + assert aws_xray_trace_config.__doc__ == "aiohttp extension for X-Ray (aws_xray_trace_config)" diff --git a/tests/functional/test_utilities_parameters.py b/tests/functional/test_utilities_parameters.py index 045b7fbbe18..163f7454e66 100644 --- a/tests/functional/test_utilities_parameters.py +++ b/tests/functional/test_utilities_parameters.py @@ -1534,6 +1534,34 @@ def _get_multiple(self, path: str, **kwargs) -> Dict[str, str]: assert str_value == json.dumps(mock_body_json) +def test_appconf_get_app_config_new(monkeypatch, mock_name, mock_value): + """ + Test get_app_config() without a default provider + """ + + class TestProvider(BaseProvider): + def __init__(self, environment: str, application: str): + super().__init__() + + def get(self, name: str, **kwargs) -> str: + return mock_value + + def _get(self, name: str, **kwargs) -> str: + raise NotImplementedError() + + def _get_multiple(self, path: str, **kwargs) -> Dict[str, str]: + raise NotImplementedError() + + monkeypatch.setattr(parameters.appconfig, "DEFAULT_PROVIDERS", {}) + monkeypatch.setattr(parameters.appconfig, "AppConfigProvider", TestProvider) + + _environment = "dev" + _application = "myapp" + value = parameters.get_app_config(mock_name, environment=_environment, application=_application) + + assert value == mock_value + + def test_transform_value_json(mock_value): """ Test transform_value() with a json transform From 971ab07c03a27da4ce7b6886703bc5446a5b2e7e Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Sat, 27 Feb 2021 07:44:23 -0800 Subject: [PATCH 2/6] chore: Bump CI --- tests/functional/test_utilities_parameters.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/functional/test_utilities_parameters.py b/tests/functional/test_utilities_parameters.py index 163f7454e66..9d88f2a9d77 100644 --- a/tests/functional/test_utilities_parameters.py +++ b/tests/functional/test_utilities_parameters.py @@ -1539,6 +1539,7 @@ def test_appconf_get_app_config_new(monkeypatch, mock_name, mock_value): Test get_app_config() without a default provider """ + # GIVEN class TestProvider(BaseProvider): def __init__(self, environment: str, application: str): super().__init__() @@ -1555,10 +1556,11 @@ def _get_multiple(self, path: str, **kwargs) -> Dict[str, str]: monkeypatch.setattr(parameters.appconfig, "DEFAULT_PROVIDERS", {}) monkeypatch.setattr(parameters.appconfig, "AppConfigProvider", TestProvider) - _environment = "dev" - _application = "myapp" - value = parameters.get_app_config(mock_name, environment=_environment, application=_application) + # WHEN + value = parameters.get_app_config(mock_name, environment="dev", application="myapp") + # THEN + assert parameters.appconfig.DEFAULT_PROVIDERS["appconfig"] is not None assert value == mock_value From 631ddc03942774e4e4a64955b26c26bfb7753c3f Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Wed, 3 Mar 2021 05:55:27 -0800 Subject: [PATCH 3/6] doc: correct docs Co-authored-by: Heitor Lessa --- tests/functional/test_utilities_parameters.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/functional/test_utilities_parameters.py b/tests/functional/test_utilities_parameters.py index 9d88f2a9d77..1733f965a24 100644 --- a/tests/functional/test_utilities_parameters.py +++ b/tests/functional/test_utilities_parameters.py @@ -1535,9 +1535,6 @@ def _get_multiple(self, path: str, **kwargs) -> Dict[str, str]: def test_appconf_get_app_config_new(monkeypatch, mock_name, mock_value): - """ - Test get_app_config() without a default provider - """ # GIVEN class TestProvider(BaseProvider): From c24add9cefb61e5e2e0e609a172a7e488f1fa00c Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Wed, 3 Mar 2021 05:59:19 -0800 Subject: [PATCH 4/6] chore: formatting --- tests/functional/test_utilities_parameters.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/functional/test_utilities_parameters.py b/tests/functional/test_utilities_parameters.py index 1733f965a24..5a915f574ae 100644 --- a/tests/functional/test_utilities_parameters.py +++ b/tests/functional/test_utilities_parameters.py @@ -1535,7 +1535,6 @@ def _get_multiple(self, path: str, **kwargs) -> Dict[str, str]: def test_appconf_get_app_config_new(monkeypatch, mock_name, mock_value): - # GIVEN class TestProvider(BaseProvider): def __init__(self, environment: str, application: str): From 9b3728be557a04283db8810ebcfcd7455037155d Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Wed, 3 Mar 2021 06:17:49 -0800 Subject: [PATCH 5/6] tests: Assert that the TraceConfig has been configured --- tests/functional/test_tracing.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/functional/test_tracing.py b/tests/functional/test_tracing.py index 36fc6d4be64..0d629e20378 100644 --- a/tests/functional/test_tracing.py +++ b/tests/functional/test_tracing.py @@ -221,8 +221,6 @@ def test_aiohttp_trace_config(): sys.modules["aiohttp"] = aiohttp_mock from aws_lambda_powertools.tracing import aiohttp_trace_config - aiohttp_trace_config() + trace_config: Mock = aiohttp_trace_config() - from aws_xray_sdk.ext.aiohttp.client import aws_xray_trace_config - - assert aws_xray_trace_config.__doc__ == "aiohttp extension for X-Ray (aws_xray_trace_config)" + assert trace_config._extract_mock_name() == "mock.TraceConfig()" From faf75ca7ef17d32e86d6ebdef18b0ae9c47845a4 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Wed, 3 Mar 2021 06:26:23 -0800 Subject: [PATCH 6/6] tests: removed --- tests/functional/test_tracing.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/functional/test_tracing.py b/tests/functional/test_tracing.py index 0d629e20378..617bf816f86 100644 --- a/tests/functional/test_tracing.py +++ b/tests/functional/test_tracing.py @@ -1,6 +1,4 @@ import contextlib -import sys -from unittest.mock import Mock import pytest @@ -214,13 +212,3 @@ def handler(event, context): result = handler({}, {}) assert "testresult" in result assert "testresult2" in result - - -def test_aiohttp_trace_config(): - aiohttp_mock = Mock() - sys.modules["aiohttp"] = aiohttp_mock - from aws_lambda_powertools.tracing import aiohttp_trace_config - - trace_config: Mock = aiohttp_trace_config() - - assert trace_config._extract_mock_name() == "mock.TraceConfig()"