# -*- coding: utf-8 -*-
import pytest
import py
from trek import Migrator, ALL

TEST_COMMENTS = [
    # representative comment characters from...
    '#',  # python
    '--', # sql
    '//', # c
    ';',  # clojure
    '"',  # viml
    # we probably don't need more than this *but* if in the future a comment
    # char or style screws things up add it to make sure we don't regress! This
    # is only a list of things I thought of immediately.
]
UP_CMD = 'MIGRATE UP'
DOWN_CMD = 'MIGRATE DOWN'

@pytest.fixture(params=TEST_COMMENTS)
def up_stmt(request):
    return '%s %s' % (request.param, UP_CMD)

@pytest.fixture(params=TEST_COMMENTS)
def down_stmt(request):
    return '%s %s' % (request.param, DOWN_CMD)


class DebugRunner(object):
    def __init__(self, args):
        self.args = args
        self.ups = []
        self.downs = []

    def version(self):
        return ''

    def up(self, name, migration):
        self.ups.append((name, migration))
        yield 'info', migration.up

    def down(self, name, migration):
        self.downs.append((name, migration))
        yield 'info', migration.down

DEBUG_RUNNER_PATH = '%s:%s' % (__name__, DebugRunner.__name__)


@pytest.yield_fixture
def migrations(request):
    """\
    take a specified amount of migrations and have them available in a
    temporary directory for the migrator. The migrations come from the
    `pytest.mark.migrations` marker, and are expect to be a number of
    (name, (up, down)) tuples, as args.

    Example:

        @pytest.mark.migrations(
            ('1', ('CREATE DATABASE test;', 'DROP DATABASE test;')),
        )
        def test_thing(migrations):
            # do something with migrations dir
            pass
    """
    marker = request.node.get_marker('migrations')
    migrations = [] if marker is None else marker.args

    # we don't need to test all the variations of migration comments here, only
    # in the parser. If this requirement changes in the future, the statements
    # here should just use the up_stmt and down_stmt fixtures.
    up_cmd = '%s %s' % (TEST_COMMENTS[0], UP_CMD)
    down_cmd = '%s %s' % (TEST_COMMENTS[0], DOWN_CMD)

    migrations_dir = py.path.local.mkdtemp()

    for name, (up, down) in migrations:
        migration = '\n'.join([up_cmd, up, down_cmd, down])
        migrations_dir.join(name).write(migration)

    yield migrations_dir

    migrations_dir.remove()


@pytest.fixture
def migrator(request, migrations):
    marker = request.node.get_marker('migrator')
    opts = {} if marker is None else marker.kwargs

    return Migrator(
        count=opts.get('count', ALL),
        runner_path=opts.get('runner_path', DEBUG_RUNNER_PATH),
        migrations_dir=str(migrations),
        direction=opts.get('direction', 'up'),
        extra=opts.get('extra', ['test'])
    )

e3a8b7d55144dcc6a0adeaabbc8a02bae8a3132b
<mask token>


@pytest.fixture(params=TEST_COMMENTS)
def up_stmt(request):
    return '%s %s' % (request.param, UP_CMD)


@pytest.fixture(params=TEST_COMMENTS)
def down_stmt(request):
    return '%s %s' % (request.param, DOWN_CMD)


class DebugRunner(object):

    def __init__(self, args):
        self.args = args
        self.ups = []
        self.downs = []

    def version(self):
        return ''

    def up(self, name, migration):
        self.ups.append((name, migration))
        yield 'info', migration.up

    def down(self, name, migration):
        self.downs.append((name, migration))
        yield 'info', migration.down


<mask token>

<mask token>


@pytest.fixture(params=TEST_COMMENTS)
def up_stmt(request):
    return '%s %s' % (request.param, UP_CMD)


@pytest.fixture(params=TEST_COMMENTS)
def down_stmt(request):
    return '%s %s' % (request.param, DOWN_CMD)


class DebugRunner(object):

    def __init__(self, args):
        self.args = args
        self.ups = []
        self.downs = []

    def version(self):
        return ''

    def up(self, name, migration):
        self.ups.append((name, migration))
        yield 'info', migration.up

    def down(self, name, migration):
        self.downs.append((name, migration))
        yield 'info', migration.down


<mask token>


@pytest.fixture
def migrator(request, migrations):
    marker = request.node.get_marker('migrator')
    opts = {} if marker is None else marker.kwargs
    return Migrator(count=opts.get('count', ALL), runner_path=opts.get(
        'runner_path', DEBUG_RUNNER_PATH), migrations_dir=str(migrations),
        direction=opts.get('direction', 'up'), extra=opts.get('extra', [
        'test']))

<mask token>
TEST_COMMENTS = ['#', '--', '//', ';', '"']
UP_CMD = 'MIGRATE UP'
DOWN_CMD = 'MIGRATE DOWN'


@pytest.fixture(params=TEST_COMMENTS)
def up_stmt(request):
    return '%s %s' % (request.param, UP_CMD)


@pytest.fixture(params=TEST_COMMENTS)
def down_stmt(request):
    return '%s %s' % (request.param, DOWN_CMD)


class DebugRunner(object):

    def __init__(self, args):
        self.args = args
        self.ups = []
        self.downs = []

    def version(self):
        return ''

    def up(self, name, migration):
        self.ups.append((name, migration))
        yield 'info', migration.up

    def down(self, name, migration):
        self.downs.append((name, migration))
        yield 'info', migration.down


DEBUG_RUNNER_PATH = '%s:%s' % (__name__, DebugRunner.__name__)


@pytest.yield_fixture
def migrations(request):
    """    take a specified amount of migrations and have them available in a
    temporary directory for the migrator. The migrations come from the
    `pytest.mark.migrations` marker, and are expect to be a number of
    (name, (up, down)) tuples, as args.

    Example:

        @pytest.mark.migrations(
            ('1', ('CREATE DATABASE test;', 'DROP DATABASE test;')),
        )
        def test_thing(migrations):
            # do something with migrations dir
            pass
    """
    marker = request.node.get_marker('migrations')
    migrations = [] if marker is None else marker.args
    up_cmd = '%s %s' % (TEST_COMMENTS[0], UP_CMD)
    down_cmd = '%s %s' % (TEST_COMMENTS[0], DOWN_CMD)
    migrations_dir = py.path.local.mkdtemp()
    for name, (up, down) in migrations:
        migration = '\n'.join([up_cmd, up, down_cmd, down])
        migrations_dir.join(name).write(migration)
    yield migrations_dir
    migrations_dir.remove()


@pytest.fixture
def migrator(request, migrations):
    marker = request.node.get_marker('migrator')
    opts = {} if marker is None else marker.kwargs
    return Migrator(count=opts.get('count', ALL), runner_path=opts.get(
        'runner_path', DEBUG_RUNNER_PATH), migrations_dir=str(migrations),
        direction=opts.get('direction', 'up'), extra=opts.get('extra', [
        'test']))

import pytest
import py
from trek import Migrator, ALL
TEST_COMMENTS = ['#', '--', '//', ';', '"']
UP_CMD = 'MIGRATE UP'
DOWN_CMD = 'MIGRATE DOWN'


@pytest.fixture(params=TEST_COMMENTS)
def up_stmt(request):
    return '%s %s' % (request.param, UP_CMD)


@pytest.fixture(params=TEST_COMMENTS)
def down_stmt(request):
    return '%s %s' % (request.param, DOWN_CMD)


class DebugRunner(object):

    def __init__(self, args):
        self.args = args
        self.ups = []
        self.downs = []

    def version(self):
        return ''

    def up(self, name, migration):
        self.ups.append((name, migration))
        yield 'info', migration.up

    def down(self, name, migration):
        self.downs.append((name, migration))
        yield 'info', migration.down


DEBUG_RUNNER_PATH = '%s:%s' % (__name__, DebugRunner.__name__)


@pytest.yield_fixture
def migrations(request):
    """    take a specified amount of migrations and have them available in a
    temporary directory for the migrator. The migrations come from the
    `pytest.mark.migrations` marker, and are expect to be a number of
    (name, (up, down)) tuples, as args.

    Example:

        @pytest.mark.migrations(
            ('1', ('CREATE DATABASE test;', 'DROP DATABASE test;')),
        )
        def test_thing(migrations):
            # do something with migrations dir
            pass
    """
    marker = request.node.get_marker('migrations')
    migrations = [] if marker is None else marker.args
    up_cmd = '%s %s' % (TEST_COMMENTS[0], UP_CMD)
    down_cmd = '%s %s' % (TEST_COMMENTS[0], DOWN_CMD)
    migrations_dir = py.path.local.mkdtemp()
    for name, (up, down) in migrations:
        migration = '\n'.join([up_cmd, up, down_cmd, down])
        migrations_dir.join(name).write(migration)
    yield migrations_dir
    migrations_dir.remove()


@pytest.fixture
def migrator(request, migrations):
    marker = request.node.get_marker('migrator')
    opts = {} if marker is None else marker.kwargs
    return Migrator(count=opts.get('count', ALL), runner_path=opts.get(
        'runner_path', DEBUG_RUNNER_PATH), migrations_dir=str(migrations),
        direction=opts.get('direction', 'up'), extra=opts.get('extra', [
        'test']))

# -*- coding: utf-8 -*-
import pytest
import py
from trek import Migrator, ALL

TEST_COMMENTS = [
    # representative comment characters from...
    '#',  # python
    '--', # sql
    '//', # c
    ';',  # clojure
    '"',  # viml
    # we probably don't need more than this *but* if in the future a comment
    # char or style screws things up add it to make sure we don't regress! This
    # is only a list of things I thought of immediately.
]
UP_CMD = 'MIGRATE UP'
DOWN_CMD = 'MIGRATE DOWN'

@pytest.fixture(params=TEST_COMMENTS)
def up_stmt(request):
    return '%s %s' % (request.param, UP_CMD)

@pytest.fixture(params=TEST_COMMENTS)
def down_stmt(request):
    return '%s %s' % (request.param, DOWN_CMD)


class DebugRunner(object):
    def __init__(self, args):
        self.args = args
        self.ups = []
        self.downs = []

    def version(self):
        return ''

    def up(self, name, migration):
        self.ups.append((name, migration))
        yield 'info', migration.up

    def down(self, name, migration):
        self.downs.append((name, migration))
        yield 'info', migration.down

DEBUG_RUNNER_PATH = '%s:%s' % (__name__, DebugRunner.__name__)


@pytest.yield_fixture
def migrations(request):
    """\
    take a specified amount of migrations and have them available in a
    temporary directory for the migrator. The migrations come from the
    `pytest.mark.migrations` marker, and are expect to be a number of
    (name, (up, down)) tuples, as args.

    Example:

        @pytest.mark.migrations(
            ('1', ('CREATE DATABASE test;', 'DROP DATABASE test;')),
        )
        def test_thing(migrations):
            # do something with migrations dir
            pass
    """
    marker = request.node.get_marker('migrations')
    migrations = [] if marker is None else marker.args

    # we don't need to test all the variations of migration comments here, only
    # in the parser. If this requirement changes in the future, the statements
    # here should just use the up_stmt and down_stmt fixtures.
    up_cmd = '%s %s' % (TEST_COMMENTS[0], UP_CMD)
    down_cmd = '%s %s' % (TEST_COMMENTS[0], DOWN_CMD)

    migrations_dir = py.path.local.mkdtemp()

    for name, (up, down) in migrations:
        migration = '\n'.join([up_cmd, up, down_cmd, down])
        migrations_dir.join(name).write(migration)

    yield migrations_dir

    migrations_dir.remove()


@pytest.fixture
def migrator(request, migrations):
    marker = request.node.get_marker('migrator')
    opts = {} if marker is None else marker.kwargs

    return Migrator(
        count=opts.get('count', ALL),
        runner_path=opts.get('runner_path', DEBUG_RUNNER_PATH),
        migrations_dir=str(migrations),
        direction=opts.get('direction', 'up'),
        extra=opts.get('extra', ['test'])
    )
