test_gzip_cache.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. '''Core plugins unit tests'''
  3. import os
  4. import tempfile
  5. import unittest
  6. from contextlib import contextmanager
  7. from tempfile import mkdtemp
  8. from shutil import rmtree
  9. import gzip_cache
  10. @contextmanager
  11. def temporary_folder():
  12. """creates a temporary folder, return it and delete it afterwards.
  13. This allows to do something like this in tests:
  14. >>> with temporary_folder() as d:
  15. # do whatever you want
  16. """
  17. tempdir = mkdtemp()
  18. try:
  19. yield tempdir
  20. finally:
  21. rmtree(tempdir)
  22. class TestGzipCache(unittest.TestCase):
  23. def test_should_compress(self):
  24. # Some filetypes should compress and others shouldn't.
  25. self.assertTrue(gzip_cache.should_compress('foo.html'))
  26. self.assertTrue(gzip_cache.should_compress('bar.css'))
  27. self.assertTrue(gzip_cache.should_compress('baz.js'))
  28. self.assertTrue(gzip_cache.should_compress('foo.txt'))
  29. self.assertFalse(gzip_cache.should_compress('foo.gz'))
  30. self.assertFalse(gzip_cache.should_compress('bar.png'))
  31. self.assertFalse(gzip_cache.should_compress('baz.mp3'))
  32. self.assertFalse(gzip_cache.should_compress('foo.mov'))
  33. def test_creates_gzip_file(self):
  34. # A file matching the input filename with a .gz extension is created.
  35. # The plugin walks over the output content after the finalized signal
  36. # so it is safe to assume that the file exists (otherwise walk would
  37. # not report it). Therefore, create a dummy file to use.
  38. with temporary_folder() as tempdir:
  39. _, a_html_filename = tempfile.mkstemp(suffix='.html', dir=tempdir)
  40. gzip_cache.create_gzip_file(a_html_filename)
  41. self.assertTrue(os.path.exists(a_html_filename + '.gz'))