# Path: tests/pgshovel/streams/fixtures.py
# DEFAULT_PUBLISHER = uuid.uuid1().bytes
# def reserialize(message):
# def copy(message, **replacements):
# def make_message(payload, sequence=1, publisher=DEFAULT_PUBLISHER):
# def make_messages(payloads, publisher=DEFAULT_PUBLISHER):
# def make_batch_messages(batch_identifier, payloads, **kwargs):
# def transactions():
# def message():


import pytest
import psycopg2
import uuid
from copy import deepcopy
from itertools import islice
from kafka import (
    KafkaClient,
    SimpleProducer,
)
from kazoo.client import KazooClient
from kafka import KafkaClient
from tests.pgshovel.fixtures import (
    cluster,
    create_temporary_database
)
from tests.pgshovel.streams.fixtures import (
    batch_identifier,
    make_batch_messages,
    transaction,
)
from pgshovel.administration import (
    create_set,
    get_managed_databases,
    initialize_cluster,
)
from pgshovel.cluster import Cluster
from pgshovel.interfaces.common_pb2 import (
    Column,
    Row,
    Snapshot,
    Tick,
    Timestamp,
)
from pgshovel.interfaces.configurations_pb2 import ReplicationSetConfiguration
from pgshovel.interfaces.streams_pb2 import (
    BeginOperation,
    CommitOperation,
    MutationOperation,
    RollbackOperation,
)
from pgshovel.relay.streams.kafka import KafkaWriter
from pgshovel.replication.loaders.simple import SimpleLoader
from pgshovel.replication.streams.kafka import KafkaStream
from pgshovel.replication.targets.postgresql import PostgreSQLTarget
from pgshovel.utilities.conversions import to_snapshot

    with target_connection as conn, conn.cursor() as cursor:
        cursor.execute('SELECT * FROM auth_user')
        assert sorted(cursor.fetchmany(3)) == [(1L, 'kevin bacon'), (2L, 'example2')]


def test_any_transactions_visible_to_snapshot_are_ignored(
    target,
    loader,
    stream,
    source_connection,
    target_connection,
    writer,
    visible_transaction
):
    with source_connection as conn, conn.cursor() as cursor:
        cursor.executemany(
            'INSERT INTO auth_user (username) VALUES (%s)',
            (('example',), ('example2',))
        )

    # Push a transaction through the relay that is invisible to the snapshot of
    # the source database.
    writer.push(visible_transaction)

    # Run the target, with a finite stream of just the one invisible
    # transaction (of 3 messages), so at the end of this method call the
    # transaction should have been applied.
    target.run(loader, stream.limited_to(3))


# Path: tests/pgshovel/streams/fixtures.py
# DEFAULT_PUBLISHER = uuid.uuid1().bytes
# def reserialize(message):
# def copy(message, **replacements):
# def make_message(payload, sequence=1, publisher=DEFAULT_PUBLISHER):
# def make_messages(payloads, publisher=DEFAULT_PUBLISHER):
# def make_batch_messages(batch_identifier, payloads, **kwargs):
# def transactions():
# def message():


# Path: tests/pgshovel/replication/targets/postgres.py
import pytest
import psycopg2
import uuid
from copy import deepcopy
from itertools import islice
from kafka import (
    KafkaClient,
    SimpleProducer,
)
from kazoo.client import KazooClient
from kafka import KafkaClient
from tests.pgshovel.fixtures import (
    cluster,
    create_temporary_database
)
from tests.pgshovel.streams.fixtures import (
    batch_identifier,
    make_batch_messages,
    transaction,
)
from pgshovel.administration import (
    create_set,
    get_managed_databases,
    initialize_cluster,
)
from pgshovel.cluster import Cluster
from pgshovel.interfaces.common_pb2 import (
    Column,
    Row,
    Snapshot,
    Tick,
    Timestamp,
)
from pgshovel.interfaces.configurations_pb2 import ReplicationSetConfiguration
from pgshovel.interfaces.streams_pb2 import (
    BeginOperation,
    CommitOperation,
    MutationOperation,
    RollbackOperation,
)
from pgshovel.relay.streams.kafka import KafkaWriter
from pgshovel.replication.loaders.simple import SimpleLoader
from pgshovel.replication.streams.kafka import KafkaStream
from pgshovel.replication.targets.postgresql import PostgreSQLTarget
from pgshovel.utilities.conversions import to_snapshot


    with target_connection as conn, conn.cursor() as cursor:
        cursor.execute('SELECT * FROM auth_user')
        assert sorted(cursor.fetchmany(3)) == [(1L, 'kevin bacon'), (2L, 'example2')]


def test_any_transactions_visible_to_snapshot_are_ignored(
    target,
    loader,
    stream,
    source_connection,
    target_connection,
    writer,
    visible_transaction
):
    with source_connection as conn, conn.cursor() as cursor:
        cursor.executemany(
            'INSERT INTO auth_user (username) VALUES (%s)',
            (('example',), ('example2',))
        )

    # Push a transaction through the relay that is invisible to the snapshot of
    # the source database.
    writer.push(visible_transaction)

    # Run the target, with a finite stream of just the one invisible
    # transaction (of 3 messages), so at the end of this method call the
    # transaction should have been applied.
    target.run(loader, stream.limited_to(3))


    with target_connection as conn, conn.cursor() as cursor: