def test_convenience_clear_not_initialized(): # This test is put near the top of this module, or at least before # the very first time ``init_needed()`` is called. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() # Initialize a needed resources object. needed = init_needed() assert get_needed() == needed assert thread_local_needed_data.__dict__[NEEDED] == needed # Clear it. del_needed() # It is gone, really. with pytest.raises(KeyError): thread_local_needed_data.__dict__[NEEDED] # Clearing it again is OK. del_needed() # get_needed still work, dummy-style. dummy2 = get_needed() assert dummy2 != needed with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed()
def test_needed_deleted_after_request(): def html_app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) assert NEEDED in environ return ['<html><head></head><body</body></html>'] wrapped_app = Injector(html_app) request = webob.Request.blank('/') request.get_response(wrapped_app) # There's no NeededResources object anymore after the request has # been done. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear() def textplain_app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) assert NEEDED in environ return ['<html><head></head><body</body></html>'] wrapped_app = Injector(textplain_app) request = webob.Request.blank('/') request.get_response(wrapped_app) # There's no NeededResources object anymore after the request has # been done, even for response content types that would not have # been processed by fanstatic's inclusion rendering. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear()
def test_needed_deleted_after_request(): def html_app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) assert NEEDED in environ return [b'<html><head></head><body</body></html>'] wrapped_app = Injector(html_app) request = webob.Request.blank('/') request.get_response(wrapped_app) # There's no NeededResources object anymore after the request has # been done. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear() def textplain_app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) assert NEEDED in environ return [b'<html><head></head><body</body></html>'] wrapped_app = Injector(textplain_app) request = webob.Request.blank('/') request.get_response(wrapped_app) # There's no NeededResources object anymore after the request has # been done, even for response content types that would not have # been processed by fanstatic's inclusion rendering. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear()
def widget(self): b.need() # XXX this does not use any official API and needs to be # reconsidered. Its done anyway now to make the tests pass, # instead of just removing the corresponding test. get_needed()._bottom = True get_needed()._force_bottom = True return "the widget HTML itself"
def test_convenience_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] y1.need() assert get_needed().resources() == [x2, x1, y1]
def test_convenience_group_resource_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js') group = Group([x1, x2, y1]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] group.need() assert get_needed().resources() == [x2, x1, y1]
def test_deform_integration_render(self): import deform_autoneed deform_autoneed.includeme() form = _mk_richtext_form() form.render() resources = get_needed().resources() self.assertIn('deform.js', [x.filename for x in resources])
def __call__(self): resources = fanstatic.get_needed() stream = self.tmpl.generate(resources=Markup(resources.render()), title=self.context.title, launcher=Markup(self.launcher())) self.request.response.mimetype = 'text/html' self.request.response.write(stream.render('html', doctype='html'))
def __getattr__(self, name): resource = _get_resource(self.module, name) return '%s/%s' % ( fanstatic.get_needed().library_url(resource.library), resource.relpath )
def set_base_url(event): # At first sight it might be better to subscribe to the # IBeforeTraverseEvent for ISite objects and only set a base_url # then. However, we might be too early in that case and miss out # on essential information for computing URLs. One example of such # information is that of the virtualhost namespace traversal. needed = fanstatic.get_needed() ensure_base_url(needed, event.request)
def __str__(self): needed = fanstatic.get_needed() if not isinstance(needed, fanstatic.NeededResources): # We cannot render a URL in this case, we just return some # fake url to indicate this. return '++resource++%s%s' % (self.library.name, self.name) ensure_base_url(needed, self.request) return needed.library_url(self.library) + self.name
def test_include_needed(self): init_needed() obj = self._cut() obj.add('true', self._js_resource, discriminator_true) obj.add('false', self._js_resource, discriminator_false) obj.add('none', self._js_resource) self.assertEqual(obj.include_needed(None, None, None), ['true', 'none']) needed = get_needed().resources() self.assertIn(self._js_resource, needed)
def test_convenience_need_not_initialized(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) dummy = get_needed() assert not isinstance(dummy, NeededResources) # We return a new dummy instance for every get_needed: dummy2 = get_needed() assert dummy != dummy2 # A dummy never has resources: assert not dummy.has_resources() dummy.need(y1) with pytest.raises(NotImplementedError): dummy.render()
def test_deform_integration_exception_render(self): import deform_autoneed deform_autoneed.includeme() form = _mk_richtext_form() try: form.validate([('foo', 'bar')]) except deform.ValidationFailure as exc: exc.render() resources = get_needed().resources() self.assertIn('deform.js', [x.filename for x in resources])
def test_rendered_on_event(self): from arche.events import ViewInitializedEvent self.config.registry.settings['arche_ga.key'] = 'a_very_secret_code' self.config.include('arche_google_analytics') init_needed() request = testing.DummyRequest() view = self._mk_view(None, request) objectEventNotify(ViewInitializedEvent(view)) needed = get_needed() self.assertTrue(needed.resources()) self.assertIn('a_very_secret_code', needed.render())
def test_replace_bootstrap(self): #replace bootstrap css with pure_js from arche.fanstatic_lib import main_css from arche.fanstatic_lib import pure_js from js.bootstrap import bootstrap_css self.config.include('arche.utils') self.config.replace_fanstatic_resource(bootstrap_css, pure_js) main_css.need() will_include = get_needed().resources() self.assertIn(pure_js, will_include) self.assertIn(main_css, will_include) self.assertNotIn(bootstrap_css, will_include)
def test_remove_resource_with_deps(self): obj = self._cut() obj.libraries['deform_autoneed'] = library = Library('deform_autoneed', 'testing_fixture') unwanted_dependency = Resource(library, 'dummy.css') resource_with_requirement = Resource(library, 'dummy.js', depends = (unwanted_dependency,)) obj.requirements['dummy'] = [resource_with_requirement, unwanted_dependency] obj.remove_resource(unwanted_dependency) from deform_autoneed import need_lib need_lib('dummy', reg = obj) resources = get_needed().resources() self.assertEqual(len(obj.requirements['dummy']), 1) self.assertNotIn('dummy.css', [x.filename for x in resources]) self.assertEqual(len(resources), 1)
def test_register_form_resources(self): import colander import deform class DummySchema(colander.Schema): dummy = colander.SchemaNode(colander.String()) dummy_form = deform.Form(DummySchema()) from fanstatic import get_needed from fanstatic import init_needed from fanstatic import NeededResources init_needed() #Otherwise fanstatic won't get a proper needed resrouces object. context = testing.DummyResource() request = testing.DummyRequest() obj = self._cut(context, request) obj.register_form_resources(dummy_form) needed_resources = get_needed() self.assertIsInstance(needed_resources, NeededResources) filenames = [x.filename for x in needed_resources.resources()] #Note: Can be any filename registered by VoteIT. Just change it if this test fails :) self.assertIn('deform.css', filenames)
def test_register_form_resources(self): import colander import deform class DummySchema(colander.Schema): dummy = colander.SchemaNode(colander.String()) dummy_form = deform.Form(DummySchema()) from fanstatic import get_needed from fanstatic import init_needed from fanstatic import NeededResources init_needed( ) #Otherwise fanstatic won't get a proper needed resrouces object. context = testing.DummyResource() request = testing.DummyRequest() obj = self._cut(context, request) obj.register_form_resources(dummy_form) needed_resources = get_needed() self.assertIsInstance(needed_resources, NeededResources) filenames = [x.filename for x in needed_resources.resources()] #Note: Can be any filename registered by VoteIT. Just change it if this test fails :) self.assertIn('deform.css', filenames)
def app(environ, start_response): start_response('200 OK', []) needed = get_needed() assert needed is environ[NEEDED]
def app(environ, start_response): start_response('200 OK', []) needed = get_needed() needed.need(y1) needed.set_base_url('http://testapp') return [b'<html><head></head><body</body></html>']
def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) needed = get_needed() needed.need(a, {slot: b}) needed.set_base_url('http://testapp') return [b'<html><head></head><body></body></html>']
def test_dummy_needed_should_take_slots_argument(): needed = get_needed() a_resource = object() needed.need(a_resource, slots={})
def __str__(self): needed = fanstatic.get_needed() if not needed.has_base_url(): needed.set_base_url(IVirtualSite(self.request).get_root_url()) return needed.library_url(self.library) + self.name
def test_start_raiting_included(self): from voteit.core.fanstaticlib import _star_rating_css init_needed() obj = self._cut() needed = get_needed().resources() self.assertIn(_star_rating_css, needed)
def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) needed = get_needed() assert needed is environ[NEEDED]
def clear_needed_resources(event): needed = fanstatic.get_needed() if isinstance(needed, fanstatic.NeededResources): # Only if there's a concrete NeededResources. needed.clear()
def test_auto_need(self): form = _mk_richtext_form() self._fut(form, reg = self.reg) resources = get_needed().resources() self.assertIn('deform.js', [x.filename for x in resources])
def app(environ, start_response): start_response('200 OK', []) needed = get_needed() needed.need(y1) return [b'<html><head></head><body</body></html>']
def app(environ, start_response): start_response('200 OK', []) needed = get_needed() needed.need(y1) needed.set_base_url('http://testapp') return ['<html><head></head><body</body></html>']
def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) needed = get_needed() needed.need(y1) return [b'<html><head></head><body</body></html>']
def get_socialshareprivacy_url(): ''' call this after you need()ed socialshareprivacy! Returns the url from which socialshareprivacy will be shipped. ''' return fanstatic.get_needed().library_url(library)
def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) needed = get_needed() needed.need(y1) return ['<html><head></head><body</body></html>']
def set_base_url(event): needed = fanstatic.get_needed() if not needed.has_resources(): return if not needed.has_base_url(): needed.set_base_url(IVirtualSite(event.request).get_root_url())
def test_ensure_base_url_wont_fail(self): dummy_needed = fanstatic.get_needed() self.assertIsNone(ensure_base_url(dummy_needed, None))
def app(environ, start_response): start_response('200 OK', []) needed = get_needed() needed.need(y1) return ['<html><head></head><body</body></html>']