Esempio n. 1
0
def test_migration_properties(migration_name, attribute, expected_exception,
                              expected_value):
    """Asserts various properties of migrations from the test models"""

    migrations = core.Migrations()
    migration = migrations._migrations[migration_name]
    with expected_exception:
        assert getattr(migration, attribute) == expected_value
Esempio n. 2
0
def test_bad_migration_sql_collection(mocker):
    mocker.patch.object(
        MigrationExecutor,
        'collect_sql',
        autospec=True,
        side_effect=RuntimeError('Cannot collect.'),
    )

    migrations = core.Migrations()

    assert len(migrations) == 3
    for migration in migrations:
        assert migration.sql == 'Error obtaining SQL - "Cannot collect."'
Esempio n. 3
0
def test_bad_migration_sql_collection(mocker):
    if (django.VERSION[0] >= 3
            and django.VERSION[1] >= 1) or django.VERSION[0] >= 4:
        MigrationSqlClass = MigrationLoader
    else:
        MigrationSqlClass = MigrationExecutor

    mocker.patch.object(
        MigrationSqlClass,
        'collect_sql',
        autospec=True,
        side_effect=RuntimeError('Cannot collect.'),
    )

    migrations = core.Migrations()

    assert len(migrations) == 3
    for migration in migrations:
        assert migration.sql == 'Error obtaining SQL - "Cannot collect."'
def test_migration_filtering(migration_docs_config):
    """Tests various filtering methods of the core Migrations object"""
    docs_file = migration_docs_config / 'docs.yaml'
    with open(docs_file, 'w+') as f:
        f.write(
            yaml.safe_dump({
                'tests.0001_initial': {
                    '_hash': 'd1468bb233398bcc089bd33151f51cd3',
                    'type': 'before',
                    'point_of_contact': 'john',
                },
                'tests.0002_testmodel_field2': {
                    '_hash': '8cb7f6747d4ef78074521309d5d05a2c',
                    'type': 'after',
                    'point_of_contact': 'john',
                },
                'tests.0003_testmodel_field3': {
                    '_hash': 'e53ffdcc6e8b9cc7f8a960e49968040f',
                    'type': 'before',
                },
            }))

    migrations = core.Migrations()

    # Check various migration properties
    after_migration = list(migrations.filter('type', 'after'))[0]
    assert str(after_migration.hash) == '8cb7f6747d4ef78074521309d5d05a2c'

    # Check various filterings
    assert len(migrations.filter('type', 'before')) == 2
    assert len(migrations.exclude('type', 'before')) == 1
    assert len(migrations.filter('type', 'after')) == 1
    assert (len(
        migrations.filter('type', 'before').filter('point_of_contact',
                                                   '.*jo.*',
                                                   match=True)) == 1)

    # Check groupings
    type_groups = migrations.group('type')
    assert len(type_groups) == 2
    assert len(type_groups['before']) == 2
    assert len(type_groups['after']) == 1
    poc_groups = migrations.group('point_of_contact')
    assert len(poc_groups[None]) == 1
    assert len(poc_groups['john']) == 2

    # Check group sorting
    assert list(migrations.group('type', ascending_keys=True)) == [
        'after',
        'before',
    ]
    assert list(migrations.group('point_of_contact', ascending_keys=True)) == [
        'john',
        None,
    ]
    assert list(migrations.group('type', descending_keys=True)) == [
        'before',
        'after',
    ]
    assert list(
        migrations.group('point_of_contact',
                         ascending_keys=True,
                         none_key_first=True)) == [None, 'john']

    # Verify filtering on applied migrations
    assert len(migrations.filter('applied', True)) == 3
    call_command('migrate', 'tests', '0001')

    migrations = core.Migrations()
    assert len(migrations.filter('applied', True)) == 1
    call_command('migrate', 'tests')

    migrations = core.Migrations()
    assert len(migrations.filter('applied', True)) == 3