diff --git a/pytest_django_test/app/migrations/0002_seed_data.py b/pytest_django_test/app/migrations/0002_seed_data.py new file mode 100644 index 000000000..1ba397052 --- /dev/null +++ b/pytest_django_test/app/migrations/0002_seed_data.py @@ -0,0 +1,9 @@ +from django.db import migrations + + +class Migration(migrations.Migration): + """Placeholder migration to be replaced in tests.""" + + dependencies = [ + ('app', '0001_initial'), + ] diff --git a/tests/test_bad_environment.py b/tests/test_bad_environment.py new file mode 100644 index 000000000..614eb362c --- /dev/null +++ b/tests/test_bad_environment.py @@ -0,0 +1,109 @@ +bad_migration = """ +# Generated by Django 2.0.8 on 2018-09-05 14:47 + +from django.db import migrations + + +def cb_seed_items(apps, schema_editor): + Item = apps.get_model('app', 'Item') + Item.objects.get(name='This one is not there') + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0001_initial'), + ] + + operations = [ + migrations.RunPython(cb_seed_items), + ] +""" + + +class TestNativeMigrations(object): + """ Tests for Django Migrations """ + + def test_migration_should_not_cause_internal_error(self, django_testdir): + django_testdir.create_test_module( + """ + import pytest + + @pytest.mark.django_db + def test_inner_migrations(): + from .app.models import Item + Item.objects.create() + """ + ) + + migration_file = django_testdir.project_root.join( + "tpkg/app/migrations/0002_seed_data.py" + ) + assert migration_file.isfile() + migration_file.write( + bad_migration, ensure=True + ) + + result = django_testdir.runpytest_subprocess( + "-vv", "-s", + ) + assert result.ret == 1 + assert "Operations to perform:" in result.stdout.str() + result.stdout.fnmatch_lines([ + "Running migrations:", + "E __fake__.Item.DoesNotExist: Item matching query does not exist.", + "*= 1 error in *", + ]) + + def test_migrations_run(self, django_testdir): + testdir = django_testdir + testdir.create_test_module( + """ + import pytest + + @pytest.mark.django_db + def test_inner_migrations(): + from .app.models import Item + Item.objects.create() + """ + ) + + testdir.create_app_file( + """ + from django.db import migrations, models + + def print_it(apps, schema_editor): + print("mark_migrations_run") + + class Migration(migrations.Migration): + + dependencies = [] + + operations = [ + migrations.CreateModel( + name='Item', + fields=[ + ('id', models.AutoField(serialize=False, + auto_created=True, + primary_key=True)), + ('name', models.CharField(max_length=100)), + ], + options={ + }, + bases=(models.Model,), + ), + migrations.RunPython( + print_it, + ), + ] + """, + "migrations/0001_initial.py", + ) + result = testdir.runpytest_subprocess("--tb=short", "-v", "-s") + assert result.ret == 0 + result.stdout.fnmatch_lines(["*mark_migrations_run*"]) + + result = testdir.runpytest_subprocess( + "--no-migrations", "--migrations", "--tb=short", "-v", "-s" + ) + assert result.ret == 0 + result.stdout.fnmatch_lines(["*mark_migrations_run*"])