コード例 #1
0
import pytest
from sqlalchemy import text

from pytest_mock_resources import create_redshift_fixture
from pytest_mock_resources.compat import boto3, moto
from tests.fixture.database import (
    COPY_TEMPLATE,
    fetch_values_from_table_and_assert,
    get_data_csv,
    original_data,
    randomcase,
    ResultProxy,
    setup_table_and_bucket,
)

redshift = create_redshift_fixture()


def test_s3_copy_into_redshift(redshift):
    with moto.mock_s3():
        setup_table_and_bucket(redshift)

        redshift.execute(
            COPY_TEMPLATE.format(
                COMMAND="COPY",
                LOCATION="s3://mybucket/file.csv",
                COLUMNS="",
                FROM="from",
                CREDENTIALS="credentials",
                OPTIONAL_ARGS="",
            ))
コード例 #2
0
import pytest
from sqlalchemy import create_engine, text

from pytest_mock_resources import (
    compat,
    create_mysql_fixture,
    create_postgres_fixture,
    create_redshift_fixture,
    create_sqlite_fixture,
)
from pytest_mock_resources.fixture.database.relational.generic import EngineManager

sqlite = create_sqlite_fixture()
postgres = create_postgres_fixture()
redshift = create_redshift_fixture()
mysql = create_mysql_fixture()


def test_basic_sqlite_fixture(sqlite):
    sqlite.execute("select 1")


def test_basic_postgres_fixture(postgres):
    postgres.execute("select 1")


def test_basic_redshift_fixture(redshift):
    redshift.execute("select 1")


def test_basic_postgres_and_redshift_fixture(postgres, redshift):
コード例 #3
0
def test_statements_mysql(mysql):
    execute = mysql.execute("""
        SELECT table_name
        FROM INFORMATION_SCHEMA.views
        WHERE table_name in ('cool_view', 'cool_view_2')
        AND table_schema = (select database())
        ORDER BY table_name
        """)

    result = [row[0] for row in execute]
    assert ["cool_view", "cool_view_2"] == result


statements = Statements("""
    CREATE TABLE account(
     user_id serial PRIMARY KEY,
     username VARCHAR (50) UNIQUE NOT NULL,
     password VARCHAR (50) NOT NULL
    );
    INSERT INTO account VALUES (1, 'user1', 'password1')
    """)
redshift = create_redshift_fixture(statements)


def test_multi_statement_statements(redshift):
    execute = redshift.execute("SELECT password FROM account")

    result = sorted([row[0] for row in execute])
    assert ["password1"] == result
コード例 #4
0
from sqlalchemy import Column, Integer, text
from sqlalchemy.ext.declarative import declarative_base

from pytest_mock_resources import create_redshift_fixture
from pytest_mock_resources.compat import boto3, moto
from tests.fixture.database import (
    COPY_TEMPLATE,
    data_columns,
    fetch_values_from_table_and_assert,
    get_data_csv,
    original_data,
    randomcase,
    setup_table_and_bucket,
)

redshift = create_redshift_fixture()


def test_s3_copy_into_redshift(redshift):
    with moto.mock_s3():
        setup_table_and_bucket(redshift)

        redshift.execute(
            COPY_TEMPLATE.format(
                COMMAND="COPY",
                LOCATION="s3://mybucket/file.csv",
                COLUMNS="",
                FROM="from",
                CREDENTIALS="credentials",
                OPTIONAL_ARGS="",
            ))