Skip to content

Commit

Permalink
Test proxy models with non-model or abstract bases (#158)
Browse files Browse the repository at this point in the history
Co-authored-by: Giannis Terzopoulos <[email protected]>
  • Loading branch information
wesleykendall and g-nie authored Aug 26, 2024
1 parent e32c033 commit 8b24aad
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pgtrigger/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion pgtrigger/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
60 changes: 60 additions & 0 deletions pgtrigger/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
###
Expand Down

0 comments on commit 8b24aad

Please sign in to comment.