A bunch of work on the default pecan create project scaffold:
* Simpified the quickstart app. Now it's a simple intro w/ a "documentation" search form. * Updated the quickstart's project to no longer use py.test. * Added some simple illustration tests into the default scaffolded project to illustrate the correct approach for integration and functional tests.
This commit is contained in:
@@ -19,6 +19,7 @@ DEFAULT = {
|
||||
'static_root' : 'public',
|
||||
'template_path' : '',
|
||||
'debug' : False,
|
||||
'logging' : False,
|
||||
'force_canonical' : True,
|
||||
'errors' : {
|
||||
'__force_dict__' : True
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
from pecan import expose
|
||||
from pecan import expose, redirect
|
||||
from formencode import Schema, validators as v
|
||||
from webob.exc import status_map
|
||||
|
||||
|
||||
class SampleForm(Schema):
|
||||
name = v.String(not_empty=True)
|
||||
age = v.Int(not_empty=True)
|
||||
class SearchForm(Schema):
|
||||
q = v.String(not_empty=True)
|
||||
|
||||
|
||||
class RootController(object):
|
||||
@@ -19,13 +18,12 @@ class RootController(object):
|
||||
|
||||
@index.when(
|
||||
method = 'POST',
|
||||
template = 'success.html',
|
||||
schema = SampleForm(),
|
||||
schema = SearchForm(),
|
||||
error_handler = '/index',
|
||||
htmlfill = dict(auto_insert_errors = True, prefix_error = False)
|
||||
htmlfill = dict(auto_insert_errors = True)
|
||||
)
|
||||
def index_post(self, name, age):
|
||||
return dict(name=name)
|
||||
def index_post(self, q):
|
||||
redirect('http://pecan.readthedocs.org/en/latest/search.html?q=%s' % q)
|
||||
|
||||
@expose('error.html')
|
||||
def error(self, status):
|
||||
|
||||
@@ -20,23 +20,15 @@
|
||||
</p>
|
||||
|
||||
<p>
|
||||
To get an idea of how to develop applications with Pecan,
|
||||
here is a simple form:
|
||||
...or you can search the documentation here:
|
||||
</p>
|
||||
|
||||
<form method="POST" action="/">
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="name">Your Name:</label></td>
|
||||
<td><input name="name" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="age">Your Age:</label></td>
|
||||
<td><input name="age" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<input type="submit" value="Submit" />
|
||||
<fieldset>
|
||||
<input name="q" />
|
||||
<input type="submit" value="Search" />
|
||||
<fieldset>
|
||||
<small>Enter search terms or a module, class or function name.</small>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
<%inherit file="layout.html"/>
|
||||
|
||||
## override the title
|
||||
<%def name="title()">
|
||||
Success!
|
||||
</%def>
|
||||
|
||||
## now define the body
|
||||
<header>
|
||||
<h1><img src="/images/logo.png" /></h1>
|
||||
</header>
|
||||
<p>Your form submission was successful! Thanks, ${name}!</p>
|
||||
<p><a href="/">Go Back</a></p>
|
||||
18
pecan/templates/project/+package+/tests/__init__.py_tmpl
Normal file
18
pecan/templates/project/+package+/tests/__init__.py_tmpl
Normal file
@@ -0,0 +1,18 @@
|
||||
from unittest import TestCase
|
||||
from pecan.configuration import set_config
|
||||
from pecan.testing import load_test_app
|
||||
|
||||
__all__ = ['EnvironmentTest']
|
||||
|
||||
|
||||
class EnvironmentTest(TestCase):
|
||||
"""
|
||||
Used for integration or functional tests where you need to test your
|
||||
literal application of its integration with the framework.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.app = load_test_app('config.py')
|
||||
|
||||
def tearDown(self):
|
||||
set_config({}, overwrite=True)
|
||||
27
pecan/templates/project/+package+/tests/config.py_tmpl
Normal file
27
pecan/templates/project/+package+/tests/config.py_tmpl
Normal file
@@ -0,0 +1,27 @@
|
||||
# Server Specific Configurations
|
||||
server = {
|
||||
'port' : '8080',
|
||||
'host' : '0.0.0.0'
|
||||
}
|
||||
|
||||
# Pecan Application Configurations
|
||||
app = {
|
||||
'root' : '${package}.controllers.root.RootController',
|
||||
'modules' : ['${package}'],
|
||||
'static_root' : '%(confdir)s/public',
|
||||
'template_path' : '%(confdir)s/${package}/templates',
|
||||
'reload' : True,
|
||||
'debug' : True,
|
||||
'logging' : False,
|
||||
'errors' : {
|
||||
'404' : '/error/404',
|
||||
'__force_dict__' : True
|
||||
}
|
||||
}
|
||||
|
||||
# Custom Configurations must be in Python dictionary format::
|
||||
#
|
||||
# foo = {'bar':'baz'}
|
||||
#
|
||||
# All configurations are accessible at::
|
||||
# pecan.conf
|
||||
@@ -1,41 +0,0 @@
|
||||
from unittest import TestCase
|
||||
import config
|
||||
|
||||
|
||||
class TestConfigServer(TestCase):
|
||||
|
||||
def test_server_port(self):
|
||||
assert config.server['port'] == '8080'
|
||||
|
||||
def test_server_host(self):
|
||||
assert config.server['host'] == '0.0.0.0'
|
||||
|
||||
|
||||
class TestConfigApp(TestCase):
|
||||
|
||||
def test_app_root(self):
|
||||
root = config.app['root']
|
||||
assert root.__class__.__name__ == 'RootController'
|
||||
|
||||
def test_app_modules(self):
|
||||
assert len(config.app['modules']) == 1
|
||||
|
||||
def test_app_static_root(self):
|
||||
assert 'public' in config.app['static_root']
|
||||
|
||||
def test_app_template_path(self):
|
||||
assert 'templates' in config.app['template_path']
|
||||
|
||||
def test_app_reload(self):
|
||||
assert config.app['reload']
|
||||
|
||||
def test_app_debug(self):
|
||||
assert config.app['debug']
|
||||
|
||||
def test_app_errors(self):
|
||||
errors = {
|
||||
'404' : '/error/404',
|
||||
'__force_dict__' : True
|
||||
}
|
||||
|
||||
assert config.app['errors'] == errors
|
||||
@@ -1,13 +1,9 @@
|
||||
from unittest import TestCase
|
||||
from webtest import TestApp
|
||||
|
||||
import py.test
|
||||
from ${package}.tests import EnvironmentTest
|
||||
|
||||
|
||||
class TestRootController(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.app = TestApp(py.test.wsgi_app)
|
||||
class TestRootController(EnvironmentTest):
|
||||
|
||||
def test_get(self):
|
||||
response = self.app.get('/')
|
||||
|
||||
@@ -20,9 +20,13 @@ div#content {
|
||||
}
|
||||
|
||||
form {
|
||||
margin: 0 1em;
|
||||
padding: 1em;
|
||||
border: 5px transparent;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
fieldset {
|
||||
border: 0;
|
||||
}
|
||||
|
||||
input.error {
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
[pytest]
|
||||
addopts = -p pecan.testing --with-config=./config.py
|
||||
@@ -15,6 +15,7 @@ setup(
|
||||
install_requires = [
|
||||
"pecan",
|
||||
],
|
||||
test_suite = '${package}',
|
||||
zip_safe = False,
|
||||
paster_plugins = ${egg_plugins},
|
||||
include_package_data = True,
|
||||
|
||||
Reference in New Issue
Block a user