File: test_migrations.py

package info (click to toggle)
python-djangorestframework-simplejwt 5.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 956 kB
  • sloc: python: 3,783; makefile: 213; javascript: 40; sh: 6
file content (42 lines) | stat: -rw-r--r-- 1,530 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
from io import StringIO
from typing import Optional

import pytest
from django.core.management import call_command
from django.test import TestCase


class MigrationTestCase(TestCase):
    def test_no_missing_migrations(self):
        """
        Ensures all model changes are reflected in migrations.
        If this test fails, there are model changes that require a new migration.

        Behavior:
        - Passes silently if no migrations are required
        - Fails with a detailed message if migrations are need
        """

        output = StringIO()

        # Initialize exception variable to track migration check result
        exec: Optional[SystemExit] = None

        try:
            # Check for pending migrations without actually creating them
            call_command(
                "makemigrations", "--check", "--dry-run", stdout=output, stderr=output
            )
        except SystemExit as e:
            # Capture the SystemExit if migrations are needed (the command will had ended with exit code 1)
            exec = e

        # If an exception was raised, verify it indicates no migration changes are required
        if exec is not None:
            self.assertEqual(
                exec.code,
                0,  # 0 means no migrations needed
                f"Model changes detected that require migrations!\n"
                f"Please run `python manage.py makemigrations` to create the necessary migrations.\n\n"
                f"Detected Changes:\n{output.getvalue()}",
            )