From 8b24aadb1ada2a8cfb57e2f7283f9eed3d7992f8 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Mon, 26 Aug 2024 08:57:47 -0500 Subject: [PATCH] Test proxy models with non-model or abstract bases (#158) Co-authored-by: Giannis Terzopoulos --- pgtrigger/migrations.py | 3 +- pgtrigger/tests/test_core.py | 1 - pgtrigger/tests/test_migrations.py | 60 ++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/pgtrigger/migrations.py b/pgtrigger/migrations.py index 8b7a103..8c26142 100644 --- a/pgtrigger/migrations.py +++ b/pgtrigger/migrations.py @@ -148,7 +148,8 @@ def _inject_m2m_dependency_in_proxy(proxy_op): proxy models. Inject the dependency here """ for base in proxy_op.bases: - # Skip abstract models + # Ignore inherited bases that are not models or abstract + # https://github.com/Opus10/django-pgtrigger/issues/126 if not (isinstance(base, str) and "." in base): continue diff --git a/pgtrigger/tests/test_core.py b/pgtrigger/tests/test_core.py index 8ef1f0a..8abb064 100644 --- a/pgtrigger/tests/test_core.py +++ b/pgtrigger/tests/test_core.py @@ -1,7 +1,6 @@ import datetime as dt import ddf -import django import pytest from django.contrib.auth.models import User from django.db import transaction diff --git a/pgtrigger/tests/test_migrations.py b/pgtrigger/tests/test_migrations.py index 08bc5e9..06ca46a 100644 --- a/pgtrigger/tests/test_migrations.py +++ b/pgtrigger/tests/test_migrations.py @@ -18,6 +18,12 @@ from pgtrigger.tests import utils +class SomeMixin: + """For testing proxy model inheritance""" + + pass + + @pytest.fixture(autouse=True) def disble_install_on_migrate(settings): settings.PGTRIGGER_INSTALL_ON_MIGRATE = False @@ -429,6 +435,60 @@ class Meta: with utils.raises_trigger_error(match="Cannot delete"): protected_model.groups.clear() + ### + # Proxy model inheritance + # https://github.com/Opus10/django-pgtrigger/issues/126 + ### + + # Set up multiple inheritance for a proxy model with a mixin + class SomeBaseModel(models.Model): + pass + + class SomeProxyModel(SomeMixin, SomeBaseModel): + class Meta: + proxy = True + + test_models.SomeBaseModel = SomeBaseModel + test_models.SomeProxyModel = SomeProxyModel + + make_migrations(atomic) + num_expected_migrations += 1 + assert num_migration_files() == num_expected_migrations + call_command("migrate") + assert_all_triggers_installed() + + class SomeAbstractModel(models.Model): + class Meta: + abstract = True + + class SomeAbstractProxyModel(SomeAbstractModel, SomeBaseModel): + class Meta: + proxy = True + + test_models.SomeAbstractModel = SomeAbstractModel + test_models.SomeAbstractProxyModel = SomeAbstractProxyModel + + make_migrations(atomic) + num_expected_migrations += 1 + assert num_migration_files() == num_expected_migrations + call_command("migrate") + assert_all_triggers_installed() + + del test_models.SomeAbstractModel + del test_models.SomeBaseModel + del test_models.SomeProxyModel + del test_models.SomeAbstractProxyModel + del apps.app_configs["tests"].models["somebasemodel"] + del apps.app_configs["tests"].models["someproxymodel"] + del apps.app_configs["tests"].models["someabstractproxymodel"] + apps.clear_cache() + + make_migrations(atomic) + num_expected_migrations += 1 + assert num_migration_files() == num_expected_migrations + call_command("migrate") + assert_all_triggers_installed() + ### # Keep only deletion protection for a dynamic through model and migrate ###