Opened 4 years ago

Closed 4 years ago

#31762 closed Bug (fixed)

Dabase creation backend should use base_manager to serialize database

Reported by: Eugene K Owned by: Hasan Ramezani
Component: Testing framework Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

models.py

class Service(Model):
    objects = CustomManagerThatFillterOutSomeRecords()

class CustomManagerThatFillterOutSomeRecords(Manager):
    def get_queryset(self):
        return super().get_queryset().exclude(pk=1)

tests.py

class TestService(TransactionTestCase):
    serialized_rollback = True

    def test_something(self):
        pass

Assume we have a migration that creates few records of Service.

from django.core.management import call_command
from django.db import migrations


def load_fixtures(*_, **__):
    call_command('loaddata', 'services.json')


class Migration(migrations.Migration):
    dependencies = []

    operations = [
        migrations.RunPython(
            load_fixtures,
            migrations.RunPython.noop,
        )
    ]

Then TestService will fail as serialize_db_to_string by default use _default_manager that is CustomManagerThatFillterOutSomeRecords.

Here is proposed fix: https://github.com/django/django/pull/13150

Change History (4)

comment:1 by Simon Charette, 4 years ago

Component: UncategorizedTesting framework
Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

That makes sense.

You'll need to add regression tests to your PR in [django/db/backends/base/creation.py https://github.com/django/django/blob/4d9cd89acbb944e10b9000092069ba8e3a855957/django/db/backends/base/creation.py] by defining a default manager that exclude rows django/db/backends/models.py, creating one such row, and ensure it's part of the string returned by serialize_db_to_string.

comment:2 by Hasan Ramezani, 4 years ago

Needs tests: unset
Owner: changed from nobody to Hasan Ramezani
Status: newassigned
Version 0, edited 4 years ago by Hasan Ramezani (next)

comment:3 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 50c3ac6:

Fixed #31762 -- Made reloading the database for tests use the base manager.

Co-authored-by: Eugene Kulak <kulak.eugene@…>

Note: See TracTickets for help on using tickets.
Back to Top