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_mode_fully_specified(): foo = Library('foo', '') k_debug = Resource(foo, 'k-debug.js') k = Resource(foo, 'k.js', debug=k_debug) needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert needed.resources() == [k_debug] # If no minified can be found, the 'raw' resource is taken. needed = NeededResources(minified=True) needed.need(k) assert needed.resources() == [k] with pytest.raises(ConfigurationError): NeededResources(debug=True, minified=True) # If only a minified resource is defined, debug returns the raw version. x = Resource(foo, 'x.js', minified='x-min.js') needed = NeededResources(debug=True) needed.need(x) assert needed.resources() == [x]
def test_mode_shortcut(): foo = Library('foo', '') k = Resource(foo, 'k.js', debug='k-debug.js') needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert len(needed.resources()) == 1 assert needed.resources()[0].relpath == 'k-debug.js'
def test_bundle_resources_bottomsafe(): foo = Library('foo', '') a = Resource(foo, 'a.css') b = Resource(foo, 'b.css', bottom=True) needed = NeededResources(resources=[a, b], bundle=True) assert needed.render_topbottom() == ('''\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/:bundle:a.css;b.css" />''', '') needed = NeededResources(resources=[a, b], bundle=True, bottom=True) assert needed.render_topbottom() == ('''\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/a.css" />''', '''\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" />''')
def test_library_url_base_url(): foo = Library('foo', '') needed = NeededResources(base_url="http://example.com/something") assert (needed.library_url(foo) == 'http://example.com/something/fanstatic/foo')
def test_library_url_version_hashing(tmpdir): foo = Library('foo', tmpdir.strpath) needed = NeededResources(versioning=True) url = needed.library_url(foo) assert re.match('/fanstatic/foo/:version:[0-9T:.-]*$', url) # The md5 based version URL is available through the # `versioning_use_md5` parameter: needed = NeededResources(versioning=True, versioning_use_md5=True) md5_url = needed.library_url(foo) assert url != md5_url # If the Library defines a version, the version is used. bar = Library('bar', '', version='1') assert needed.library_url(bar) == '/fanstatic/bar/:version:1'
def test_library_ordering_bug(): jquery_lib = Library('jquery', '') jqueryui_lib = Library('jqueryui', '') obviel_lib = Library('obviel', '') bread_lib = Library('bread', '') app_lib = Library('app', '') jquery = Resource(jquery_lib, 'jquery.js') jqueryui = Resource(jqueryui_lib, 'jqueryui.js', depends=[jquery]) obviel = Resource(obviel_lib, 'obviel.js', depends=[jquery]) obviel_forms = Resource(obviel_lib, 'obviel_forms.js', depends=[obviel]) obviel_datepicker = Resource(obviel_lib, 'obviel_datepicker.js', depends=[obviel_forms, jqueryui]) vtab = Resource(bread_lib, 'vtab.js', depends=[jqueryui]) tabview = Resource(bread_lib, 'tabview.js', depends=[obviel, vtab]) bread = Resource(bread_lib, 'bread.js', depends=[tabview, obviel_forms]) app = Resource(app_lib, 'app.js', depends=[bread, obviel_datepicker]) needed = NeededResources() needed.need(app) resources = needed.resources() for resource in resources: print resource, resource.library.library_nr assert resources == [ jquery, jqueryui, obviel, obviel_forms, obviel_datepicker, vtab, tabview, bread, app ]
def test_library_url_script_name_base_url(): foo = Library('foo', '') needed = NeededResources(script_name='/root', base_url="http://example.com/something") # base_url is set so script_name should be ignored assert (needed.library_url(foo) == 'http://example.com/something/fanstatic/foo')
def test_html_insert_head_with_attributes(): # ticket 72: .need() broken when <head> tag has attributes foo = Library('foo', '') x1 = Resource(foo, 'a.js') needed = NeededResources(resources=[x1]) html = '<html><head profile="http://example.org">something</head></html>' assert needed.render_into_html(html) == '''\
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_rollup_without_mode(): foo = Library('foo', '') h1 = Resource(foo, 'h1.js', debug='h1-debug.js') h2 = Resource(foo, 'h2.js', debug='h2-debug.js') gianth = Resource(foo, 'gianth.js', supersedes=[h1, h2]) needed = NeededResources(resources=[h1, h2], rollup=True, debug=True) # no mode available for rollup, use the rollup. assert needed.resources() == [gianth]
def test_bundle_single_dont_bundle_entry(): foo = Library('foo', '') a = Resource(foo, 'a.js', dont_bundle=True) needed = NeededResources(bundle=True) needed.need(a) resources = needed.resources() assert resources == [a]
def test_rollup_modes(): foo = Library('foo', '') f1 = Resource(foo, 'f1.js', debug='f1-debug.js') f2 = Resource(foo, 'f2.js', debug='f2-debug.js') giantf = Resource(foo, 'giantf.js', supersedes=[f1, f2], debug='giantf-debug.js') needed = NeededResources(rollup=True) needed.need(f1) needed.need(f2) assert needed.resources() == [giantf] needed = NeededResources(rollup=True, debug=True) needed.need(f1) needed.need(f2) assert needed.resources() == [giantf.modes['debug']]
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_bundle_single_entry(): # we can successfully bundle a single resource (it's not bundled though) foo = Library('foo', '') a = Resource(foo, 'a.js') needed = NeededResources(bundle=True) needed.need(a) resources = needed.resources() assert resources == [a]
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_top_bottom_insert(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) html = "<html><head>rest of head</head><body>rest of body</body></html>" needed = NeededResources(bottom=True, force_bottom=True) needed.need(y1) assert needed.render_topbottom_into_html(html) == '''\
def test_redundant_resource_reorder(): 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(x1) needed.need(x2) needed.need(y1) assert needed.resources() == [x2, x1, y1]
def test_rollup_cannot(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) assert needed.resources() == [b1] assert giant not in needed.resources()
def test_redundant_more_complicated_depends_on_all(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5]
def test_html_bottom_safe_used_with_minified(): foo = Library('foo', '') a = Resource(foo, 'a.js', minified='a-minified.js', bottom=True) needed = NeededResources(minified=True, bottom=True) needed.need(a) top, bottom = needed.render_topbottom() assert top == '' assert bottom == ('<script type="text/javascript" ' 'src="/fanstatic/foo/a-minified.js"></script>')
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_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_no_need_to_fill_in_not_required(): needed = NeededResources() lib = Library('lib', '') slot = Slot(lib, '.js', required=False) a = Resource(lib, 'a.js', depends=[slot]) needed.need(a) # slot wasn't required and not filled in, so filled slot doesn't show up assert needed.resources() == [a]
def test_sort_library_by_name(): b_lib = Library('b_lib', '') a_lib = Library('a_lib', '') a_a = Resource(a_lib, 'a.js') a_b = Resource(b_lib, 'a.js') needed = NeededResources() needed.need(a_b) needed.need(a_a) assert needed.resources() == [a_a, a_b]
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_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_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_group_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') group = Group([x1, x2]) needed = NeededResources() needed.need(group) assert group.resources == set([x1, x2]) more_stuff = Resource(foo, 'more_stuff.js', depends=[group]) assert more_stuff.resources == set([x1, x2, more_stuff])
def test_html_top_bottom_force_bottom(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources(bottom=True, force_bottom=True) needed.need(y1) top, bottom = needed.render_topbottom() assert top == '''\ <link rel="stylesheet" type="text/css" href="/fanstatic/foo/b.css" />''' assert bottom == '''\