def test_execute(self, mock_run, mock_session):
        access_key = "aws_access_key_id"
        secret_key = "aws_secret_access_key"
        mock_session.return_value = Session(access_key, secret_key)

        schema = "schema"
        table = "table"
        s3_bucket = "bucket"
        s3_key = "key"
        copy_options = ""

        op = S3ToRedshiftOperator(
            schema=schema,
            table=table,
            s3_bucket=s3_bucket,
            s3_key=s3_key,
            copy_options=copy_options,
            redshift_conn_id="redshift_conn_id",
            aws_conn_id="aws_conn_id",
            task_id="task_id",
            dag=None,
        )
        op.execute(None)

        copy_query = f"""
            COPY {schema}.{table}
            FROM 's3://{s3_bucket}/{s3_key}'
            with credentials
            'aws_access_key_id={access_key};aws_secret_access_key={secret_key}'
            {copy_options};
        """

        assert mock_run.call_count == 1
        assert_equal_ignore_multiple_spaces(self, mock_run.call_args[0][0], copy_query)
Example #2
0
    def test_execute(self, mock_run, mock_session):
        access_key = "aws_access_key_id"
        secret_key = "aws_secret_access_key"
        mock_session.return_value = Session(access_key, secret_key)
        mock_session.return_value.access_key = access_key
        mock_session.return_value.secret_key = secret_key
        mock_session.return_value.token = None

        schema = "schema"
        table = "table"
        s3_bucket = "bucket"
        s3_key = "key"
        copy_options = ""

        op = S3ToRedshiftOperator(
            schema=schema,
            table=table,
            s3_bucket=s3_bucket,
            s3_key=s3_key,
            copy_options=copy_options,
            redshift_conn_id="redshift_conn_id",
            aws_conn_id="aws_conn_id",
            task_id="task_id",
            dag=None,
        )
        op.execute(None)

        credentials_block = build_credentials_block(mock_session.return_value)
        copy_query = op._build_copy_query(credentials_block, copy_options)

        assert mock_run.call_count == 1
        assert access_key in copy_query
        assert secret_key in copy_query
        assert_equal_ignore_multiple_spaces(self, mock_run.call_args[0][0],
                                            copy_query)
Example #3
0
    def test_truncate(self, mock_run, mock_session):
        access_key = "aws_access_key_id"
        secret_key = "aws_secret_access_key"
        mock_session.return_value = Session(access_key, secret_key)

        schema = "schema"
        table = "table"
        s3_bucket = "bucket"
        s3_key = "key"
        copy_options = ""

        op = S3ToRedshiftOperator(
            schema=schema,
            table=table,
            s3_bucket=s3_bucket,
            s3_key=s3_key,
            copy_options=copy_options,
            truncate_table=True,
            redshift_conn_id="redshift_conn_id",
            aws_conn_id="aws_conn_id",
            task_id="task_id",
            dag=None,
        )
        op.execute(None)

        copy_statement = f"""
                    COPY {schema}.{table}
                    FROM 's3://{s3_bucket}/{s3_key}'
                    with credentials
                    'aws_access_key_id={access_key};aws_secret_access_key={secret_key}'
                    {copy_options};
                """

        truncate_statement = f'TRUNCATE TABLE {schema}.{table};'
        transaction = f"""
                    BEGIN;
                    {truncate_statement}
                    {copy_statement}
                    COMMIT
                    """
        assert_equal_ignore_multiple_spaces(self, mock_run.call_args[0][0],
                                            transaction)

        assert mock_run.call_count == 1
Example #4
0
    def test_truncate(self, mock_run, mock_session):
        access_key = "aws_access_key_id"
        secret_key = "aws_secret_access_key"
        mock_session.return_value = Session(access_key, secret_key)
        mock_session.return_value.access_key = access_key
        mock_session.return_value.secret_key = secret_key
        mock_session.return_value.token = None

        schema = "schema"
        table = "table"
        s3_bucket = "bucket"
        s3_key = "key"
        copy_options = ""

        op = S3ToRedshiftOperator(
            schema=schema,
            table=table,
            s3_bucket=s3_bucket,
            s3_key=s3_key,
            copy_options=copy_options,
            truncate_table=True,
            redshift_conn_id="redshift_conn_id",
            aws_conn_id="aws_conn_id",
            task_id="task_id",
            dag=None,
        )
        op.execute(None)

        credentials_block = build_credentials_block(mock_session.return_value)
        copy_statement = op._build_copy_query(credentials_block, copy_options)

        truncate_statement = f'TRUNCATE TABLE {schema}.{table};'
        transaction = f"""
                    BEGIN;
                    {truncate_statement}
                    {copy_statement}
                    COMMIT
                    """
        assert_equal_ignore_multiple_spaces(self, mock_run.call_args[0][0],
                                            transaction)

        assert mock_run.call_count == 1
Example #5
0
      schedule_interval=None,
      tags=['example']) as dag:
 setup__task_add_sample_data_to_s3 = PythonOperator(
     python_callable=_add_sample_data_to_s3,
     task_id='setup__add_sample_data_to_s3')
 setup__task_create_table = PostgresOperator(
     sql=
     f'CREATE TABLE IF NOT EXISTS {REDSHIFT_TABLE}(Id int, Name varchar)',
     postgres_conn_id='redshift_default',
     task_id='setup__create_table',
 )
 # [START howto_operator_s3_to_redshift_task_1]
 task_transfer_s3_to_redshift = S3ToRedshiftOperator(
     s3_bucket=S3_BUCKET,
     s3_key=S3_KEY,
     schema="PUBLIC",
     table=REDSHIFT_TABLE,
     copy_options=['csv'],
     task_id='transfer_s3_to_redshift',
 )
 # [END howto_operator_s3_to_redshift_task_1]
 teardown__task_drop_table = PostgresOperator(
     sql=f'DROP TABLE IF EXISTS {REDSHIFT_TABLE}',
     postgres_conn_id='redshift_default',
     task_id='teardown__drop_table',
 )
 teardown__task_remove_sample_data_from_s3 = PythonOperator(
     python_callable=_remove_sample_data_from_s3,
     task_id='teardown__remove_sample_data_from_s3')
 ([setup__task_add_sample_data_to_s3, setup__task_create_table] >>
  task_transfer_s3_to_redshift >> [
      teardown__task_drop_table,