Add a test for the pecan adapter
This commit is contained in:
parent
ddd2ba251e
commit
eaa6cc8083
45
tests/pecantest/config.py
Normal file
45
tests/pecantest/config.py
Normal 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
|
6
tests/pecantest/setup.cfg
Normal file
6
tests/pecantest/setup.cfg
Normal 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
22
tests/pecantest/setup.py
Normal 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'])
|
||||||
|
)
|
0
tests/pecantest/test/__init__.py
Normal file
0
tests/pecantest/test/__init__.py
Normal file
15
tests/pecantest/test/app.py
Normal file
15
tests/pecantest/test/app.py
Normal 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)
|
||||||
|
)
|
0
tests/pecantest/test/controllers/__init__.py
Normal file
0
tests/pecantest/test/controllers/__init__.py
Normal file
16
tests/pecantest/test/controllers/root.py
Normal file
16
tests/pecantest/test/controllers/root.py
Normal 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)
|
49
tests/pecantest/test/controllers/ws.py
Normal file
49
tests/pecantest/test/controllers/ws.py
Normal 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 d’un révolutionnaire pour servir à "
|
||||||
|
u"l’histoire 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
|
2
tests/pecantest/test/model/__init__.py
Normal file
2
tests/pecantest/test/model/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
def init_model():
|
||||||
|
pass
|
22
tests/pecantest/test/tests/__init__.py
Normal file
22
tests/pecantest/test/tests/__init__.py
Normal 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)
|
25
tests/pecantest/test/tests/config.py
Normal file
25
tests/pecantest/test/tests/config.py
Normal 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
|
21
tests/pecantest/test/tests/test_ws.py
Normal file
21
tests/pecantest/test/tests/test_ws.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user