<s> import os <EOL> import re <EOL> from copy import copy <EOL> from textwrap import dedent <EOL> from BeautifulSoup import BeautifulSoup <EOL> from django . template import Template , Context <EOL> from django . test import TestCase <EOL> from compressor import CssCompressor , JsCompressor , UncompressableFileError <EOL> from compressor . conf import settings <EOL> from compressor . utils import get_file_hash <EOL> class BaseTestCase ( TestCase ) : <EOL> def tearDown ( self ) : <EOL> for k , v in self . old_settings . iteritems ( ) : <EOL> setattr ( settings , k , v ) <EOL> def setUp ( self ) : <EOL> self . old_settings = copy ( settings . __dict__ ) <EOL> self . TEST_DIR = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , "<STR_LIT:..>" , "<STR_LIT>" ) <EOL> settings . MEDIA_ROOT = os . path . join ( self . TEST_DIR , '<STR_LIT>' ) <EOL> settings . MEDIA_URL = '<STR_LIT>' <EOL> settings . COMPRESS_CSS_FILTERS = [ '<STR_LIT>' ] <EOL> settings . COMPRESS_JS_FILTERS = [ '<STR_LIT>' ] <EOL> settings . COMPRESS = True <EOL> settings . COMPILER_FORMATS = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' + os . path . join ( self . TEST_DIR , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> } <EOL> class CompressorTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( CompressorTestCase , self ) . setUp ( ) <EOL> self . ccssFile = os . path . join ( settings . MEDIA_ROOT , u'<STR_LIT>' ) <EOL> self . css = dedent ( """<STR_LIT>""" ) + '<STR_LIT:\n>' . join ( ( <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) <EOL> self . css_hash = "<STR_LIT>" <EOL> self . js_hash = "<STR_LIT>" <EOL> self . cssNode = CssCompressor ( self . css ) <EOL> self . js = """<STR_LIT>""" <EOL> self . jsNode = JsCompressor ( self . js ) <EOL> def test_get_filename ( self ) : <EOL> settings . COMPRESS_URL = '<STR_LIT>' <EOL> settings . COMPRESS_ROOT = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> path = os . path . join ( settings . MEDIA_URL , '<STR_LIT>' ) <EOL> filename = self . cssNode . get_filename ( path ) <EOL> self . assertEqual ( filename , os . path . join ( settings . MEDIA_ROOT , '<STR_LIT>' ) ) <EOL> path = "<STR_LIT>" <EOL> self . assertRaises ( UncompressableFileError , self . cssNode . get_filename , path ) <EOL> self . assertRaises ( UncompressableFileError , self . jsNode . get_filename , path ) <EOL> def test_css_compiler_exists ( self ) : <EOL> settings . COMPILER_FORMATS = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' + os . path . join ( self . TEST_DIR , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> } <EOL> self . assertRaises ( Exception , self . cssNode . output ) <EOL> def test_css_split ( self ) : <EOL> out = [ <EOL> ( '<STR_LIT:file>' , os . path . join ( settings . MEDIA_ROOT , u'<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , u'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:file>' , os . path . join ( settings . MEDIA_ROOT , u'<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:file>' , self . ccssFile , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> ] <EOL> split = self . cssNode . split_contents ( ) <EOL> split = [ ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] , str ( x [ <NUM_LIT:2> ] ) ) for x in split ] <EOL> self . assertEqual ( out , split ) <EOL> if os . path . exists ( self . ccssFile ) : <EOL> os . remove ( self . ccssFile ) <EOL> def test_css_hunks ( self ) : <EOL> out = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( out , self . cssNode . hunks ) <EOL> if os . path . exists ( self . ccssFile ) : <EOL> os . remove ( self . ccssFile ) <EOL> def test_css_output ( self ) : <EOL> out = u'<STR_LIT>' <EOL> self . assertEqual ( out , self . cssNode . combined ) <EOL> if os . path . exists ( self . ccssFile ) : <EOL> os . remove ( self . ccssFile ) <EOL> def test_css_mtimes ( self ) : <EOL> is_date = re . compile ( r'<STR_LIT>' ) <EOL> for date in self . cssNode . mtimes : <EOL> self . assert_ ( is_date . match ( str ( date ) ) , "<STR_LIT>" ) <EOL> if os . path . exists ( self . ccssFile ) : <EOL> os . remove ( self . ccssFile ) <EOL> def test_css_return_if_off ( self ) : <EOL> settings . COMPRESS = False <EOL> css = """<STR_LIT>""" <EOL> self . assertEqual ( dedent ( css ) . strip ( ) , dedent ( self . cssNode . output ( ) ) . strip ( ) ) <EOL> if os . path . exists ( self . ccssFile ) : <EOL> os . remove ( self . ccssFile ) <EOL> def test_css_hash ( self ) : <EOL> self . assertEqual ( self . css_hash , self . cssNode . hash ) <EOL> if os . path . exists ( self . ccssFile ) : <EOL> os . remove ( self . ccssFile ) <EOL> def test_css_return_if_on ( self ) : <EOL> output = u'<STR_LIT>' % self . css_hash <EOL> self . assertEqual ( output . strip ( ) , self . cssNode . output ( ) . strip ( ) ) <EOL> if os . path . exists ( self . ccssFile ) : <EOL> os . remove ( self . ccssFile ) <EOL> def test_js_split ( self ) : <EOL> out = [ ( '<STR_LIT:file>' , os . path . join ( settings . MEDIA_ROOT , u'<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , u'<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> split = self . jsNode . split_contents ( ) <EOL> split = [ ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] , str ( x [ <NUM_LIT:2> ] ) ) for x in split ] <EOL> self . assertEqual ( out , split ) <EOL> def test_js_hunks ( self ) : <EOL> out = [ '<STR_LIT>' , u'<STR_LIT>' ] <EOL> self . assertEqual ( out , self . jsNode . hunks ) <EOL> def test_js_concat ( self ) : <EOL> out = u'<STR_LIT>' <EOL> self . assertEqual ( out , self . jsNode . concat ( ) ) <EOL> def test_js_output ( self ) : <EOL> out = u'<STR_LIT>' <EOL> self . assertEqual ( out , self . jsNode . combined ) <EOL> def test_js_return_if_off ( self ) : <EOL> settings . COMPRESS = False <EOL> self . assertEqual ( self . js , self . jsNode . output ( ) ) <EOL> def test_js_return_if_on ( self ) : <EOL> output = u'<STR_LIT>' % self . js_hash <EOL> self . assertEqual ( output , self . jsNode . output ( ) ) <EOL> class CssAbsolutizingTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( CssAbsolutizingTestCase , self ) . setUp ( ) <EOL> settings . COMPRESS = True <EOL> settings . MEDIA_URL = '<STR_LIT>' <EOL> self . css = """<STR_LIT>""" <EOL> self . url1_hash = get_file_hash ( u'<STR_LIT>' ) <EOL> self . url2_hash = get_file_hash ( u'<STR_LIT>' ) <EOL> self . cssNode = CssCompressor ( self . css ) <EOL> def test_css_absolute_filter ( self ) : <EOL> from compressor . filters . css_default import CssAbsoluteFilter <EOL> filename = os . path . join ( settings . MEDIA_ROOT , '<STR_LIT>' ) <EOL> content = "<STR_LIT>" <EOL> output = "<STR_LIT>" % settings . MEDIA_URL <EOL> filter = CssAbsoluteFilter ( content ) <EOL> self . assertEqual ( output , filter . input ( filename = filename ) ) <EOL> self . assertEqual ( output , filter . input ( filename = filename , media_url = settings . MEDIA_URL ) ) <EOL> settings . MEDIA_URL = '<STR_LIT>' <EOL> filename = os . path . join ( settings . MEDIA_ROOT , '<STR_LIT>' ) <EOL> output = "<STR_LIT>" % settings . MEDIA_URL <EOL> self . assertEqual ( output , filter . input ( filename = filename , media_url = settings . MEDIA_URL ) ) <EOL> settings . MEDIA_URL = '<STR_LIT>' <EOL> output = "<STR_LIT>" % settings . MEDIA_URL <EOL> self . assertEqual ( output , filter . input ( filename = filename , media_url = settings . MEDIA_URL ) ) <EOL> def test_css_hunks ( self ) : <EOL> out = [ "<STR_LIT>" % ( ( self . url1_hash , ) * <NUM_LIT:4> ) , <EOL> "<STR_LIT>" % ( ( self . url2_hash , ) * <NUM_LIT:4> ) <EOL> ] <EOL> self . assertEqual ( out , self . cssNode . hunks ) <EOL> class CssMediaTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( CssMediaTestCase , self ) . setUp ( ) <EOL> self . css = """<STR_LIT>""" <EOL> self . cssNode = CssCompressor ( self . css ) <EOL> def test_css_output ( self ) : <EOL> out = u'<STR_LIT>' <EOL> self . assertEqual ( out , self . cssNode . combined ) <EOL> class TemplatetagTestCase ( BaseTestCase ) : <EOL> def render ( self , template_string , context_dict = None ) : <EOL> """<STR_LIT>""" <EOL> if context_dict is None : <EOL> context_dict = { } <EOL> c = Context ( context_dict ) <EOL> t = Template ( template_string ) <EOL> return t . render ( c ) . strip ( ) <EOL> def test_css_tag ( self ) : <EOL> template = u"""<STR_LIT>""" <EOL> context = { '<STR_LIT>' : settings . MEDIA_URL } <EOL> out = u'<STR_LIT>' <EOL> self . assertEqual ( out , self . render ( template , context ) ) <EOL> def test_js_tag ( self ) : <EOL> template = u"""<STR_LIT>""" <EOL> context = { '<STR_LIT>' : settings . MEDIA_URL } <EOL> out = u'<STR_LIT>' <EOL> self . assertEqual ( out , self . render ( template , context ) ) </s>
