diff --git a/CHANGELOG.md b/CHANGELOG.md index befc01f5..5246f16f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed errors with reverse M2M relationships [PR #299](https://github.com/model-bakers/model_bakery/pull/299) - Fixed errors with reverse M2O relationships [PR #300](https://github.com/model-bakers/model_bakery/pull/300) - Improve exception message for unknown field types [PR #301](https://github.com/model-bakers/model_bakery/pull/301) +- Fixed random generation of ContentType values when there is no database access [#290](https://github.com/model-bakers/model_bakery/pull/290) ### Removed diff --git a/model_bakery/random_gen.py b/model_bakery/random_gen.py index 74167425..c4f584d2 100644 --- a/model_bakery/random_gen.py +++ b/model_bakery/random_gen.py @@ -206,7 +206,11 @@ def gen_content_type(): try: return ContentType.objects.get_for_model(choice(apps.get_models())) - except AssertionError: + except (AssertionError, RuntimeError): + # AssertionError is raised by Django's test framework when db access is not available: + # https://github.com/django/django/blob/stable/4.0.x/django/test/testcases.py#L150 + # RuntimeError is raised by pytest-django when db access is not available: + # https://github.com/pytest-dev/pytest-django/blob/v4.5.2/pytest_django/plugin.py#L709 warnings.warn("Database access disabled, returning ContentType raw instance") return ContentType() diff --git a/tests/test_baker.py b/tests/test_baker.py index 21e08aab..e955f9d6 100644 --- a/tests/test_baker.py +++ b/tests/test_baker.py @@ -5,6 +5,7 @@ import pytest from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.db.models import Manager from django.db.models.signals import m2m_changed from django.test import TestCase, override_settings @@ -570,7 +571,7 @@ def test_field_lookup_for_related_field_does_not_work_with_prepare(self): assert 0 == models.RelatedNamesModel.objects.count() def test_ensure_reverse_fk_for_many_to_one_is_working(self): - """This is a regression test to make sure issue 291 is fixed""" + """This is a regression test to make sure issue 291 is fixed.""" fk1, fk2 = baker.prepare( models.Issue291Model3, fk_model_2=None, name="custom name", _quantity=2 ) @@ -609,13 +610,23 @@ class TestHandlingContentTypeField: def test_create_model_with_contenttype_field(self): dummy = baker.make(models.DummyGenericForeignKeyModel) assert isinstance(dummy, models.DummyGenericForeignKeyModel) + assert isinstance(dummy.content_type, ContentType) -@pytest.mark.django_db class TestHandlingContentTypeFieldNoQueries: def test_create_model_with_contenttype_field(self): - dummy = baker.prepare(models.DummyGenericForeignKeyModel) + # Clear ContentType's internal cache so that it *will* try to connect to + # the database in order to fetch the corresponding ContentType model for + # a randomly chosen model. + ContentType.objects.clear_cache() + + with pytest.warns( + UserWarning, + match="Database access disabled, returning ContentType raw instance", + ): + dummy = baker.prepare(models.DummyGenericForeignKeyModel) assert isinstance(dummy, models.DummyGenericForeignKeyModel) + assert isinstance(dummy.content_type, ContentType) @pytest.mark.django_db