0

I am currently writing tests and trying to assert if the expiry date is similar to what I expect it to me. Now I have the problem that the seconds are not similar and differ. E.g. >>> session_expiry_date = 1550744238.096939 while >>> expiry_date = 1550744238.087822. My best idea is to abandon the numbers after the dot and just compare if it's the same day.

I now struggle to convert the timestamps into dates (YEAR-MONTH-DAY) that I can compare it in a way with self.assertTrue. Can you explain to me how to convert these timestamps to comparable Year-Month-Day format?

def test_st_code_via_get(self):
    self.assertFalse(self.session_name in self.client.session)

    current_time = timezone.now().timestamp()
    expiry_date = timezone.now() + timezone.timedelta(
        days=settings.ST_ATTRIBUTION_WINDOW
    )

    self.client.get(self.path, {'a': 'LOREM'})
    session_expiry_date = self.client.session[self.session_name].get('expiry_date')

    self.assertTrue(expiry_date,  session_expiry_date)
Joey Coder
  • 3,199
  • 8
  • 28
  • 60
  • Possible duplicate of [Converting unix timestamp string to readable date](https://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date) – Federico klez Culloca Feb 14 '19 at 10:39
  • you probably meant `self.assertEqual`? Use `self.assertAlmostEqual()` (see [here](https://docs.python.org/3.7/library/unittest.html#unittest.TestCase.assertAlmostEqual)) – dirkgroten Feb 14 '19 at 11:11

0 Answers0