From 85e255b33577c52fc07de04a3ecdeb34e19ccfb7 Mon Sep 17 00:00:00 2001 From: "Samia, Michel (ms580j)" Date: Sat, 11 May 2019 00:37:47 +0200 Subject: [PATCH 1/3] [test] added a data migration that reproduces this issue --- .../app/migrations/0002_seed_data.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 pytest_django_test/app/migrations/0002_seed_data.py 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..38a4f4770 --- /dev/null +++ b/pytest_django_test/app/migrations/0002_seed_data.py @@ -0,0 +1,18 @@ +# 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), + ] From 3b496ce845b6678ffc59fc364e0493310ec2f6e0 Mon Sep 17 00:00:00 2001 From: Paul Prescod Date: Tue, 22 Oct 2019 18:00:14 -0700 Subject: [PATCH 2/3] Demonstrates Issue #713 --- .../app/migrations/0002_seed_data.py | 13 +-- tests/test_bad_environment.py | 105 ++++++++++++++++++ 2 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 tests/test_bad_environment.py diff --git a/pytest_django_test/app/migrations/0002_seed_data.py b/pytest_django_test/app/migrations/0002_seed_data.py index 38a4f4770..1ba397052 100644 --- a/pytest_django_test/app/migrations/0002_seed_data.py +++ b/pytest_django_test/app/migrations/0002_seed_data.py @@ -1,18 +1,9 @@ -# 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): + """Placeholder migration to be replaced in tests.""" + dependencies = [ ('app', '0001_initial'), ] - - operations = [ - migrations.RunPython(cb_seed_items), - ] diff --git a/tests/test_bad_environment.py b/tests/test_bad_environment.py new file mode 100644 index 000000000..87ba98cfe --- /dev/null +++ b/tests/test_bad_environment.py @@ -0,0 +1,105 @@ +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 == 0 + assert "Operations to perform:" not in result.stdout.str() + result.stdout.fnmatch_lines(["*= 1 passed 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*"]) From f1d379f0d5b2d6a0178417253b9e349e6ca6be2c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 23 Oct 2019 08:17:18 +0200 Subject: [PATCH 3/3] adjust test --- tests/test_bad_environment.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_bad_environment.py b/tests/test_bad_environment.py index 87ba98cfe..614eb362c 100644 --- a/tests/test_bad_environment.py +++ b/tests/test_bad_environment.py @@ -46,9 +46,13 @@ def test_inner_migrations(): result = django_testdir.runpytest_subprocess( "-vv", "-s", ) - assert result.ret == 0 - assert "Operations to perform:" not in result.stdout.str() - result.stdout.fnmatch_lines(["*= 1 passed in *"]) + 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