def test_rendering_base_url(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) assert needed.render() == '''\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /> <script type="text/javascript" src="/fanstatic/foo/a.js"></script> <script type="text/javascript" src="/fanstatic/foo/c.js"></script>''' needed = NeededResources(base_url='http://localhost/static') needed.need(y1) assert needed.render() == '''\ <link rel="stylesheet" type="text/css" href="http://localhost/static/fanstatic/foo/b.css" /> <script type="text/javascript" src="http://localhost/static/fanstatic/foo/a.js"></script> <script type="text/javascript" src="http://localhost/static/fanstatic/foo/c.js"></script>''' # The base_url has been set. assert needed.has_base_url() needed.set_base_url('foo') # The base_url can only be set once. assert needed._base_url == 'http://localhost/static'
def test_slot_depends_incorrect(): needed = NeededResources() lib = Library('lib', '') c = Resource(lib, 'c.js') slot = Slot(lib, '.js', depends=[c]) a = Resource(lib, 'a.js', depends=[slot]) d = Resource(lib, 'd.js') b = Resource(lib, 'b.js', depends=[d]) needed.need(a, {slot: b}) with pytest.raises(SlotError): needed.render()
def test_custom_renderer_for_resource(): foo = Library('foo', '') from fanstatic.core import render_print_css a = Resource(foo, 'printstylesheet.css', renderer=render_print_css) needed = NeededResources() needed.need(a) assert needed.render() == """\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/printstylesheet.css" media="print" />""" def render_unknown(url): return '<unknown href="%s"/>' % url b = Resource(foo, 'nothing.unknown', renderer=render_unknown) needed.need(b) assert needed.render() == """\
def test_render_bundle(): foo = Library('foo', '') x1 = Resource(foo, 'a.css') x2 = Resource(foo, 'b.css') x3 = Resource(foo, 'c.css', dont_bundle=True) needed = NeededResources(resources=[x1, x2, x3]) assert needed.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' needed = NeededResources(resources=[x1, x2, x3], bundle=True) assert needed.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' x4 = Resource(foo, 'subdir/subdir/x4.css') x5 = Resource(foo, 'subdir/subdir/x5.css') needed = NeededResources(resources=[x1, x2, x4, x5], bundle=True) assert needed.render() == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
def test_empty_base_url_and_publisher_signature(): ''' When the base_url is not set and the publisher_signature is an empty string, render a URL without them. ''' foo = Library('foo', '') x1 = Resource(foo, 'a.js') needed = NeededResources(publisher_signature='') needed.need(x1) assert needed.render() == '''\
def test_rendering(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) assert needed.render() == '''\
def test_rendering_base_url_assign(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) needed.set_base_url('http://localhost/static') assert needed.render() == '''\
def test_resource_subclass_render(): foo = Library('foo', '') class MyResource(Resource): def render(self, library_url): return '<myresource reference="%s/%s"/>' % (library_url, self.relpath) a = MyResource(foo, 'printstylesheet.css') needed = NeededResources() needed.need(a) assert needed.render() == """\
def test_slot_minified(): needed = NeededResources(minified=True) lib = Library('lib', '') slot = Slot(lib, '.js') a = Resource(lib, 'a.js', depends=[slot]) b = Resource(lib, 'b.js', minified='b-min.js') needed.need(a, {slot: b}) assert needed.render() == '''\
def test_render_bundle(): foo = Library('foo', '') x1 = Resource(foo, 'a.css') x2 = Resource(foo, 'b.css') x3 = Resource(foo, 'c.css', dont_bundle=True) needed = NeededResources(resources=[x1, x2, x3]) assert needed.render( ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' needed = NeededResources(resources=[x1, x2, x3], bundle=True) assert needed.render( ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" /> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/c.css" />''' x4 = Resource(foo, 'subdir/subdir/x4.css') x5 = Resource(foo, 'subdir/subdir/x5.css') needed = NeededResources(resources=[x1, x2, x4, x5], bundle=True) assert needed.render( ) == '''<link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />
def test_slot_depends_subset(): needed = NeededResources() lib = Library('lib', '') c = Resource(lib, 'c.js') slot = Slot(lib, '.js', depends=[c]) a = Resource(lib, 'a.js', depends=[slot]) b = Resource(lib, 'b.js', depends=[]) needed.need(a, {slot: b}) assert needed.render() == '''\
def test_render_filled_slots(): needed = NeededResources() lib = Library('lib', '') slot = Slot(lib, '.js') a = Resource(lib, 'a.js', depends=[slot]) b = Resource(lib, 'b.js') needed.need(a, {slot: b}) assert needed.render() == '''\
def test_registered_inclusion_renderers_in_order(): foo = Library('foo', '') def render_unknown(url): return '<unknown href="%s"/>' % url register_inclusion_renderer('.later', render_unknown, 50) a = Resource(foo, 'nothing.later') b = Resource(foo, 'something.js') c = Resource(foo, 'something.css') d = Resource(foo, 'something.ico') needed = NeededResources() needed.need(a) needed.need(b) needed.need(c) needed.need(d) assert needed.render() == """\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/something.css" /> <script type="text/javascript" src="/fanstatic/foo/something.js"></script> <link rel="shortcut icon" type="image/x-icon" href="/fanstatic/foo/something.ico"/> <unknown href="/fanstatic/foo/nothing.later"/>""" register_inclusion_renderer('.sooner', render_unknown, 5) e = Resource(foo, 'nothing.sooner') needed.need(e) assert needed.render() == """\ <unknown href="/fanstatic/foo/nothing.sooner"/> <link rel="stylesheet" type="text/css" href="/fanstatic/foo/something.css" /> <script type="text/javascript" src="/fanstatic/foo/something.js"></script> <link rel="shortcut icon" type="image/x-icon" href="/fanstatic/foo/something.ico"/> <unknown href="/fanstatic/foo/nothing.later"/>""" register_inclusion_renderer('.between', render_unknown, 25) f = Resource(foo, 'nothing.between') needed.need(f) assert needed.render() == """\
def test_register_inclusion_renderer(): foo = Library('foo', '') with pytest.raises(UnknownResourceExtensionError): # The renderer for '.unknown' is not yet defined. Resource(foo, 'nothing.unknown') def render_unknown(url): return '<link rel="unknown" href="%s" />' % url register_inclusion_renderer('.unknown', render_unknown) a = Resource(foo, 'nothing.unknown') needed = NeededResources() needed.need(a) assert needed.render() == ('<link rel="unknown" href="/fanstatic/foo/nothing.unknown" />')
def test_register_inclusion_renderer(): foo = Library('foo', '') with pytest.raises(UnknownResourceExtensionError): # The renderer for '.unknown' is not yet defined. Resource(foo, 'nothing.unknown') def render_unknown(url): return '<link rel="unknown" href="%s" />' % url register_inclusion_renderer('.unknown', render_unknown) a = Resource(foo, 'nothing.unknown') needed = NeededResources() needed.need(a) assert needed.render() == ( '<link rel="unknown" href="/fanstatic/foo/nothing.unknown" />')
def test_custom_renderer_keep_together(): foo = Library('foo', '') def render_print_css(url): return ('<link rel="stylesheet" type="text/css" href="%s" media="print"/>' % url) a = Resource(foo, 'printstylesheet.css', renderer=render_print_css) b = Resource(foo, 'regular.css') c = Resource(foo, 'something.js') needed = NeededResources() needed.need(a) needed.need(b) needed.need(c) assert needed.render() == """\
def test_custom_renderer_keep_together(): foo = Library('foo', '') def render_print_css(url): return ( '<link rel="stylesheet" type="text/css" href="%s" media="print"/>' % url) a = Resource(foo, 'printstylesheet.css', renderer=render_print_css) b = Resource(foo, 'regular.css') c = Resource(foo, 'something.js') needed = NeededResources() needed.need(a) needed.need(b) needed.need(c) assert needed.render() == """\