def test_page_render(): # Working around some weird issue with pypy3+django3.0 from django.conf import settings settings.DEBUG = False # end workaround class MyPage(Page): header = html.h1('Foo') body = html.div('bar bar') my_page = MyPage() my_page = my_page.bind(request=user_req('get')) response = my_page.render_to_response() expected_html = ''' <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1> Foo </h1> <div> bar bar </div> </body> </html> ''' prettified_expected = prettify(expected_html) prettified_actual = prettify(response.content) assert prettified_expected == prettified_actual
def test_assets_render_from_bulma_style(): class MyPage(Page): class Meta: iommi_style = 'bulma' assets__axios = None expected = prettify('''\ <!DOCTYPE html> <html> <head> <title></title> <script crossorigin="anonymous" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" src="https://code.jquery.com/jquery-3.4.1.js"></script> <link href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css" rel="stylesheet"> <script> $(document).ready(function() { // Check for click events on the navbar burger icon $(".navbar-burger").click(function() { // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu" $(".navbar-burger").toggleClass("is-active"); $(".navbar-menu").toggleClass("is-active"); }); }); </script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"> </head> <body> <div class="container main"/> </body> </html> ''') actual = prettify( MyPage().bind(request=req('get')).render_to_response().content) assert actual == expected
def test_assets_render_any_fragment_from_style(): register_style( 'my_style', Style( test, assets__an_asset=Fragment('This is a fragment!'), )) class MyPage(Page): class Meta: iommi_style = 'my_style' expected = prettify(''' <!DOCTYPE html> <html> <head> <title/> This is a fragment! </head> <body/> </html> ''') actual = prettify( MyPage().bind(request=req('get')).render_to_response().content) assert actual == expected unregister_style('my_style')
def test_assets_render_from_style(): register_style( 'my_style', Style( test, assets__an_asset=Asset.css(attrs__href='http://foo.bar/baz'), )) class MyPage(Page): class Meta: iommi_style = 'my_style' expected = prettify(''' <!DOCTYPE html> <html> <head> <title/> <link href='http://foo.bar/baz' rel="stylesheet"/> </head> <body/> </html> ''') actual = prettify( MyPage().bind(request=req('get')).render_to_response().content) assert actual == expected unregister_style('my_style')
def test_page_render(): class MyPage(Page): header = html.h1('Foo') body = html.div('bar bar') my_page = MyPage() request = req('get') request.user = Struct() my_page = my_page.bind(request=request) response = my_page.render_to_response() expected_html = ''' <html> <head></head> <body> <h1> Foo </h1> <div> bar bar </div> </body> </html> ''' prettified_expected = prettify(expected_html) prettified_actual = prettify(response.content) assert prettified_expected == prettified_actual
def test_deprecated_assets_style(settings, capsys): settings.DEBUG = True with register_style( 'my_style', Style( test, assets__an_asset=Asset.css(attrs__href='http://foo.bar/baz'), ), ): captured = capsys.readouterr() assert 'Warning: The preferred way to add top level assets config' in captured.out settings.DEBUG = False expected = prettify( ''' <!DOCTYPE html> <html> <head> <title/> <link href='http://foo.bar/baz' rel="stylesheet"/> </head> <body/> </html> ''' ) actual = prettify(Page(iommi_style='my_style').bind(request=req('get')).render_to_response().content) assert actual == expected
def test_asset_shortcuts(): class MyPage(Page): class Meta: assets__css_asset = Asset.css(attrs__href='http://foo.bar/baz.css') assets__js_asset = Asset.js(attrs__href='http://foo.bar/baz.j') expected = prettify(''' <!DOCTYPE html> <html> <head> <title/> <link href="http://foo.bar/baz.css" rel="stylesheet"/> <script href="http://foo.bar/baz.j"/> </head> <body /> </html> ''') actual = prettify( MyPage().bind(request=req('get')).render_to_response().content) assert actual == expected
def test_assets_render_any_fragment(): class MyPage(Page): foo = html.div( 'foo', assets__my_asset=html.span(attrs__href='http://foo.bar/baz')) expected = prettify(''' <!DOCTYPE html> <html> <head> <title/> <span href='http://foo.bar/baz'/> </head> <body> <div> foo </div> </body> </html> ''') actual = prettify( MyPage().bind(request=req('get')).render_to_response().content) assert actual == expected
def test_assets_float_to_root(): class MyPage(Page): foo = html.div( 'foo', assets__my_asset=Asset.css(attrs__href='http://foo.bar/baz')) expected = prettify(''' <!DOCTYPE html> <html> <head> <title/> <link href='http://foo.bar/baz' rel="stylesheet"/> </head> <body> <div> foo </div> </body> </html> ''') actual = prettify( MyPage().bind(request=req('get')).render_to_response().content) assert actual == expected
def assert_renders(action, html): assert prettify(action.__html__()) == prettify(html)