简体   繁体   中英

How to mock aws timestream for unit test on python?

I found on the net an example to test the aws timestream service; when I try to launch the test I get a 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?

If you look at the AWS DataWrangler example for Timestream:

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 (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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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