diff --git a/tests/pecantest/config.py b/tests/pecantest/config.py new file mode 100644 index 0000000..63dbe2c --- /dev/null +++ b/tests/pecantest/config.py @@ -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 diff --git a/tests/pecantest/setup.cfg b/tests/pecantest/setup.cfg new file mode 100644 index 0000000..00ca220 --- /dev/null +++ b/tests/pecantest/setup.cfg @@ -0,0 +1,6 @@ +[nosetests] +match=^test +where=test +nocapture=1 +cover-package=test +cover-erase=1 diff --git a/tests/pecantest/setup.py b/tests/pecantest/setup.py new file mode 100644 index 0000000..b901f62 --- /dev/null +++ b/tests/pecantest/setup.py @@ -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']) +) diff --git a/tests/pecantest/test/__init__.py b/tests/pecantest/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/pecantest/test/app.py b/tests/pecantest/test/app.py new file mode 100644 index 0000000..727c357 --- /dev/null +++ b/tests/pecantest/test/app.py @@ -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) + ) diff --git a/tests/pecantest/test/controllers/__init__.py b/tests/pecantest/test/controllers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/pecantest/test/controllers/root.py b/tests/pecantest/test/controllers/root.py new file mode 100644 index 0000000..b098376 --- /dev/null +++ b/tests/pecantest/test/controllers/root.py @@ -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) diff --git a/tests/pecantest/test/controllers/ws.py b/tests/pecantest/test/controllers/ws.py new file mode 100644 index 0000000..15f5367 --- /dev/null +++ b/tests/pecantest/test/controllers/ws.py @@ -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 diff --git a/tests/pecantest/test/model/__init__.py b/tests/pecantest/test/model/__init__.py new file mode 100644 index 0000000..ab4be6a --- /dev/null +++ b/tests/pecantest/test/model/__init__.py @@ -0,0 +1,2 @@ +def init_model(): + pass diff --git a/tests/pecantest/test/tests/__init__.py b/tests/pecantest/test/tests/__init__.py new file mode 100644 index 0000000..78ea527 --- /dev/null +++ b/tests/pecantest/test/tests/__init__.py @@ -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) diff --git a/tests/pecantest/test/tests/config.py b/tests/pecantest/test/tests/config.py new file mode 100644 index 0000000..9b8ffa6 --- /dev/null +++ b/tests/pecantest/test/tests/config.py @@ -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 diff --git a/tests/pecantest/test/tests/test_ws.py b/tests/pecantest/test/tests/test_ws.py new file mode 100644 index 0000000..4b7f7d1 --- /dev/null +++ b/tests/pecantest/test/tests/test_ws.py @@ -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 '1' in a.body