mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
split in apps per test
This commit is contained in:
parent
1d4cbee39a
commit
5ff94aa1f0
1
tests/contribute_to_meta/.gitignore
vendored
Normal file
1
tests/contribute_to_meta/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
apps/*/migrations/0001_initial.py
|
7
tests/contribute_to_meta/apps/modelsimple/models.py
Normal file
7
tests/contribute_to_meta/apps/modelsimple/models.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from ...fields import ConstraintField
|
||||||
|
|
||||||
|
|
||||||
|
class Model(models.Model):
|
||||||
|
field = ConstraintField(max_length=10)
|
10
tests/contribute_to_meta/apps/modelwithmeta/models.py
Normal file
10
tests/contribute_to_meta/apps/modelwithmeta/models.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from ...fields import ConstraintField
|
||||||
|
|
||||||
|
|
||||||
|
class Model(models.Model):
|
||||||
|
class Meta:
|
||||||
|
constraints = []
|
||||||
|
|
||||||
|
field = ConstraintField(max_length=10)
|
@ -1 +0,0 @@
|
|||||||
0001_initial.py
|
|
@ -1,14 +0,0 @@
|
|||||||
from django.db import models
|
|
||||||
|
|
||||||
from .fields import ConstraintField
|
|
||||||
|
|
||||||
|
|
||||||
class ModelA(models.Model):
|
|
||||||
field = ConstraintField(max_length=10)
|
|
||||||
|
|
||||||
|
|
||||||
class ModelC(models.Model):
|
|
||||||
class Meta:
|
|
||||||
constraints = []
|
|
||||||
|
|
||||||
field = ConstraintField(max_length=10)
|
|
@ -1,49 +1,50 @@
|
|||||||
|
from importlib import import_module
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
from django.test import TestCase, skipUnlessDBFeature
|
from django.test import TransactionTestCase, override_settings, skipUnlessDBFeature
|
||||||
|
|
||||||
from .models import ModelA, ModelC
|
apps = [
|
||||||
|
"contribute_to_meta.apps.modelsimple",
|
||||||
|
"contribute_to_meta.apps.modelwithmeta",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(INSTALLED_APPS=apps)
|
||||||
@skipUnlessDBFeature("supports_table_check_constraints")
|
@skipUnlessDBFeature("supports_table_check_constraints")
|
||||||
class ConstraintsTests(TestCase):
|
class ConstraintsTests(TransactionTestCase):
|
||||||
"""Check that the constraints allow valid values and reject invalid ones"""
|
"""Check that the constraints allow valid values and reject invalid ones"""
|
||||||
|
|
||||||
def test_ModelA_constraint(self):
|
available_apps = apps
|
||||||
ModelA.objects.create(field="valid")
|
|
||||||
|
def _do_test(self, app_qualified_name):
|
||||||
|
app_name = app_qualified_name.split(".")[-1]
|
||||||
|
|
||||||
|
# Reset the migrations
|
||||||
|
folder = Path(__file__).parent / "apps" / app_name / "migrations"
|
||||||
|
migration_path = folder / "0001_initial.py"
|
||||||
|
migration_path.unlink(missing_ok=True)
|
||||||
|
|
||||||
|
# Run the migrations
|
||||||
|
call_command("makemigrations", app_name, "--verbosity", "0")
|
||||||
|
call_command("migrate", app_name, "--verbosity", "0")
|
||||||
|
|
||||||
|
# Check that the constraint behaves as expected
|
||||||
|
Model = import_module(app_qualified_name).models.Model
|
||||||
|
Model.objects.create(field="valid")
|
||||||
with self.assertRaises(IntegrityError):
|
with self.assertRaises(IntegrityError):
|
||||||
ModelA.objects.create(field="invalid")
|
Model.objects.create(field="invalid")
|
||||||
|
|
||||||
def test_ModelC_constraint(self):
|
# Check that the constraint is present in the migration file
|
||||||
ModelC.objects.create(field="valid")
|
migration_path = folder / "0001_initial.py"
|
||||||
with self.assertRaises(IntegrityError):
|
content = migration_path.read_text()
|
||||||
ModelC.objects.create(field="invalid")
|
|
||||||
|
|
||||||
|
|
||||||
class ConstraintsMigrationsTests(TestCase):
|
|
||||||
"""Check that migrations correctly generate the constraints"""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls):
|
|
||||||
super().setUpClass()
|
|
||||||
# Clean and recreate migration files for further inspection
|
|
||||||
init_file = Path(__file__).parent / "migrations" / "0001_initial.py"
|
|
||||||
init_file.unlink(missing_ok=True)
|
|
||||||
call_command("makemigrations", verbosity=0)
|
|
||||||
cls.migration_content = init_file.read_text()
|
|
||||||
|
|
||||||
def test_ModelA(self):
|
|
||||||
# check migration contents
|
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
"test_constraint_modela" in self.migration_content,
|
"models.CheckConstraint" in content, f"No constraint in `{migration_path}`"
|
||||||
"Could not find constraint `test_constraint_modela` in migration",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_ModelC(self):
|
def test_modelsimple(self):
|
||||||
# check migration contents
|
self._do_test("contribute_to_meta.apps.modelsimple")
|
||||||
self.assertTrue(
|
|
||||||
"test_constraint_modelc" in self.migration_content,
|
def test_modelwithmeta(self):
|
||||||
"Could not find constraint `test_constraint_modelc` in migration",
|
self._do_test("contribute_to_meta.apps.modelwithmeta")
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user