fix coverage

This commit is contained in:
Johanan Oppong Amoateng 2025-12-25 13:31:51 +00:00
parent e3dae4242d
commit c47046a362
2 changed files with 44 additions and 0 deletions

View File

@ -413,12 +413,31 @@ class PolymorphicTests(TransactionTestCase):
)
def test_create_instanceof_q(self):
# Test with a list of models
q = query_translate.create_instanceof_q([Model2B])
expected = sorted(
ContentType.objects.get_for_model(m).pk for m in [Model2B, Model2C, Model2D]
)
assert dict(q.children) == dict(polymorphic_ctype__in=expected)
# Test with empty list - should return None
q = query_translate.create_instanceof_q([])
assert q is None
# Test with None - should return None
q = query_translate.create_instanceof_q(None)
assert q is None
# Test error when passing non-PolymorphicModel type
from django.contrib.auth.models import User
with pytest.raises(TypeError):
query_translate.create_instanceof_q([User])
# Test error when passing invalid type (not a model class)
with pytest.raises(TypeError):
query_translate.create_instanceof_q([123])
def test_base_manager(self):
def base_manager(model):
return (type(model._base_manager), model._base_manager.model)
@ -633,6 +652,18 @@ class PolymorphicTests(TransactionTestCase):
ordered=False,
)
# Test error with invalid model string (non-existent model)
with pytest.raises(ValueError, match="model.*not found"):
Model2A.objects.instance_of("tests.NonExistentModel")
# Test error with short name but no context (using create_instanceof_q directly)
with pytest.raises(ValueError, match="not found"):
query_translate.create_instanceof_q("InvalidModel", queryset_model=None)
# Test error when string resolves to non-PolymorphicModel
with pytest.raises(TypeError, match="is not a PolymorphicModel"):
Model2A.objects.instance_of("contenttypes.ContentType")
def test_instance_of_Q_object_with_string_reference(self):
"""Test instance_of in Q-object with string model reference (issue #505)"""
self.create_model2abcd()

View File

@ -77,3 +77,16 @@ class QueryTranslateTests(TestCase):
self.assertEqual(DeepCopyTester.objects.all().delete()[0], 3)
self.assertEqual(DeepCopyTester.objects.count(), 0)
def test_proxy_model_query_related_name(self):
"""Test _get_query_related_name fallback for proxy models"""
from polymorphic.query_translate import _get_query_related_name
from polymorphic.tests.models import ProxyChild, SubclassSelectorProxyModel
# Test that proxy models use the fallback (lowercase class name)
# since they don't have a OneToOneField parent link
result = _get_query_related_name(ProxyChild)
assert result == "proxychild"
result = _get_query_related_name(SubclassSelectorProxyModel)
assert result == "subclassselectorproxymodel"