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