Search code examples
pythonamazon-web-servicestestingboto3moto

Aws Moto redshift statements assertions (Python)


Considering an AWS lambda written in Python that uses boto3 as client to AWS Redshift service.

Considering the following example:


import boto3
import moto


def lambda_handler(event, context):
    session = boto3.session.Session()

    redshift_data_service = session.client(
        service_name='redshift-data',
        region_name='a_region',
    )

    redshift_data_service.execute_statement(
        sql="insert into XYZ VALUES..."
    )


@moto.redshiftdata
def test_insert_sql_insert():
    lambda_handler(...)
    redshift_data_service = boto3.client("redshift-data", region_name='a_region')
    
    # List of executed statements (id... sql...)
    # Assert Statement Descriptions

Using Moto python dependency to mock redshift-data (similar to the example above), how can I retrieve data regarding executed statements? I am referring to the previously executed statement IDs and the sql string ("INSERT...") called on on lambda_handler

I couldn't find anything to get statement IDs in order to query the service and have a statement description.

Is there a way to assert the text of the previously executed SQLs?


Solution

  • There is no official way to retrieve/verify which statements have been provided in theexecute_statement-method.

    You can, however, use the internal API to verify this data:

    from moto.core import DEFAULT_ACCOUNT_ID
    from moto.redshiftdata import redshiftdata_backends
    
    backend = redshiftdata_backends[DEFAULT_ACCOUNT_ID]["us-east-1"]  # Use the appropriate account/region
    for statement in backend.statements.values():
        print(statement.query_string)
    

    Note that, as it is an internal API, the data structure may change inbetween versions.