Add a test for the pecan adapter

This commit is contained in:
Christophe de Vienne 2012-11-06 22:37:33 +01:00
parent ddd2ba251e
commit eaa6cc8083
12 changed files with 223 additions and 0 deletions

45
tests/pecantest/config.py Normal file
View File

@ -0,0 +1,45 @@
# Server Specific Configurations
server = {
'port': '8080',
'host': '0.0.0.0'
}
# Pecan Application Configurations
app = {
'root': 'test.controllers.root.RootController',
'modules': ['test'],
'static_root': '%(confdir)s/public',
'template_path': '%(confdir)s/test/templates',
'debug': True,
'errors': {
404: '/error/404',
'__force_dict__': True
}
}
logging = {
'loggers': {
'root' : {'level': 'INFO', 'handlers': ['console']},
'test': {'level': 'DEBUG', 'handlers': ['console']}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
}
},
'formatters': {
'simple': {
'format': ('%(asctime)s %(levelname)-5.5s [%(name)s]'
'[%(threadName)s] %(message)s')
}
}
}
# Custom Configurations must be in Python dictionary format::
#
# foo = {'bar':'baz'}
#
# All configurations are accessible at::
# pecan.conf

View File

@ -0,0 +1,6 @@
[nosetests]
match=^test
where=test
nocapture=1
cover-package=test
cover-erase=1

22
tests/pecantest/setup.py Normal file
View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
try:
from setuptools import setup, find_packages
except ImportError:
from ez_setup import use_setuptools
use_setuptools()
from setuptools import setup, find_packages
setup(
name = 'test',
version = '0.1',
description = '',
author = '',
author_email = '',
install_requires = [
"pecan",
],
test_suite = 'test',
zip_safe = False,
include_package_data = True,
packages = find_packages(exclude=['ez_setup'])
)

View File

View File

@ -0,0 +1,15 @@
from pecan import make_app
from test import model
def setup_app(config):
model.init_model()
return make_app(
config.app.root,
static_root = config.app.static_root,
template_path = config.app.template_path,
logging = getattr(config, 'logging', {}),
debug = getattr(config.app, 'debug', False),
force_canonical = getattr(config.app, 'force_canonical', True)
)

View File

@ -0,0 +1,16 @@
from pecan import expose
from webob.exc import status_map
from .ws import AuthorsController
class RootController(object):
authors = AuthorsController()
@expose('error.html')
def error(self, status):
try:
status = int(status)
except ValueError: # pragma: no cover
status = 500
message = getattr(status_map.get(status), 'explanation', '')
return dict(status=status, message=message)

View File

@ -0,0 +1,49 @@
# encoding=utf8
from pecan.rest import RestController
from wsme.types import Base, text, wsattr
import wsme
import wsme.pecan
class Author(Base):
id = int
firstname = text
books = wsattr(['Book'])
class Book(Base):
id = int
name = text
author = wsattr('Author')
class BooksController(RestController):
@wsme.pecan.wsexpose(Book, int, int)
def get(self, author_id, id):
print repr(author_id), repr(id)
book = Book(
name=u"Les Confessions dun révolutionnaire pour servir à "
u"lhistoire de la révolution de février",
author=Author(lastname=u"Proudhon"))
return book
@wsme.pecan.wsexpose(Book, int, int, body=Book)
def put(self, author_id, id, book=None):
print author_id, id
print book
return book
class AuthorsController(RestController):
books = BooksController()
@wsme.pecan.wsexpose(Author, int)
def get(self, id):
author = Author()
author.id = id
author.name = u"aname"
return author

View File

@ -0,0 +1,2 @@
def init_model():
pass

View File

@ -0,0 +1,22 @@
import os
from unittest import TestCase
from pecan import set_config
from pecan.testing import load_test_app
__all__ = ['FunctionalTest']
class FunctionalTest(TestCase):
"""
Used for functional tests where you need to test your
literal application and its integration with the framework.
"""
def setUp(self):
self.app = load_test_app(os.path.join(
os.path.dirname(__file__),
'config.py'
))
def tearDown(self):
set_config({}, overwrite=True)

View File

@ -0,0 +1,25 @@
# Server Specific Configurations
server = {
'port' : '8080',
'host' : '0.0.0.0'
}
# Pecan Application Configurations
app = {
'root' : 'test.controllers.root.RootController',
'modules' : ['test'],
'static_root' : '%(confdir)s/../../public',
'template_path' : '%(confdir)s/../templates',
'debug' : True,
'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

View File

@ -0,0 +1,21 @@
from test.tests import FunctionalTest
import json
class TestWS(FunctionalTest):
def test_get_author(self):
a = self.app.get(
'/authors/1.json',
)
print a
a = json.loads(a.body)
print a
assert a['id'] == 1
a = self.app.get(
'/authors/1.xml',
)
print a
assert '<id>1</id>' in a.body