1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
"""
This module covers simple direct migrations.
We test both schema and data-migrations here.
"""
import pytest
from django.core.exceptions import FieldError
from django.db.utils import IntegrityError
@pytest.mark.django_db
def test_pytest_plugin_initial(migrator):
"""Ensures that the initial migration works."""
old_state = migrator.apply_initial_migration(('main_app', None))
with pytest.raises(LookupError):
# Models does not yet exist:
old_state.apps.get_model('main_app', 'SomeItem')
new_state = migrator.apply_tested_migration(('main_app', '0001_initial'))
# After the initial migration is done, we can use the model state:
SomeItem = new_state.apps.get_model('main_app', 'SomeItem')
assert SomeItem.objects.filter(string_field='').count() == 0
@pytest.mark.django_db
def test_pytest_plugin0001(migrator):
"""Ensures that the first migration works."""
old_state = migrator.apply_initial_migration(('main_app', '0001_initial'))
SomeItem = old_state.apps.get_model('main_app', 'SomeItem')
with pytest.raises(FieldError):
SomeItem.objects.filter(is_clean=True)
new_state = migrator.apply_tested_migration(
('main_app', '0002_someitem_is_clean'),
)
SomeItem = new_state.apps.get_model('main_app', 'SomeItem')
assert SomeItem.objects.filter(is_clean=True).count() == 0
@pytest.mark.django_db
def test_pytest_plugin0002(migrator):
"""Ensures that the second migration works."""
old_state = migrator.apply_initial_migration(
('main_app', '0002_someitem_is_clean'),
)
SomeItem = old_state.apps.get_model('main_app', 'SomeItem')
SomeItem.objects.create(string_field='a')
SomeItem.objects.create(string_field='a b')
assert SomeItem.objects.count() == 2
assert SomeItem.objects.filter(is_clean=True).count() == 2
new_state = migrator.apply_tested_migration(
('main_app', '0003_update_is_clean'),
)
SomeItem = new_state.apps.get_model('main_app', 'SomeItem')
assert SomeItem.objects.count() == 2
assert SomeItem.objects.filter(is_clean=True).count() == 1
@pytest.mark.django_db
def test_pytest_plugin0003(migrator):
"""Ensures that the third migration works."""
old_state = migrator.apply_initial_migration(
('main_app', '0003_update_is_clean'),
)
SomeItem = old_state.apps.get_model('main_app', 'SomeItem')
SomeItem.objects.create(string_field='a') # default is still there
assert SomeItem.objects.count() == 1
assert SomeItem.objects.filter(is_clean=True).count() == 1
new_state = migrator.apply_tested_migration(
('main_app', '0004_auto_20191119_2125'),
)
SomeItem = new_state.apps.get_model('main_app', 'SomeItem')
with pytest.raises(IntegrityError):
SomeItem.objects.create(string_field='b') # no default anymore
|