python - 如何从 DynamoDB 中获取项目并存储元素以将其用于 Pytest?
问题描述
如何从 DynamoDB 中获取项目并在 Pytest 中使用环境(元素)?我的函数使用 boto3 来获取环境,并将环境存储在一个名为env[]
.
def get_environment():
"""
Retrieves environment from DynamoDB
:param ssm_client:
:return:
"""
env = []
try:
response = dynamodb_client.scan(
TableName='accounts',
AttributesToGet=['account_id','environment'],
)
env = [ s['environment'] for s in response['Items'] ]
return env
except Exception as e:
print("[ERROR] Failed")
回复:
[{'environment': {'S': 'sandbox'}, 'account_id': {'S': '409XX'}}, {'environment': {'S': 'services'}, 'account_id': {'S': '3228XX'}}, {'environment': {'S': 'non-production'}, 'account_id': {'S': '1145XX'}}, {'environment': {'S': 'production'}, 'account_id': {'S': '37988XX'}}, {'environment': {'S': 'non-production'}, 'account_id': {'S': '38856XXX'}}, {'environment': {'S': 'non-production'}, 'account_id': {'S': '10819XXXX'}}]
当我尝试打印环境名称时,它会引发错误
def print_environment():
response = get_environment()
environment = [ item['environment'] for item in response ]
print(environment)
File "app.py", line 250, in print_environment
environment = [ item['environment'] for item in response ]
File "app.py", line 250, in <listcomp>
environment = [ item['environment'] for item in response ]
KeyError: 'environment'
我只想为特定的 accountID 获取 Environment 并将其存储environment
并在 pytest 中使用它。例子
def test_environment():
environment = get_environment()
account_id = get_account_ids()
for accounts in account_id:
if (environment = 'production') then assert something .....
解决方案
您的字典列表不包含“环境”作为键。
它包含“环境”指向的值。
这种值的示例是{'S': 'sandbox'}, 'account_id': {'S': '409XX'}}
推荐阅读
- reactjs - react js create-react-app中的“未定义useHistory”
- python-3.x - Ray 没有将远程功能分发到本地机器上的所有节点
- javascript - Lodash 从嵌套数组中删除重复项
- javascript - 错误:必须使用“allowedNonPeerDependencies”选项明确允许依赖关系@types/html2canvas
- microsoft-dynamics - 在 Cloud Elements 中创建 MS 动态实例时无法交换代码以获得访问令牌错误
- html - 浮动效果 - 我的代码存在问题
- rust - 如何在 WASI 中使用 Rust 的 async/await 语法
- python - Python Selenuim,在没有 XPATH 的情况下找到具有特定属性的按钮
- javascript - 如何使用颤振/飞镖在解析(Back4App)中获取关系数据?
- bash - 如何使用 for 循环更新第 i 个索引处的数组元素?