Code

Opened 2 years ago

Closed 2 years ago

#18608 closed Bug (fixed)

Broken monkey-patching in humanize tests

Reported by: aaugustin Owned by: aaugustin
Component: contrib.humanize Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The tests for django.contrib.humanize monkey-patch the datetime module in django.utils.timesince as follows:

        timesince.datetime = new.module(b"mock_datetime")
        timesince.datetime.datetime = MockDateTime

Unfortunately, this means that isinstance(d, datetime.datetime) in django.utils.timesince will return False while the monkey-patch is in effect even though d is a real datetime.datetime.

The humanize tests rely on a fixed value: now = datetime.datetime(2012, 3, 9) which happens to be at 0h00. They pass by accident, really. Add an hour and a minute to now — this shouldn't matter at all — and they'll fail!

This is preventing me from writing correct tests for #18504, because datetime objects are treated as date objects and their time part is ignored. It's impossible to write tests for edge cases around DST changes with this bug.

I hope I don't need to explain how horrible this was to debug :(

Attachments (1)

18608.diff (1.9 KB) - added by claudep 2 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 2 years ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

Changed 2 years ago by claudep

comment:2 Changed 2 years ago by claudep

  • Has patch set

comment:3 Changed 2 years ago by aaugustin

  • Owner changed from nobody to aaugustin

This patch restricts the monkey-patching to the module that is currently tested, namely humanize. This isn't perfect but it's less insane.

I'll review the patch (at first sight, we should also remove orig_timesince_datetime) and push it.

comment:4 Changed 2 years ago by Aymeric Augustin <aymeric.augustin@…>

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

In [123362dd37a076987f265179fe16dcd2b6a16d12]:

Fixed #18608 -- Reduced monkey-patching in tests.

Thanks Claude Paroz for the patch.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.