forked from orbit-oss/flask
Added a testcase for the improved module support
This commit is contained in:
parent
665fa2a32b
commit
d67a36cbdb
9 changed files with 45 additions and 2 deletions
|
|
@ -12,7 +12,7 @@
|
||||||
from flask.helpers import _PackageBoundObject
|
from flask.helpers import _PackageBoundObject
|
||||||
|
|
||||||
|
|
||||||
def _register_module(module):
|
def _register_module(module, static_path):
|
||||||
"""Internal helper function that returns a function for recording
|
"""Internal helper function that returns a function for recording
|
||||||
that registers the `send_static_file` function for the module on
|
that registers the `send_static_file` function for the module on
|
||||||
the application of necessary. It also registers the module on
|
the application of necessary. It also registers the module on
|
||||||
|
|
@ -99,7 +99,7 @@ class Module(_PackageBoundObject):
|
||||||
_PackageBoundObject.__init__(self, import_name)
|
_PackageBoundObject.__init__(self, import_name)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.url_prefix = url_prefix
|
self.url_prefix = url_prefix
|
||||||
self._register_events = [_register_module(self)]
|
self._register_events = [_register_module(self, static_path)]
|
||||||
|
|
||||||
def route(self, rule, **options):
|
def route(self, rule, **options):
|
||||||
"""Like :meth:`Flask.route` but for a module. The endpoint for the
|
"""Like :meth:`Flask.route` but for a module. The endpoint for the
|
||||||
|
|
|
||||||
|
|
@ -630,6 +630,21 @@ class ModuleTestCase(unittest.TestCase):
|
||||||
assert rv.status_code == 500
|
assert rv.status_code == 500
|
||||||
assert 'internal server error' == rv.data
|
assert 'internal server error' == rv.data
|
||||||
|
|
||||||
|
def test_templates_and_static(self):
|
||||||
|
from moduleapp import app
|
||||||
|
c = app.test_client()
|
||||||
|
|
||||||
|
rv = c.get('/')
|
||||||
|
assert rv.data == 'Hello from the Frontend'
|
||||||
|
rv = c.get('/admin/')
|
||||||
|
assert rv.data == 'Hello from the Admin'
|
||||||
|
rv = c.get('/admin/static/test.txt')
|
||||||
|
assert rv.data.strip() == 'Admin File'
|
||||||
|
|
||||||
|
with app.test_request_context():
|
||||||
|
assert flask.url_for('admin.static', filename='test.txt') \
|
||||||
|
== '/admin/static/test.txt'
|
||||||
|
|
||||||
|
|
||||||
class SendfileTestCase(unittest.TestCase):
|
class SendfileTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
||||||
7
tests/moduleapp/__init__.py
Normal file
7
tests/moduleapp/__init__.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
from flask import Flask
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
from moduleapp.apps.admin import admin
|
||||||
|
from moduleapp.apps.frontend import frontend
|
||||||
|
app.register_module(admin)
|
||||||
|
app.register_module(frontend)
|
||||||
0
tests/moduleapp/apps/__init__.py
Normal file
0
tests/moduleapp/apps/__init__.py
Normal file
9
tests/moduleapp/apps/admin/__init__.py
Normal file
9
tests/moduleapp/apps/admin/__init__.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from flask import Module, render_template
|
||||||
|
|
||||||
|
|
||||||
|
admin = Module(__name__, url_prefix='/admin')
|
||||||
|
|
||||||
|
|
||||||
|
@admin.route('/')
|
||||||
|
def index():
|
||||||
|
return render_template('admin/index.html')
|
||||||
1
tests/moduleapp/apps/admin/static/test.txt
Normal file
1
tests/moduleapp/apps/admin/static/test.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Admin File
|
||||||
1
tests/moduleapp/apps/admin/templates/index.html
Normal file
1
tests/moduleapp/apps/admin/templates/index.html
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Hello from the Admin
|
||||||
9
tests/moduleapp/apps/frontend/__init__.py
Normal file
9
tests/moduleapp/apps/frontend/__init__.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from flask import Module, render_template
|
||||||
|
|
||||||
|
|
||||||
|
frontend = Module(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@frontend.route('/')
|
||||||
|
def index():
|
||||||
|
return render_template('frontend/index.html')
|
||||||
1
tests/moduleapp/apps/frontend/templates/index.html
Normal file
1
tests/moduleapp/apps/frontend/templates/index.html
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Hello from the Frontend
|
||||||
Loading…
Add table
Add a link
Reference in a new issue