Opened 3 years ago

Closed 3 years ago

#17882 closed Bug (fixed)

regressiontests.admin_filters fails with pyscopg2 when USE_TZ is False

Reported by: brodie Owned by: aaugustin
Component: Database layer (models, ORM) Version: master
Severity: Release blocker Keywords:
Cc: aaugustin, jezdez Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

With these settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangotest',
        'USER': 'djangotest',
        'PASSWORD': 'djangotest',
    },
    'other': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangotestother',
        'USER': 'djangotestother',
        'PASSWORD': 'djangotestother',
    }
}

SECRET_KEY = "django_tests_secret_key"

Running the admin_filters test from trunk (at [17682]) fails when testing time zone support:

Creating test database for alias 'default'...
Creating test database for alias 'other'...
....E...........
======================================================================
ERROR: test_datefieldlistfilter_with_time_zone_support (regressiontests.admin_filters.tests.ListFiltersTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "django/django/test/utils.py", line 214, in inner
    return test_func(*args, **kwargs)
  File "django/tests/regressiontests/admin_filters/tests.py", line 227, in test_datefieldlistfilter_with_time_zone_support
    self.test_datefieldlistfilter()
  File "django/tests/regressiontests/admin_filters/tests.py", line 146, in test_datefieldlistfilter
    changelist = self.get_changelist(request, Book, modeladmin)
  File "django/tests/regressiontests/admin_filters/tests.py", line 140, in get_changelist
    modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin)
  File "django/django/contrib/admin/views/main.py", line 70, in __init__
    self.query_set = self.get_query_set(request)
  File "django/django/contrib/admin/views/main.py", line 322, in get_query_set
    raise IncorrectLookupParameters(e)
IncorrectLookupParameters: database connection isn't set to UTC

----------------------------------------------------------------------
Ran 16 tests in 0.628s

FAILED (errors=1)
Destroying test database for alias 'default'...
Destroying test database for alias 'other'...

Adding USE_TZ = True to the settings file makes the test pass. I suspect the test is failing because, despite it using @override_settings(USE_TZ=True), the current database connection isn't set up to use time zones.

Attachments (3)

17882-quick-n-dirty-patch.diff (1.2 KB) - added by aaugustin 3 years ago.
17882-better-fix-with-setting-changed-signal.diff (764 bytes) - added by aaugustin 3 years ago.
17882-second-take.patch (4.1 KB) - added by aaugustin 3 years ago.

Download all attachments as: .zip

Change History (15)

comment:1 Changed 3 years ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Severity changed from Normal to Release blocker
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 3 years ago by julien

This problem occurs after the changes in [17670].

comment:3 Changed 3 years ago by aaugustin

  • Owner changed from nobody to aaugustin

comment:4 Changed 3 years ago by aaugustin

Surprisingly, this failure doesn't show up on the CI server.

However, I can reproduce it locally. It's probably just a matter of closing the connection so it gets reestablished with the proper "SET TIME ZONE" command.

Changed 3 years ago by aaugustin

comment:5 Changed 3 years ago by aaugustin

  • Has patch set

That's a quick fix. I'm now looking at what it would take to use the settings_changed signal.

comment:6 Changed 3 years ago by aaugustin

  • Triage Stage changed from Accepted to Design decision needed

I have a preference for the second patch, but I'm not sure how we want to use setting_changed in 1.4 exactly -- see #17787. DDN until this is clarified.

comment:7 Changed 3 years ago by jezdez

  • Triage Stage changed from Design decision needed to Ready for checkin

I think this ticket is clearly a great use case for the signal, let's use the second patch.

comment:8 Changed 3 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

In [17699]:

Fixed #17882 -- Reopened the database connection when a test changes time zone settings. Thanks brodie for the report.

comment:9 Changed 3 years ago by aaugustin

  • Patch needs improvement set
  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Ready for checkin to Accepted

comment:10 Changed 3 years ago by aaugustin

I can reproduce the failure locally under MySQL but not under PostgreSQL. I don't understand why yet...

(django-dev)myk@mYk tests % PYTHONPATH=.. python2.7 runtests.py --settings=test_mysql admin_filters cache timezones
Creating test database for alias 'default'...
Creating test database for alias 'other'...
....F......................................................sssssss..s..............s.....s....................................................................................................................................................................................................................ssssssssssssssssssssssssssssssssss
======================================================================
FAIL: test_datefieldlistfilter_with_time_zone_support (regressiontests.admin_filters.tests.ListFiltersTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/myk/Documents/dev/django-trunk/django/test/utils.py", line 214, in inner
    return test_func(*args, **kwargs)
  File "/Users/myk/Documents/dev/django-trunk/tests/regressiontests/admin_filters/tests.py", line 227, in test_datefieldlistfilter_with_time_zone_support
    self.test_datefieldlistfilter()
  File "/Users/myk/Documents/dev/django-trunk/tests/regressiontests/admin_filters/tests.py", line 154, in test_datefieldlistfilter
    self.assertEqual(list(queryset), [self.django_book, self.djangonaut_book])
AssertionError: Lists differ: [] != [<Book: The Django Book>, <Boo...

Second list contains 2 additional elements.
First extra element 0:
The Django Book

- []
+ [<Book: The Django Book>, <Book: Djangonaut: an art of living>]

----------------------------------------------------------------------
Ran 336 tests in 56.977s

FAILED (failures=1, skipped=44)
Destroying test database for alias 'default'...
Destroying test database for alias 'other'...
PYTHONPATH=.. python2.7 runtests.py --settings=test_mysql admin_filters cache  12,88s user 1,65s system 22% cpu 1:03,86 total
(django-dev)myk@mYk tests % PYTHONPATH=.. python2.7 runtests.py --settings=test_postgresql admin_filters cache timezones
Creating test database for alias 'default'...
Got an error creating the test database: database "test_django" already exists

Type 'yes' if you would like to try deleting the test database 'test_django', or 'no' to cancel: yes
Destroying old test database 'default'...
.............................................................s.s.ss..s..............s.....s...................................................................................................................................................................................................................ssssssssssssssssssssssssssssssssss
----------------------------------------------------------------------
Ran 336 tests in 72.750s

OK (skipped=41)
Destroying test database for alias 'default'...
PYTHONPATH=.. python2.7 runtests.py --settings=test_postgresql admin_filters   22,06s user 1,99s system 30% cpu 1:19,27 total

Changed 3 years ago by aaugustin

comment:11 Changed 3 years ago by aaugustin

This improved version is unfortunately blocked by #17895.

comment:12 Changed 3 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from reopened to closed

In [17709]:

Fixed #17882 (again) -- Updated the database connections' time zone when time-zone-related settings are changed in tests.

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