summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2025-05-22 22:23:02 +0100
committernessita <124304+nessita@users.noreply.github.com>2025-05-23 11:23:47 -0300
commitb373721af0e5c3de0986977ac07e3ad55061ecbe (patch)
treebf8cad3833af18ed75d1a189808441267f7f2375
parentc2615a050036eda0bca090c707191076220cee9f (diff)
Fixed flakiness in file_storage.tests.CustomStorageTests.test_file_get_accessed_time.
Two separate calls to look up access time can result in sub-second differences which cause the test to fail. Also made the equivalent tests for ctime and mtime have the same changes to ensure that they won't flake in the same way in future.
-rw-r--r--tests/file_storage/tests.py48
1 files changed, 27 insertions, 21 deletions
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index ff09f603d8..fde07a955c 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -186,17 +186,19 @@ class FileStorageTests(SimpleTestCase):
f = ContentFile("custom contents")
f_name = self.storage.save("test.file", f)
self.addCleanup(self.storage.delete, f_name)
+
+ path = self.storage.path(f_name)
atime = self.storage.get_accessed_time(f_name)
- self.assertEqual(
+ self.assertAlmostEqual(
atime,
- datetime.datetime.fromtimestamp(
- os.path.getatime(self.storage.path(f_name))
- ),
+ datetime.datetime.fromtimestamp(os.path.getatime(path)),
+ delta=datetime.timedelta(seconds=1),
)
- self.assertLess(
- timezone.now() - self.storage.get_accessed_time(f_name),
- datetime.timedelta(seconds=2),
+ self.assertAlmostEqual(
+ atime,
+ timezone.now(),
+ delta=datetime.timedelta(seconds=1),
)
@requires_tz_support
@@ -212,17 +214,19 @@ class FileStorageTests(SimpleTestCase):
f = ContentFile("custom contents")
f_name = self.storage.save("test.file", f)
self.addCleanup(self.storage.delete, f_name)
+
+ path = self.storage.path(f_name)
ctime = self.storage.get_created_time(f_name)
- self.assertEqual(
+ self.assertAlmostEqual(
ctime,
- datetime.datetime.fromtimestamp(
- os.path.getctime(self.storage.path(f_name))
- ),
+ datetime.datetime.fromtimestamp(os.path.getctime(path)),
+ delta=datetime.timedelta(seconds=1),
)
- self.assertLess(
- timezone.now() - self.storage.get_created_time(f_name),
- datetime.timedelta(seconds=2),
+ self.assertAlmostEqual(
+ ctime,
+ timezone.now(),
+ delta=datetime.timedelta(seconds=1),
)
@requires_tz_support
@@ -238,17 +242,19 @@ class FileStorageTests(SimpleTestCase):
f = ContentFile("custom contents")
f_name = self.storage.save("test.file", f)
self.addCleanup(self.storage.delete, f_name)
+
+ path = self.storage.path(f_name)
mtime = self.storage.get_modified_time(f_name)
- self.assertEqual(
+ self.assertAlmostEqual(
mtime,
- datetime.datetime.fromtimestamp(
- os.path.getmtime(self.storage.path(f_name))
- ),
+ datetime.datetime.fromtimestamp(os.path.getmtime(path)),
+ delta=datetime.timedelta(seconds=1),
)
- self.assertLess(
- timezone.now() - self.storage.get_modified_time(f_name),
- datetime.timedelta(seconds=2),
+ self.assertAlmostEqual(
+ mtime,
+ timezone.now(),
+ delta=datetime.timedelta(seconds=1),
)
@requires_tz_support