Skip to content

Commit

Permalink
Fix random generation of ContentType values when there is no database…
Browse files Browse the repository at this point in the history
… access (#290)

* Remove django_db mark from test case that checks behavior under no db access

* Allow generating ContentType values without db access

* Assert that ContentType attribute is populated

* Add #290 to changelog

* Clear ContentType's internal cache to avoid flaky test failures
  • Loading branch information
ento authored Apr 3, 2022
1 parent 42bc039 commit b5afc92
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion model_bakery/random_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
17 changes: 14 additions & 3 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b5afc92

Please sign in to comment.