Extension that renders templates with Jinja
This commit is contained in:
1
tests/microdot_jinja/templates/hello.txt
Normal file
1
tests/microdot_jinja/templates/hello.txt
Normal file
@@ -0,0 +1 @@
|
||||
Hello, {{ name }}!
|
||||
58
tests/microdot_jinja/test_jinja.py
Normal file
58
tests/microdot_jinja/test_jinja.py
Normal file
@@ -0,0 +1,58 @@
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from microdot import Microdot, Request
|
||||
from microdot_asyncio import Microdot as MicrodotAsync, Request as RequestAsync
|
||||
from microdot_jinja import render_template, init_templates
|
||||
from tests.mock_socket import get_request_fd, get_async_request_fd
|
||||
|
||||
init_templates('tests/microdot_jinja/templates')
|
||||
|
||||
|
||||
def _run(coro):
|
||||
return asyncio.get_event_loop().run_until_complete(coro)
|
||||
|
||||
|
||||
@unittest.skipIf(sys.implementation.name == 'micropython',
|
||||
'not supported under MicroPython')
|
||||
class TestUTemplate(unittest.TestCase):
|
||||
def test_render_template(self):
|
||||
s = render_template('hello.txt', name='foo')
|
||||
self.assertEqual(s, 'Hello, foo!')
|
||||
|
||||
def test_render_template_in_app(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return render_template('hello.txt', name='foo')
|
||||
|
||||
req = Request.create(app, get_request_fd('GET', '/'), 'addr')
|
||||
res = app.dispatch_request(req)
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(list(res.body_iter()), [b'Hello, foo!'])
|
||||
|
||||
def test_render_template_in_app_async(self):
|
||||
app = MicrodotAsync()
|
||||
|
||||
@app.route('/')
|
||||
async def index(req):
|
||||
return render_template('hello.txt', name='foo')
|
||||
|
||||
req = _run(RequestAsync.create(
|
||||
app, get_async_request_fd('GET', '/'), 'addr'))
|
||||
res = _run(app.dispatch_request(req))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
|
||||
async def get_result():
|
||||
result = []
|
||||
async for chunk in res.body_iter():
|
||||
result.append(chunk)
|
||||
return result
|
||||
|
||||
result = _run(get_result())
|
||||
self.assertEqual(result, [b'Hello, foo!'])
|
||||
@@ -6,8 +6,7 @@ except ImportError:
|
||||
import unittest
|
||||
from microdot import Microdot, Request
|
||||
from microdot_asyncio import Microdot as MicrodotAsync, Request as RequestAsync
|
||||
from microdot_utemplate import render_template, render_template_string, \
|
||||
init_templates
|
||||
from microdot_utemplate import render_template, init_templates
|
||||
from tests.mock_socket import get_request_fd, get_async_request_fd
|
||||
|
||||
init_templates('tests/microdot_utemplate/templates')
|
||||
@@ -22,10 +21,6 @@ class TestUTemplate(unittest.TestCase):
|
||||
s = list(render_template('hello.txt', name='foo'))
|
||||
self.assertEqual(s, ['Hello, ', 'foo', '!\n'])
|
||||
|
||||
def test_render_template_string(self):
|
||||
s = render_template_string('hello.txt', name='foo')
|
||||
self.assertEqual(s.strip(), 'Hello, foo!')
|
||||
|
||||
def test_render_template_in_app(self):
|
||||
app = Microdot()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user