Commits

Atsushi Odagiri committed 85dc056

show page

  • Participants
  • Parent commits 3c17012

Comments (0)

Files changed (3)

File crosswiki/models.py

 DBSession = sqlahelper.get_session()
 
 class Page(Base):
+    query = DBSession.query_property()
     __tablename__ = 'pages'
     id = sa.Column(sa.Integer, primary_key=True)
     page_name = sa.Column(sa.Unicode(255), unique=True)

File crosswiki/tests.py

 import unittest
+import mock
 
 from pyramid import testing
 
-class ViewTests(unittest.TestCase):
+
+class ShowPageTests(unittest.TestCase):
     def setUp(self):
         self.config = testing.setUp()
 
     def tearDown(self):
         testing.tearDown()
 
-    def test_my_view(self):
-        from .views import my_view
-        request = testing.DummyRequest()
-        info = my_view(request)
-        self.assertEqual(info['project'], 'crosswiki')
+    def _callFUT(self, *args, **kwargs):
+        from .views import show_page
+        return show_page(*args, **kwargs)
+
+    @mock.patch('crosswiki.views.Page')
+    def test_it(self, MockPage):
+        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'))
+        result = self._callFUT(request)
+        MockPage.query.filter_by.assert_called_with(page_name='FrontPage')
+
+        self.assertEqual(result['page_name'], 'this is page name')
+        self.assertEqual(result['content'], 'this is content')
+
 
 class PageTests(unittest.TestCase):
     def _getTarget(self):
         from .models import Page
         return Page
+
     def _makeOne(self, *args, **kwargs):
         return self._getTarget()(*args, **kwargs)
 

File crosswiki/views.py

 from pyramid.view import view_config
+from .models import Page
 
-@view_config(route_name='home', renderer='templates/mytemplate.pt')
-def my_view(request):
-    return {'project':'crosswiki'}
+def show_page(request):
+    page_name = request.matchdict['page_name']
+    page = Page.query.filter_by(page_name=page_name)
+    return dict(page_name=page.page_name, content=page.html_content)