Example #1
0
class CompressorTestCase(BaseTestCase):
    
    def setUp(self):
        super(CompressorTestCase, self).setUp()
        
        
        self.ccssFile = os.path.join(settings.MEDIA_ROOT, u'css/three.css')
        self.css = dedent("""
        <link rel="stylesheet" href="/media/css/one.css" type="text/css">
        <style type="text/css">p { border:5px solid green;}</style>
        <link rel="stylesheet" href="/media/css/two.css" type="text/css">
        <link rel="stylesheet" href="/media/css/three.ccss" type="text/css">
        <style type="text/ccss">
        """)+ \
        '\n'.join((
        "small:\n  font-size:10px",
        '</style>\n<style type="ccss">',
        "h1:\n  font-weight:bold",
        "</style>"))
        self.css_hash = "1ff892c21b66"
        self.js_hash = "3f33b9146e12"
        self.cssNode = CssCompressor(self.css)

        self.js = """
        <script src="/media/js/one.js" type="text/javascript"></script>
        <script type="text/javascript">obj.value = "value";</script>
        """
        self.jsNode = JsCompressor(self.js)
    
    def test_get_filename(self):
        settings.COMPRESS_URL = '/static_test/'
        settings.COMPRESS_ROOT = os.path.join(os.path.dirname(__file__),'static_test_dir')
        path = os.path.join(settings.MEDIA_URL,'something.css')
        filename = self.cssNode.get_filename(path)
        self.assertEqual(filename,os.path.join(settings.MEDIA_ROOT,'something.css'))
        path = "http://something.com/static/something.css"
        self.assertRaises(UncompressableFileError, self.cssNode.get_filename,path)
        self.assertRaises(UncompressableFileError, self.jsNode.get_filename,path)
    
    def test_css_compiler_exists(self):
        settings.COMPILER_FORMATS = {
            '.ccss': {
                'binary_path': 'python ' + os.path.join(self.TEST_DIR,'clevrcss.py'),
                'arguments': '*.ccss'
            },
        }            
        self.assertRaises(Exception, self.cssNode.output)

    def test_css_split(self):
        out = [
            ('file', os.path.join(settings.MEDIA_ROOT, u'css/one.css'), '<link rel="stylesheet" href="/media/css/one.css" type="text/css" />'),
            ('hunk', u'p { border:5px solid green;}', '<style type="text/css">p { border:5px solid green;}</style>'),
            ('file', os.path.join(settings.MEDIA_ROOT, u'css/two.css'), '<link rel="stylesheet" href="/media/css/two.css" type="text/css" />'),
            ('file', self.ccssFile, '<link rel="stylesheet" href="/media/css/three.css" type="text/css" />'),
            ('hunk', 'small {\n  font-size: 10px;\n}', "<style type='text/css'>\nsmall {\n  font-size: 10px;\n}\n</style>"),
            ('hunk', 'h1 {\n  font-weight: bold;\n}', "<style type='text/css'>\nh1 {\n  font-weight: bold;\n}\n</style>")
        ]
        split = self.cssNode.split_contents()
        split = [(x[0], x[1], str(x[2])) for x in split]
        self.assertEqual(out, split)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_hunks(self):
        out = ['body { background:#990; }', 'p { border:5px solid green;}', 'body { color:#fff; }', 'a {\n  color: #5c4032;\n}', 'small {\n  font-size: 10px;\n}', 'h1 {\n  font-weight: bold;\n}']
        self.assertEqual(out, self.cssNode.hunks)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_output(self):
        out = u'body { background:#990; }\np { border:5px solid green;}\nbody { color:#fff; }\na {\n  color: #5c4032;\n}\nsmall {\n  font-size: 10px;\n}\nh1 {\n  font-weight: bold;\n}'
        self.assertEqual(out, self.cssNode.combined)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_mtimes(self):
        is_date = re.compile(r'^\d{10}\.\d$')
        for date in self.cssNode.mtimes:
            self.assert_(is_date.match(str(date)), "mtimes is returning something that doesn't look like a date")
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_return_if_off(self):
        settings.COMPRESS = False
        css = """
<link rel="stylesheet" href="/media/css/one.css" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css">
<link rel="stylesheet" href="/media/css/three.css" type="text/css">
<style type='text/css'>\nsmall {\n  font-size: 10px;\n}
</style>\n<style type='text/css'>\nh1 {\n  font-weight: bold;\n}\n</style>
"""
        self.assertEqual(dedent(css).strip(), dedent(self.cssNode.output()).strip())
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_hash(self):
        self.assertEqual(self.css_hash, self.cssNode.hash)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_return_if_on(self):
        output = u'<link rel="stylesheet" href="/media/CACHE/css/%s.css" type="text/css">' % self.css_hash
        self.assertEqual(output.strip(), self.cssNode.output().strip())
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_js_split(self):
        out = [('file', os.path.join(settings.MEDIA_ROOT, u'js/one.js'), '<script src="/media/js/one.js" type="text/javascript"></script>'),
         ('hunk', u'obj.value = "value";', '<script type="text/javascript">obj.value = "value";</script>')
         ]
        split = self.jsNode.split_contents()
        split = [(x[0], x[1], str(x[2])) for x in split]
        self.assertEqual(out, split)
        
    def test_js_hunks(self):
        out = ['obj = {};', u'obj.value = "value";']
        self.assertEqual(out, self.jsNode.hunks)

    def test_js_concat(self):
        out = u'obj = {};\nobj.value = "value";'
        self.assertEqual(out, self.jsNode.concat())

    def test_js_output(self):
        out = u'obj={};obj.value="value";'
        self.assertEqual(out, self.jsNode.combined)

    def test_js_return_if_off(self):
        settings.COMPRESS = False
        self.assertEqual(self.js, self.jsNode.output())

    def test_js_return_if_on(self):
        output = u'<script type="text/javascript" src="/media/CACHE/js/%s.js"></script>\n' % self.js_hash
        self.assertEqual(output, self.jsNode.output())
Example #2
0
class CompressorTestCase(TestCase):

    def setUp(self):
        settings.COMPRESS = True
        settings.COMPILER_FORMATS = {
            '.ccss': {
                'binary_path': 'python ' + os.path.join(django_settings.TEST_DIR,'clevercss.py'),
                'arguments': '*.ccss'
            },
            '.xcss': {
                'python':'clevercss.convert',
            },            
        }
        self.ccssFile = os.path.join(settings.MEDIA_ROOT, u'css/three.css')
        self.xcssFile = os.path.join(settings.MEDIA_ROOT, u'css/four.xcss')        
        self.css = dedent("""
        <link rel="stylesheet" href="/media/css/one.css" type="text/css">
        <style type="text/css">p { border:5px solid green;}</style>
        <link rel="stylesheet" href="/media/css/two.css" type="text/css">
        <link rel="stylesheet" href="/media/css/three.ccss" type="text/css">
        <link rel="stylesheet" href="/media/css/four.xcss" type="text/css">
        <style type="text/xcss">
        """)+ \
        '\n'.join((
        "small:\n  font-size:10px",
        '</style>\n<style type="xcss">',
        "h1:\n  font-weight:bold",
        "</style>"))

        self.cssNode = CssCompressor(self.css)

        self.js = """
        <script src="/media/js/one.js" type="text/javascript"></script>
        <script type="text/javascript">obj.value = "value";</script>
        """
        self.jsNode = JsCompressor(self.js)
    
    def test_get_filename(self):
        settings.COMPRESS_URL = '/static_test/'
        settings.COMPRESS_ROOT = os.path.join(os.path.dirname(__file__),'static_test_dir')
        path = os.path.join(settings.MEDIA_URL,'something.css')
        filename = self.cssNode.get_filename(path)
        self.assertEqual(filename,os.path.join(settings.MEDIA_ROOT,'something.css'))
        path = "http://something.com/static/something.css"
        self.assertRaises(UncompressableFileError, self.cssNode.get_filename,path)
        self.assertRaises(UncompressableFileError, self.jsNode.get_filename,path)
    
    def test_css_compiler_exists(self):
        settings.COMPILER_FORMATS = {
            '.ccss': {
                'binary_path': 'python ' + os.path.join(django_settings.TEST_DIR,'clevrcss.py'),
                'arguments': '*.ccss'
            },
        }            
        self.assertRaises(Exception, self.cssNode.output)

    def test_css_split(self):
        out = [
            {'elem': '<link rel="stylesheet" href="/media/css/one.css" type="text/css" />',
                'filename': os.path.join(settings.MEDIA_ROOT, u'css/one.css') },
            {'elem': '<style type="text/css">p { border:5px solid green;}</style>',
                'data': u'p { border:5px solid green;}' },
            {'elem': '<link rel="stylesheet" href="/media/css/two.css" type="text/css" />',
                'filename': os.path.join(settings.MEDIA_ROOT, u'css/two.css') },
            {'elem': '<link rel="stylesheet" href="/media/css/three.css" type="text/css" />',
                'filename': self.ccssFile, },
            {'elem': '<link rel="stylesheet" href="/media/css/four.xcss" type="text/css" />',
                'filename': self.xcssFile, 
                'data': u'p {\n  color: black;\n}' },
            {'elem': '<style type="text/xcss">\nsmall:\n  font-size:10px\n</style>',
                'data': u'small {\n  font-size: 10px;\n}' },
            {'elem': '<style type="xcss">\nh1:\n  font-weight:bold\n</style>',
                'data': u'h1 {\n  font-weight: bold;\n}' }
        ]
        split = self.cssNode.split_contents()
        for item in split:
            item['elem'] = str(item['elem'])        
        self.assertEqual(out, split)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_hunks(self):
        out = ['body { background:#990; }',
               'p { border:5px solid green;}', 
               'body { color:#fff; }', 
               'a {\n  color: #5c4032;\n}',
               'p {\n  color: black;\n}',
               'small {\n  font-size: 10px;\n}', 
               'h1 {\n  font-weight: bold;\n}'               
               ]
        self.assertEqual(out, self.cssNode.hunks)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_output(self):
        out = '\n'.join((
            'body { background:#990; }',
            'p { border:5px solid green;}',
            'body { color:#fff; }',
            'a {\n  color: #5c4032;\n}',
            'p {\n  color: black;\n}',
            'small {\n  font-size: 10px;\n}',
            'h1 {\n  font-weight: bold;\n}',
            ))
        self.assertEqual(out, self.cssNode.combined)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_mtimes(self):
        is_date = re.compile(r'^\d{10}\.\d$')
        for date in self.cssNode.mtimes:
            self.assert_(is_date.match(str(date)), "mtimes is returning something that doesn't look like a date")
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_return_if_off(self):
        settings.COMPRESS = False
        # TODO: The processor replaces all compileable formats with .css even when off
        # Not sure if that is what one would expect from turned off thing
        css_expected = self.css.strip().replace('three.ccss', 'three.css')
        self.assertEqual(css_expected, self.cssNode.output().strip())
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_cachekey(self):
        is_cachekey = re.compile(r'django_compressor\.\w{12}')
        self.assert_(is_cachekey.match(self.cssNode.cachekey), "cachekey is returning something that doesn't look like r'django_compressor\.\w{12}'")
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_hash(self):
        self.assertEqual('82250ce4120c', self.cssNode.hash)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_return_if_on(self):
        output = u'<link rel="stylesheet" href="/media/CACHE/css/82250ce4120c.css" type="text/css">'
        self.assertEqual(output.strip(), self.cssNode.output().strip())
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_custom_output(self):
        t = Template("""
        {% load compress %}
        {% compress css as main %}
        <style type="text/css">body {background: yellow;}</style>
        {% endcompress %}
        """)
        output = u'<link rel="stylesheet" href="/media/CACHE/css/main.css" type="text/css">'
        self.assertEqual(output.strip(), t.render(Context({})).strip())
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_js_split(self):
        out = [
            {'filename': os.path.join(settings.MEDIA_ROOT, u'js/one.js'), 
             'elem':'<script src="/media/js/one.js" type="text/javascript"></script>'},
            {'data': u'obj.value = "value";', 
             'elem': '<script type="text/javascript">obj.value = "value";</script>'},
         ]
        split = self.jsNode.split_contents()
        for item in split:
            item['elem'] = str(item['elem'])
        self.assertEqual(out, split)
        
    def test_js_hunks(self):
        out = ['obj = {};', u'obj.value = "value";']
        self.assertEqual(out, self.jsNode.hunks)

    def test_js_concat(self):
        out = u'obj = {};\nobj.value = "value";'
        self.assertEqual(out, self.jsNode.concat())

    def test_js_output(self):
        out = u'obj={};obj.value="value";'
        self.assertEqual(out, self.jsNode.combined)

    def test_js_return_if_off(self):
        settings.COMPRESS = False
        self.assertEqual(self.js, self.jsNode.output())

    def test_js_return_if_on(self):
        output = u'<script type="text/javascript" src="/media/CACHE/js/3f33b9146e12.js"></script>\n'
        self.assertEqual(output, self.jsNode.output())