forked from smithy-lang/smithy-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sources.py
More file actions
86 lines (68 loc) · 3.15 KB
/
test_sources.py
File metadata and controls
86 lines (68 loc) · 3.15 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import os
from unittest.mock import patch
from smithy_aws_core.config.sources import EnvironmentSource
class TestEnvironmentSource:
def test_source_name(self):
source = EnvironmentSource()
assert source.name == "environment"
def test_get_region_from_aws_region(self):
with patch.dict(os.environ, {"AWS_REGION": "us-west-2"}, clear=True):
source = EnvironmentSource()
value = source.get("region")
assert value == "us-west-2"
def test_get_returns_none_when_env_var_not_set(self):
with patch.dict(os.environ, {}, clear=True):
source = EnvironmentSource()
value = source.get("region")
assert value is None
def test_get_returns_none_for_unknown_key(self):
source = EnvironmentSource()
value = source.get("unknown_config_key")
assert value is None
def test_get_handles_empty_string_env_var(self):
with patch.dict(os.environ, {"AWS_REGION": ""}, clear=True):
source = EnvironmentSource()
value = source.get("region")
assert value == ""
def test_get_handles_whitespace_env_var(self):
with patch.dict(os.environ, {"AWS_REGION": " us-west-2 "}, clear=True):
source = EnvironmentSource()
value = source.get("region")
# Whitespaces should be stripped
assert value == "us-west-2"
def test_get_handles_whole_whitespace_env_var(self):
with patch.dict(os.environ, {"AWS_REGION": " "}, clear=True):
source = EnvironmentSource()
value = source.get("region")
# Whitespaces should be stripped
assert value == ""
def test_multiple_keys_with_different_env_vars(self):
env_vars = {"AWS_REGION": "eu-west-1", "AWS_RETRY_MODE": "standard"}
with patch.dict(os.environ, env_vars, clear=True):
source = EnvironmentSource()
region = source.get("region")
retry_mode = source.get("retry_mode")
assert region == "eu-west-1"
assert retry_mode == "standard"
def test_get_is_idempotent(self):
with patch.dict(os.environ, {"AWS_REGION": "ap-south-1"}, clear=True):
source = EnvironmentSource()
# Calling get on source multiple times should return the same value
value1 = source.get("region")
value2 = source.get("region")
value3 = source.get("region")
assert value1 == value2 == value3 == "ap-south-1"
def test_source_does_not_cache_env_vars(self):
source = EnvironmentSource()
# First read
with patch.dict(os.environ, {"AWS_REGION": "us-east-1"}, clear=True):
value1 = source.get("region")
assert value1 == "us-east-1"
# Environment changes
with patch.dict(os.environ, {"AWS_REGION": "us-west-2"}, clear=False):
value2 = source.get("region")
assert value2 == "us-west-2"
# Source reads from os.environ and not from cache
assert value1 != value2