forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_shared_functions.py
More file actions
140 lines (97 loc) · 4.64 KB
/
test_shared_functions.py
File metadata and controls
140 lines (97 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import warnings
from dataclasses import dataclass
import pytest
from pydantic import BaseModel
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.functions import (
extract_event_from_common_models,
powertools_debug_is_set,
powertools_dev_is_set,
resolve_env_var_choice,
resolve_max_age,
resolve_truthy_env_var_choice,
strtobool,
)
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
from aws_lambda_powertools.utilities.parameters.base import DEFAULT_MAX_AGE_SECS
def test_resolve_env_var_choice_explicit_wins_over_env_var():
assert resolve_truthy_env_var_choice(env="true", choice=False) is False
assert resolve_env_var_choice(env="something", choice=False) is False
def test_resolve_env_var_choice_env_wins_over_absent_explicit():
assert resolve_truthy_env_var_choice(env="true") == 1
assert resolve_env_var_choice(env="something") == "something"
@pytest.mark.parametrize("true_value", ["y", "yes", "t", "true", "on", "1"])
def test_strtobool_true(true_value):
assert strtobool(true_value)
@pytest.mark.parametrize("false_value", ["n", "no", "f", "false", "off", "0"])
def test_strtobool_false(false_value):
assert strtobool(false_value) is False
def test_strtobool_value_error():
with pytest.raises(ValueError) as exp:
strtobool("fail")
assert str(exp.value) == "invalid truth value 'fail'"
def test_powertools_dev_warning(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_DEBUG is set
monkeypatch.setenv(constants.POWERTOOLS_DEV_ENV, "1")
warning_message = "POWERTOOLS_DEV environment variable is enabled. Increasing verbosity across utilities."
# WHEN set_package_logger is used at initialization
# THEN a warning should be emitted
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("default")
powertools_dev_is_set()
assert len(w) == 1
assert str(w[0].message) == warning_message
def test_powertools_debug_warning(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_DEBUG is set
monkeypatch.setenv(constants.POWERTOOLS_DEBUG_ENV, "1")
warning_message = "POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG."
# WHEN set_package_logger is used at initialization
# THEN a warning should be emitted
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("default")
powertools_debug_is_set()
assert len(w) == 1
assert str(w[0].message) == warning_message
def test_extract_event_dict():
data = {"hello": "world"}
assert extract_event_from_common_models(data) == data
def test_extract_event_pydantic():
class DummyModel(BaseModel):
hello: str
data = {"hello": "world"}
assert extract_event_from_common_models(DummyModel(**data)) == data
def test_extract_event_dict_wrapper():
class DummyClassSample(DictWrapper):
pass
data = {"hello": "world"}
assert extract_event_from_common_models(DummyClassSample(data)) == data
def test_extract_event_dataclass():
@dataclass
class DummyDataclass:
hello: str
data = {"hello": "world"}
assert extract_event_from_common_models(DummyDataclass(**data)) == data
@pytest.mark.parametrize("data", [False, True, "", 10, [], {}, object])
def test_extract_event_any(data):
assert extract_event_from_common_models(data) == data
def test_resolve_max_age_explicit_wins_over_env_var(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE environment variable is set
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE_ENV, "20")
# WHEN the choice is set explicitly
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=10)
# THEN the result must be the choice
assert max_age == 10
def test_resolve_max_age_with_default_value():
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE is not set
# WHEN the choice is set to None
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=None)
# THEN the result must be the default value (DEFAULT_MAX_AGE_SECS)
assert max_age == int(DEFAULT_MAX_AGE_SECS)
def test_resolve_max_age_env_var_wins_over_default_value(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE environment variable is set
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE_ENV, "20")
# WHEN the choice is set to None
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=None)
# THEN the result must be the environment variable value
assert max_age == 20