From ab1f5fa8fc15f33be0e8ce127e1109e0c6ee4e32 Mon Sep 17 00:00:00 2001 From: Marica Odagaki Date: Mon, 7 Feb 2022 11:37:07 -0800 Subject: [PATCH 1/5] Remove django_db mark from test case that checks behavior under no db access --- tests/test_baker.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_baker.py b/tests/test_baker.py index b4ac5176..bf29098b 100644 --- a/tests/test_baker.py +++ b/tests/test_baker.py @@ -594,7 +594,6 @@ def test_create_model_with_contenttype_field(self): assert isinstance(dummy, models.DummyGenericForeignKeyModel) -@pytest.mark.django_db class TestHandlingContentTypeFieldNoQueries: def test_create_model_with_contenttype_field(self): dummy = baker.prepare(models.DummyGenericForeignKeyModel) From 2e4ecb0f8f52c89cd82450fd5693a1cd251f63a2 Mon Sep 17 00:00:00 2001 From: Marica Odagaki Date: Mon, 7 Feb 2022 11:42:00 -0800 Subject: [PATCH 2/5] Allow generating ContentType values without db access --- model_bakery/random_gen.py | 6 +++++- tests/test_baker.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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 bf29098b..a5a74f51 100644 --- a/tests/test_baker.py +++ b/tests/test_baker.py @@ -596,7 +596,11 @@ def test_create_model_with_contenttype_field(self): class TestHandlingContentTypeFieldNoQueries: def test_create_model_with_contenttype_field(self): - dummy = baker.prepare(models.DummyGenericForeignKeyModel) + with pytest.warns( + UserWarning, + match="Database access disabled, returning ContentType raw instance", + ): + dummy = baker.prepare(models.DummyGenericForeignKeyModel) assert isinstance(dummy, models.DummyGenericForeignKeyModel) From b72197e1aa89b324f74707883448ab46f1fb5ee5 Mon Sep 17 00:00:00 2001 From: Marica Odagaki Date: Mon, 7 Feb 2022 11:42:26 -0800 Subject: [PATCH 3/5] Assert that ContentType attribute is populated --- tests/test_baker.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_baker.py b/tests/test_baker.py index a5a74f51..9bdb2e8c 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 @@ -592,6 +593,7 @@ 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) class TestHandlingContentTypeFieldNoQueries: @@ -602,6 +604,7 @@ def test_create_model_with_contenttype_field(self): ): dummy = baker.prepare(models.DummyGenericForeignKeyModel) assert isinstance(dummy, models.DummyGenericForeignKeyModel) + assert isinstance(dummy.content_type, ContentType) @pytest.mark.django_db From dd63cd80697eb07fe8b2915ce1df78fc7386ece5 Mon Sep 17 00:00:00 2001 From: Marica Odagaki Date: Mon, 7 Feb 2022 12:03:37 -0800 Subject: [PATCH 4/5] Add #290 to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f30ca6f9..41742ba5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Changed +- Fixed random generation of ContentType values when there is no database access [#290](https://github.com/model-bakers/model_bakery/pull/290) ### Removed From ff019e95cd7c98a52f73b2bdbee773b3c20db835 Mon Sep 17 00:00:00 2001 From: Marica Odagaki Date: Fri, 1 Apr 2022 17:04:18 -0800 Subject: [PATCH 5/5] Clear ContentType's internal cache to avoid flaky test failures --- tests/test_baker.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_baker.py b/tests/test_baker.py index 0f96edf0..e955f9d6 100644 --- a/tests/test_baker.py +++ b/tests/test_baker.py @@ -615,6 +615,11 @@ def test_create_model_with_contenttype_field(self): class TestHandlingContentTypeFieldNoQueries: def test_create_model_with_contenttype_field(self): + # 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",