Beispiel #1
0
def test_migrate_init_extension_schema_fixture(capsys, settings, mocker):
    root = os.path.dirname(__file__)
    settings.NORTH_MIGRATIONS_ROOT = os.path.join(root, 'test_data/sql')

    mocker.patch(
        'django_north.management.migrations.get_version_for_init',
        return_value='16.12')
    mock_run_script = mocker.patch(
        'django_north.management.commands.migrate.Command.run_script')
    command = migrate.Command()
    command.verbosity = 2

    # extensions & fixtures
    settings.NORTH_BEFORE_SCHEMA_FILES = ['extension.sql']
    settings.NORTH_ADDITIONAL_SCHEMA_FILES = []
    settings.NORTH_AFTER_SCHEMA_FILES = []
    command.init_schema()
    assert len(mock_run_script.call_args_list) == 3
    assert mock_run_script.call_args_list[0] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'extension.sql'))
    assert mock_run_script.call_args_list[1] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'schema_16.12.sql'))
    assert mock_run_script.call_args_list[2] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'fixtures',
                     'fixtures_16.12.sql'))
    captured = capsys.readouterr()
    assert captured.out == (
        'Load extension.sql\n'
        'Load schema\n'
        '  Applying 16.12...\n'
        'Load fixtures\n'
        '  Applying 16.12...\n'
    )
Beispiel #2
0
def test_migrate_run_script(settings, mocker):
    root = os.path.dirname(__file__)
    settings.NORTH_MIGRATIONS_ROOT = os.path.join(root, 'test_data/sql')

    mock_run = mocker.patch('django_north.management.runner.Script.run')

    command = migrate.Command()

    path = os.path.join(settings.NORTH_MIGRATIONS_ROOT,
                        '16.12/16.12-0-version-dml.sql')
    command.run_script(path)
    assert len(mock_run.call_args_list) == 1
def test_migrate_init_schema(capsys, settings, mocker):
    root = os.path.dirname(__file__)
    settings.NORTH_MIGRATIONS_ROOT = os.path.join(root, 'test_data/sql')

    mocker.patch(
        'django_north.management.migrations.get_version_for_init',
        return_value='16.12')
    mock_run_script = mocker.patch(
        'django_north.management.commands.migrate.Command.run_script')

    command = migrate.Command()
    command.verbosity = 2

    # no additional files
    if hasattr(settings, 'NORTH_ADDITIONAL_SCHEMA_FILES'):
        del settings.NORTH_ADDITIONAL_SCHEMA_FILES
    command.init_schema()
    assert len(mock_run_script.call_args_list) == 2
    assert mock_run_script.call_args_list[0] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'schema_16.12.sql'))
    assert mock_run_script.call_args_list[1] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'fixtures',
                     'fixtures_16.12.sql'))
    captured = capsys.readouterr()
    assert captured.out == (
        'Load schema\n'
        '  Applying 16.12...\n'
        'Load fixtures\n'
        '  Applying 16.12...\n'
    )

    mock_run_script.reset_mock()
    command = migrate.Command()
    command.verbosity = 2

    # extensions & fixtures
    settings.NORTH_ADDITIONAL_SCHEMA_FILES = ['extension.sql']
    command.init_schema()
    assert len(mock_run_script.call_args_list) == 3
    assert mock_run_script.call_args_list[0] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'extension.sql'))
    assert mock_run_script.call_args_list[1] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'schema_16.12.sql'))
    assert mock_run_script.call_args_list[2] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'fixtures',
                     'fixtures_16.12.sql'))
    captured = capsys.readouterr()
    assert captured.out == (
        'Load extension.sql\n'
        'Load schema\n'
        '  Applying 16.12...\n'
        'Load fixtures\n'
        '  Applying 16.12...\n'
    )

    mock_run_script.reset_mock()
    command = migrate.Command()
    command.verbosity = 2

    # extensions, roles & fixtures
    settings.NORTH_ADDITIONAL_SCHEMA_FILES = ['extension.sql', 'roles.sql']
    command.init_schema()
    assert len(mock_run_script.call_args_list) == 4
    assert mock_run_script.call_args_list[0] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'extension.sql'))
    assert mock_run_script.call_args_list[1] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'roles.sql'))
    assert mock_run_script.call_args_list[2] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'schema_16.12.sql'))
    assert mock_run_script.call_args_list[3] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'fixtures',
                     'fixtures_16.12.sql'))
    captured = capsys.readouterr()
    assert captured.out == (
        'Load extension.sql\n'
        'Load roles.sql\n'
        'Load schema\n'
        '  Applying 16.12...\n'
        'Load fixtures\n'
        '  Applying 16.12...\n'
    )

    mock_run_script.reset_mock()
    command = migrate.Command()
    command.verbosity = 2

    # no fixtures for this version
    mocker.patch(
        'django_north.management.migrations.get_version_for_init',
        return_value='17.3')
    del settings.NORTH_ADDITIONAL_SCHEMA_FILES
    command.init_schema()
    assert len(mock_run_script.call_args_list) == 2
    assert mock_run_script.call_args_list[0] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'schema_17.3.sql'))
    captured = capsys.readouterr()
    assert captured.out == (
        'Load schema\n'
        '  Applying 17.3...\n'
        'Load fixtures\n'
        '  Applying 16.12...\n'
    )

    mock_run_script.reset_mock()
    command = migrate.Command()
    command.verbosity = 2

    # no fixtures
    mocker.patch(
        'django_north.management.migrations.get_version_for_init',
        return_value='16.11')
    del settings.NORTH_ADDITIONAL_SCHEMA_FILES
    command.init_schema()
    assert len(mock_run_script.call_args_list) == 1
    assert mock_run_script.call_args_list[0] == mocker.call(
        os.path.join(settings.NORTH_MIGRATIONS_ROOT, 'schemas',
                     'schema_16.11.sql'))
    captured = capsys.readouterr()
    assert captured.out == (
        'Load schema\n'
        '  Applying 16.11...\n'
    )