Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix random generation of ContentType values when there is no database access #290

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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