1

I want to write a test, that takes a time from a saved object recipient.expiry_date and checks that the date is 30 days in the future.

This is what I have tried:

days_to_match = timezone.now() + timedelta(days=30)

self.assertEqual(recipient.expiry_date, days_to_match)

Because it also contains the time this won't match.

How can this be done?

Please note that recipient.expiry_date is set in my model using timezone.now()so comparison on dates from python seemed to error.

Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
Prometheus
  • 32,405
  • 54
  • 166
  • 302

2 Answers2

3

As you can see in this SO answer, you can use the date() to compare only the date and not the time. Or you can put the time data = 0.

self.assertEqual(recipient.expiry_date.date(), days_to_match.date())
Community
  • 1
  • 1
Nil
  • 2,345
  • 1
  • 26
  • 33
1

You can just look at the date portions:

self.assertEqual(recipient.expiry_date.year, days_to_match.year)
self.assertEqual(recipient.expiry_date.month, days_to_match.month)
self.assertEqual(recipient.expiry_date.day, days_to_match.day)
Markku K.
  • 3,840
  • 19
  • 20