Skip to content

Instantly share code, notes, and snippets.

@slinkp
Created November 29, 2010 20:25
Show Gist options
  • Save slinkp/720549 to your computer and use it in GitHub Desktop.
Save slinkp/720549 to your computer and use it in GitHub Desktop.
--- tests.py.orig 2010-11-29 13:41:10.538521131 -0500
+++ tests.py 2010-11-29 15:14:34.528520210 -0500
@@ -408,6 +408,18 @@
'function () { return 1; }',
media_url=media_url)
+
+ def assertFilenamesAlmostEqual(self, name1, name2):
+ # Occasionally we get a failure because the clock ticked to
+ # the next second after the file was rendered.
+ import re
+ name1, name2 = name1.strip(), name2.strip()
+ timestamp_re = re.compile(r'[^\.]\.([0-9]+)\..*')
+ t1 = int(timestamp_re.search(name1).group(1))
+ t2 = int(timestamp_re.search(name2).group(1))
+ self.assert_(t1 - t2 in (-1, 0, 1),
+ "Filenames %r and %r timestamps differ by more than 1" % (name1, name2))
+
def _test_staticfile_single(self, filename, code, name_prefix='', save_prefix='',
media_url=''):
@@ -424,14 +436,13 @@
bits = filename.rsplit('.', 1)
now = int(time.time())
new_filename = bits[0] + '.%s.' % now + bits[-1]
- self.assertEqual(rendered, media_url + name_prefix + new_filename)
-
+ self.assertFilenamesAlmostEqual(rendered, media_url + name_prefix + new_filename)
+
if save_prefix:
save_dir = os.path.join(os.path.join(settings.MEDIA_ROOT, save_prefix))
- self.assertTrue(os.path.basename(new_filename) in os.listdir(save_dir))
+ new_file_path = os.path.join(save_dir, os.path.basename(rendered))
+ self.assertTrue(os.path.exists(new_file_path))
# The content of the new file should be the same
- new_file = os.listdir(save_dir)[0]
- new_file_path = os.path.join(save_dir, new_file)
if sys.platform != "win32":
self.assertTrue(os.path.islink(new_file_path))
new_code = open(new_file_path).read()
@@ -454,7 +465,7 @@
# Run it again just to check that it still works
rendered = template.render(context).strip()
- self.assertEqual(rendered, media_url + name_prefix + new_filename)
+ self.assertFilenamesAlmostEqual(rendered, media_url + name_prefix + new_filename)
# pretend that time has passed and 10 seconds has lapsed then re-run the
# rendering again and depending on settings.DEBUG this is noticed
@@ -474,7 +485,7 @@
rendered = template.render(context).strip()
if settings.DEBUG:
new_filename = bits[0] + '.%s.' % (now + 10) + bits[1]
- self.assertEqual(rendered, media_url + name_prefix + new_filename)
+ self.assertFilenamesAlmostEqual(rendered, media_url + name_prefix + new_filename)
if settings.DEBUG:
@@ -594,14 +605,14 @@
bits = filename.split('.')
now = int(time.time())
new_filename = bits[0] + '.%s.' % now + bits[1]
- self.assertEqual(rendered, name_prefix + new_filename)
+ self.assertFilenamesAlmostEqual(rendered, name_prefix + new_filename)
if save_prefix:
save_dir = os.path.join(os.path.join(settings.MEDIA_ROOT, save_prefix))
- self.assertTrue(os.path.basename(new_filename) in os.listdir(save_dir))
+ new_file = os.path.join(save_dir, rendered.lstrip('/'))
+ self.assertTrue(os.path.exists(new_file)) #os.path.basename(new_file) in os.listdir(save_dir))
# The content of the new file should be smaller
- new_file = os.path.join(save_dir, os.listdir(save_dir)[0])
new_code = open(new_file).read()
self.assertTrue(len(new_code.strip()) < len(code.strip()))
else:
@@ -620,7 +631,7 @@
# Run it again just to check that it still works
rendered = template.render(context).strip()
- self.assertEqual(rendered, name_prefix + new_filename)
+ self.assertFilenamesAlmostEqual(rendered, name_prefix + new_filename)
# pretend that time has passed and 10 seconds has lapsed then re-run the
# rendering again and depending on settings.DEBUG this is noticed
@@ -641,7 +652,7 @@
rendered = template.render(context).strip()
if settings.DEBUG:
new_filename = bits[0] + '.%s.' % (now + 10) + bits[1]
- self.assertEqual(rendered, name_prefix + new_filename)
+ self.assertFilenamesAlmostEqual(rendered, name_prefix + new_filename)
if settings.DEBUG:
@@ -777,7 +788,7 @@
bits = expect_filename.split('.')
expect_filename = expect_filename[:-3]
expect_filename += '.%s%s' % (now, os.path.splitext(filenames[0])[1])
- self.assertEqual(rendered, name_prefix + expect_filename)
+ self.assertFilenamesAlmostEqual(rendered, name_prefix + expect_filename)
if save_prefix:
new_filenames_set = os.listdir(os.path.join(settings.MEDIA_ROOT, save_prefix))
@@ -799,7 +810,7 @@
template = Template(template_as_string)
context = Context()
rendered = template.render(context).strip()
- self.assertEqual(rendered, name_prefix + expect_filename)
+ self.assertFilenamesAlmostEqual(rendered, name_prefix + expect_filename)
from posix import stat_result
def fake_stat(arg):
@@ -822,7 +833,7 @@
# won't notice this until after you restart Django.
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment