File: test_pytest_plugin_reverse.py

package info (click to toggle)
python-django-test-migrations 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: python: 1,479; makefile: 26
file content (48 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download
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
"""
This module covers tests for migration rollbacks.

It might be useful when something goes wrong
and you need to switch back to the previous state.
"""

import pytest
from django.core.exceptions import FieldError


@pytest.mark.django_db
def test_pytest_plugin0001(migrator):
    """Ensures that the first migration works."""
    old_state = migrator.apply_initial_migration(
        ('main_app', '0002_someitem_is_clean'),
    )
    SomeItem = old_state.apps.get_model('main_app', 'SomeItem')

    assert SomeItem.objects.filter(is_clean=True).count() == 0

    new_state = migrator.apply_tested_migration(('main_app', '0001_initial'))
    SomeItem = new_state.apps.get_model('main_app', 'SomeItem')

    with pytest.raises(FieldError):
        SomeItem.objects.filter(is_clean=True)


@pytest.mark.django_db
def test_pytest_plugin0002(migrator):
    """Ensures that the second 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', is_clean=True)
    SomeItem.objects.create(string_field='a b', is_clean=False)

    assert SomeItem.objects.count() == 2
    assert SomeItem.objects.filter(is_clean=True).count() == 1

    new_state = migrator.apply_tested_migration(
        ('main_app', '0002_someitem_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