简体   繁体   English

如何在 python 上模拟 aws 时间流以进行单元测试?

[英]How to mock aws timestream for unit test on python?

I found on the net an example to test the aws timestream service;我在网上找到了一个测试 aws timestream 服务的示例; when I try to launch the test I get a NoRegionError当我尝试启动测试时,我得到一个 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)

ERROR enter image description here错误在此处输入图像描述

how can the problem be solved?如何解决问题? Since even the "moto" library does not offer the mock of the aws service?因为即使是“moto”库也不提供 aws 服务的模拟?

If you look at the AWS DataWrangler example for Timestream:如果您查看时间流的 AWS DataWrangler 示例:

https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.timestream.write.html https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.timestream.write.html

When you call the constructor, you can pass the boto3_session or it will use the default session:当您调用构造函数时,您可以传递 boto3_session 或者它将使用默认的 session:

boto3_session (boto3.Session(), optional) – Boto3 Session. The default boto3 Session will be used if boto3_session receive None.

You boto3 configs need to specify a default region or you need to declare in the boto3 session.您的 boto3 配置需要指定默认区域,或者您需要在 boto3 session 中声明。

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM