Skip to content

[WIP] for issue 713: INTERNALERROR due to DB access in repr #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pytest_django_test/app/migrations/0002_seed_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import migrations


class Migration(migrations.Migration):
"""Placeholder migration to be replaced in tests."""

dependencies = [
('app', '0001_initial'),
]
109 changes: 109 additions & 0 deletions tests/test_bad_environment.py
Original file line number Diff line number Diff line change
@@ -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*"])