def test_scripts_file_compiled(self): with contextlib.nested(*make_named_files(2)) as (js_file, temp_file): js_file.write('file') js_file.flush() js_read_handle = open(js_file.name, 'r') temp_read_handle = open(temp_file.name, 'r') def temp_opener(filename): if 'media/comp/js' in filename: return temp_file if 'media/test.js' in filename: return js_read_handle return None with contextlib.nested( django_exceptions(), django_settings({ 'COMPILER_ROOT': 'comp', 'MEDIA_ROOT': 'media', 'MEDIA_URL': '/media/' }), open_redirector(temp_opener), modified_time(), paths_exist('media/comp', 'media/comp/css', 'media/comp/js')): from compilation.templatetags.compiler import CompilerNode html = "<link type=\"text/javascript\" href=\"/media/test.js\" />" nodelist = MockNodelist(html) compiler_node = CompilerNode(nodelist) compiler_node.render(None) self.assertEqual(temp_read_handle.read(), 'file\n')
def test_read_django_url(self): with django_settings(): with make_named_files() as temp_file: temp_file.write('test') temp_file.flush() handler = self.handler('/test%s' % temp_file.name, 'url') self.assertEqual(handler.content, 'test')
def test_styles_compiled(self): with contextlib.nested(*make_named_files(2)) as (css_file, temp_file): css_file.write('file') css_file.flush() css_read_handle = open(css_file.name, 'r') temp_read_handle = open(temp_file.name, 'r') def temp_opener(filename): if 'media/comp/css' in filename: return temp_file if 'media/test.css' in filename: return css_read_handle return None with contextlib.nested( django_exceptions(), django_settings({ 'COMPILER_ROOT': 'comp', 'MEDIA_ROOT': 'media', 'MEDIA_URL': '/media/' }), open_redirector(temp_opener), modified_time(), paths_exist('media/comp', 'media/comp/css', 'media/comp/js')): from compilation.templatetags.compiler import CompilerNode html = """ <link type="text/css" href="/media/test.css" /> <style type="text/css">inline</style> """ nodelist = MockNodelist(html) compiler_node = CompilerNode(nodelist) compiler_node.render(None) self.assertSortedEqual( temp_read_handle.read().strip().split('\n'), ['file', 'inline'])
def test_lazy_read(self): with make_named_files() as temp_file: with open_exception(temp_file.name): temp_file.write('test') temp_file.flush() handler = self.handler(temp_file.name, 'file') #No exception yet self.assertRaises(TestException, getattr, handler, 'content') #Read when requested
def test_hash(self): import hashlib import os.path handler = self.handler('test', 'content') self.assertEqual(hashlib.sha1('test').hexdigest(), handler.hash) with make_named_files() as temp_file: with open_exception(temp_file.name): #Don't allow it to open the file for the hash temp_file.write('test') temp_file.flush() handler = self.handler(temp_file.name, 'file') self.assertEqual(hashlib.sha1(str(os.path.getmtime(temp_file.name))).hexdigest(), handler.hash)
def test_hash(self): import hashlib import os.path handler = self.handler('test', 'content') self.assertEqual(hashlib.sha1('test').hexdigest(), handler.hash) with make_named_files() as temp_file: with open_exception( temp_file.name ): #Don't allow it to open the file for the hash temp_file.write('test') temp_file.flush() handler = self.handler(temp_file.name, 'file') self.assertEqual( hashlib.sha1(str(os.path.getmtime( temp_file.name))).hexdigest(), handler.hash)
def test_scripts_file_compiled(self): with contextlib.nested(*make_named_files(2)) as (js_file, temp_file): js_file.write('file') js_file.flush() js_read_handle = open(js_file.name, 'r') temp_read_handle = open(temp_file.name, 'r') def temp_opener(filename): if 'media/comp/js' in filename: return temp_file if 'media/test.js' in filename: return js_read_handle return None with contextlib.nested(django_exceptions(), django_settings({'COMPILER_ROOT':'comp', 'MEDIA_ROOT':'media', 'MEDIA_URL':'/media/'}), open_redirector(temp_opener), modified_time(), paths_exist('media/comp', 'media/comp/css', 'media/comp/js')): from compilation.templatetags.compiler import CompilerNode html = "<link type=\"text/javascript\" href=\"/media/test.js\" />" nodelist = MockNodelist(html) compiler_node = CompilerNode(nodelist) compiler_node.render(None) self.assertEqual(temp_read_handle.read(), 'file\n')
def test_styles_compiled(self): with contextlib.nested(*make_named_files(2)) as (css_file, temp_file): css_file.write('file') css_file.flush() css_read_handle = open(css_file.name, 'r') temp_read_handle = open(temp_file.name, 'r') def temp_opener(filename): if 'media/comp/css' in filename: return temp_file if 'media/test.css' in filename: return css_read_handle return None with contextlib.nested(django_exceptions(), django_settings({'COMPILER_ROOT':'comp', 'MEDIA_ROOT':'media', 'MEDIA_URL':'/media/'}), open_redirector(temp_opener), modified_time(), paths_exist('media/comp', 'media/comp/css', 'media/comp/js')): from compilation.templatetags.compiler import CompilerNode html = """ <link type="text/css" href="/media/test.css" /> <style type="text/css">inline</style> """ nodelist = MockNodelist(html) compiler_node = CompilerNode(nodelist) compiler_node.render(None) self.assertSortedEqual(temp_read_handle.read().strip().split('\n'), ['file', 'inline'])
def test_read_file(self): with make_named_files() as temp_file: temp_file.write('test') temp_file.flush() handler = self.handler(temp_file.name, 'file') self.assertEqual(handler.content, 'test')