test_gzip_cache.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. '''Core plugins unit tests'''
  3. import os
  4. import tempfile
  5. import unittest
  6. import time
  7. from contextlib import contextmanager
  8. from tempfile import mkdtemp
  9. from shutil import rmtree
  10. from hashlib import md5
  11. import gzip_cache
  12. @contextmanager
  13. def temporary_folder():
  14. """creates a temporary folder, return it and delete it afterwards.
  15. This allows to do something like this in tests:
  16. >>> with temporary_folder() as d:
  17. # do whatever you want
  18. """
  19. tempdir = mkdtemp()
  20. try:
  21. yield tempdir
  22. finally:
  23. rmtree(tempdir)
  24. class TestGzipCache(unittest.TestCase):
  25. def test_should_compress(self):
  26. # Some filetypes should compress and others shouldn't.
  27. self.assertTrue(gzip_cache.should_compress('foo.html'))
  28. self.assertTrue(gzip_cache.should_compress('bar.css'))
  29. self.assertTrue(gzip_cache.should_compress('baz.js'))
  30. self.assertTrue(gzip_cache.should_compress('foo.txt'))
  31. self.assertFalse(gzip_cache.should_compress('foo.gz'))
  32. self.assertFalse(gzip_cache.should_compress('bar.png'))
  33. self.assertFalse(gzip_cache.should_compress('baz.mp3'))
  34. self.assertFalse(gzip_cache.should_compress('foo.mov'))
  35. def test_should_overwrite(self):
  36. # Default to false if GZIP_CACHE_OVERWRITE is not set
  37. settings = { }
  38. self.assertFalse(gzip_cache.should_overwrite(settings))
  39. settings = { 'GZIP_CACHE_OVERWRITE': False }
  40. self.assertFalse(gzip_cache.should_overwrite(settings))
  41. settings = { 'GZIP_CACHE_OVERWRITE': True }
  42. self.assertTrue(gzip_cache.should_overwrite(settings))
  43. def test_creates_gzip_file(self):
  44. # A file matching the input filename with a .gz extension is created.
  45. # The plugin walks over the output content after the finalized signal
  46. # so it is safe to assume that the file exists (otherwise walk would
  47. # not report it). Therefore, create a dummy file to use.
  48. with temporary_folder() as tempdir:
  49. _, a_html_filename = tempfile.mkstemp(suffix='.html', dir=tempdir)
  50. gzip_cache.create_gzip_file(a_html_filename, False)
  51. self.assertTrue(os.path.exists(a_html_filename + '.gz'))
  52. def test_creates_same_gzip_file(self):
  53. # Should create the same gzip file from the same contents.
  54. # gzip will create a slightly different file because it includes
  55. # a timestamp in the compressed file by default. This can cause
  56. # problems for some caching strategies.
  57. with temporary_folder() as tempdir:
  58. _, a_html_filename = tempfile.mkstemp(suffix='.html', dir=tempdir)
  59. a_gz_filename = a_html_filename + '.gz'
  60. gzip_cache.create_gzip_file(a_html_filename, False)
  61. gzip_hash = get_md5(a_gz_filename)
  62. time.sleep(1)
  63. gzip_cache.create_gzip_file(a_html_filename, False)
  64. self.assertEqual(gzip_hash, get_md5(a_gz_filename))
  65. def test_overwrites_gzip_file(self):
  66. # A file matching the input filename with a .gz extension is not created.
  67. # The plugin walks over the output content after the finalized signal
  68. # so it is safe to assume that the file exists (otherwise walk would
  69. # not report it). Therefore, create a dummy file to use.
  70. with temporary_folder() as tempdir:
  71. _, a_html_filename = tempfile.mkstemp(suffix='.html', dir=tempdir)
  72. gzip_cache.create_gzip_file(a_html_filename, True)
  73. self.assertFalse(os.path.exists(a_html_filename + '.gz'))
  74. def get_md5(filepath):
  75. with open(filepath, 'rb') as fh:
  76. return md5(fh.read()).hexdigest()