[英]How to mock global variable in a imported module in python unit test?
[英]How to mock aws timestream for unit test on python?
我在網上找到了一個測試 aws timestream 服務的示例; 當我嘗試啟動測試時,我得到一個 NoRegionError
#test_timestream.py
import logging
from datetime import datetime
import pandas as pd
import awswrangler as wr
import logging
from datetime import datetime
import pandas as pd
import awswrangler as wr
logging.getLogger("awswrangler").setLevel(logging.DEBUG)
def test_basic_scenario(timestream_database_and_table):
name = timestream_database_and_table
df = pd.DataFrame(
{
"time": [datetime.now(), datetime.now(), datetime.now()],
"dim0": ["foo", "boo", "bar"],
"dim1": [1, 2, 3],
"measure": [1.0, 1.1, 1.2],
}
)
rejected_records = wr.timestream.write(
df=df,
database=name,
table=name,
time_col="time",
measure_col="measure",
dimensions_cols=["dim0", "dim1"],
)
assert len(rejected_records) == 0
df = wr.timestream.query(
f"""
SELECT
1 as col_int,
try_cast(now() as time) as col_time,
TRUE as col_bool,
current_date as col_date,
'foo' as col_str,
measure_value::double,
measure_name,
time
FROM "{name}"."{name}"
ORDER BY time
DESC LIMIT 10
"""
)
assert df.shape == (3, 8)
如何解決問題? 因為即使是“moto”庫也不提供 aws 服務的模擬?
如果您查看時間流的 AWS DataWrangler 示例:
https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.timestream.write.html
當您調用構造函數時,您可以傳遞 boto3_session 或者它將使用默認的 session:
boto3_session (boto3.Session(), optional) – Boto3 Session. The default boto3 Session will be used if boto3_session receive None.
您的 boto3 配置需要指定默認區域,或者您需要在 boto3 session 中聲明。
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html
import boto3
from botocore.config import Config
my_config = Config(
region_name = 'us-west-2',
signature_version = 'v4',
retries = {
'max_attempts': 10,
'mode': 'standard'
}
)
client = boto3.client('kinesis', config=my_config)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.