Commits

Atsushi Odagiri  committed 8a4df66

functional test for add and show page.

  • Participants
  • Parent commits ce73ec1

Comments (0)

Files changed (4)

File crosswiki/__init__.py

 from pyramid.config import Configurator
+from sqlalchemy import engine_from_config
+import sqlahelper
 
 def main(global_config, **settings):
     """ This function returns a Pyramid WSGI application.
     """
-    config = Configurator(settings=settings)
+    engine = engine_from_config(settings)
+    config = Configurator(settings=settings,
+        root_factory='.models.WikiResource')
     config.add_static_view('static', 'static', cache_max_age=3600)
     config.add_route('home', '/')
+    config.add_route('page', '/{page_name}')
+    config.add_route('create_page', '/{page_name}/create')
     config.scan()
+    from .models import create_frontpage
+    create_frontpage(engine)
     return config.make_wsgi_app()

File crosswiki/models.py

 from docutils.core import publish_parts
 import sqlalchemy as sa
 import sqlahelper
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.orm.exc import NoResultFound
 
 Base = sqlahelper.get_base()
 DBSession = sqlahelper.get_session()
 
+class WikiResource(object):
+    def __init__(self, request):
+        self.request = request
+
+    def get_page(self):
+        page_name = self.request.matchdict['page_name']
+        try:
+            page = Page.query.filter_by(page_name=page_name).one()
+            return page
+        except NoResultFound:
+            return None
+
+    def add(self, data):
+        DBSession.add(data)
+
 class Page(Base):
     query = DBSession.query_property()
     __tablename__ = 'pages'
             writer_name='html')
         return parts['html_body'] 
 
+def create_frontpage(engine):
+    sqlahelper.add_engine(engine)
+    Base.metadata.create_all(bind=DBSession.bind)
+    page = Page(page_name='FrontPage',
+        content='FrontPage\n_______________')
+    DBSession.add(page)
+    import transaction
+    try:
+        transaction.commit()
+    except IntegrityError:
+        transaction.abort()

File crosswiki/tests.py

 import unittest
 import mock
+import webtest
 
 from pyramid import testing
 
         from .views import show_page
         return show_page(*args, **kwargs)
 
-    @mock.patch('crosswiki.views.Page')
-    def test_it(self, MockPage):
+    def test_it(self):
         mockPage = mock.Mock()
         mockPage.page_name = u'this is page name'
         mockPage.html_content = u'this is content'
-        MockPage.query.filter_by = mock.Mock(return_value=mockPage)
-        request = testing.DummyRequest(matchdict=dict(page_name='FrontPage'))
+        context = mock.Mock()
+        context.get_page.return_value = mockPage
+        request = testing.DummyRequest(context=context, matchdict=dict(page_name='FrontPage'))
         result = self._callFUT(request)
-        MockPage.query.filter_by.assert_called_with(page_name='FrontPage')
+        context.get_page.assert_called_with()
 
         self.assertEqual(result['page_name'], 'this is page name')
         self.assertEqual(result['content'], 'this is content')
         self.assertEqual(created.page_name, 'FrontPage')
         self.assertEqual(created.content, 'this is content')
 
+class WikiResourceTests(unittest.TestCase):
+    def _getTarget(self):
+        from .models import WikiResource
+        return WikiResource
+
+    def _makeOne(self, *args, **kwargs):
+        return self._getTarget()(*args, **kwargs)
+
+    @mock.patch('crosswiki.models.Page')
+    def test_get_page(self, MockPage):
+        page = object()
+        mockQuery = mock.Mock()
+        mockQuery.one.return_value = page
+        MockPage.query.filter_by.return_value = mockQuery
+        request = testing.DummyRequest(matchdict=dict(page_name='FrontPage'))
+        target = self._makeOne(request)
+        result = target.get_page()
+        MockPage.query.filter_by.assert_called_with(page_name='FrontPage')
+        self.assertEqual(result, page)
+
+    @mock.patch('crosswiki.models.Page')
+    def test_get_page_no_result(self, MockPage):
+        mockQuery = mock.Mock()
+        from sqlalchemy.orm.exc import NoResultFound
+        mockQuery.one.side_effect = NoResultFound()
+        MockPage.query.filter_by.return_value = mockQuery
+        request = testing.DummyRequest(matchdict=dict(page_name='FrontPage'))
+        target = self._makeOne(request)
+        result = target.get_page()
+        self.assertIsNone(result)
 
 class PageUrlTests(unittest.TestCase):
     def setUp(self):
 """)
 
         self.assertEqual(target.html_content, '<div class="document" id="frontpage">\n<h1 class="title">FrontPage</h1>\n<p>this is FrontPage</p>\n</div>\n')
+
+class FunctionalTests(unittest.TestCase):
+    settings = {
+        'sqlalchemy.url': 'sqlite:///',
+        'mako.directories': 'crosswiki:templates',
+    }
+
+    def _makeAUT(self):
+        from . import main
+        app = main({}, **self.settings)
+        return webtest.TestApp(app)
+
+    def test_show(self):
+        app = self._makeAUT()
+        res = app.get('/FrontPage')
+        self.assertIn('FrontPage', res)
+
+    def test_create(self):
+        app = self._makeAUT()
+        res = app.get('/NewPage')
+        self.assertEqual(res.location, 'http://localhost/NewPage/create')
+        res = app.get(res.location)
+        res.form['content'].value = 'NewPage'
+        res = res.form.submit()
+        self.assertEqual(res.location, 'http://localhost/NewPage')
+        

File crosswiki/views.py

 from pyramid.view import view_config
-from pyramid.httpexceptions import HTTPFound
+from pyramid.httpexceptions import HTTPFound, HTTPNotFound
 from .models import Page
-from .helpers import page_url
+from .helpers import page_url, create_page_url
 
+@view_config(route_name='page', renderer='page.mak')
 def show_page(request):
     page_name = request.matchdict['page_name']
-    page = Page.query.filter_by(page_name=page_name)
+    page = request.context.get_page()
+    if page is None:
+        raise HTTPFound(location=create_page_url(request, page_name))
     return dict(page_name=page.page_name, content=page.html_content)
 
+@view_config(route_name='create_page', request_method='GET', renderer='create_page_form.mak')
+def create_page_form(request):
+    return dict()
+
+@view_config(route_name='create_page', request_method='POST')
 def create_page(request):
     page = Page(page_name=request.matchdict['page_name'],
         content=request.params['content'])