Commits

Chris McDonough committed ec5cdae

normalize tests

Comments (0)

Files changed (3)

src/authorization/tutorial/tests.py

 import unittest
 
 from pylons.configuration import Configurator
+from repoze.bfg import testing
 
-class MyControllerTests(unittest.TestCase):
+class TestWikiHandler(unittest.TestCase):
     def setUp(self):
+        self.session = self._initTestingDB()
         self.config = Configurator()
         self.config.begin()
+        self.config.add_route('root', '/')
+        self.config.add_route('wiki_action', '/wiki/{pagename}/{action}')
 
     def tearDown(self):
+        self.session.remove()
         self.config.end()
 
+    def _initTestingDB(self):
+        from tutorial.models import DBSession
+        from tutorial.models import Base
+        from sqlalchemy import create_engine
+        engine = create_engine('sqlite://')
+        DBSession.configure(bind=engine)
+        Base.metadata.bind = engine
+        Base.metadata.create_all(engine)
+        return DBSession
+
     def _makeOne(self, request):
-        from tutorial.views import MyController
-        return MyController(request)
+        from tutorial.handlers import WikiHandler
+        return WikiHandler(request)
+        
+    def test___call__(self):
+        request = testing.DummyRequest()
+        handler = self._makeOne(request)
+        response = handler()
+        self.assertEqual(response.location,
+                         'http://example.com/wiki/FrontPage/view')
 
-    def test_index(self):
-        request = DummyRequest()
-        controller = self._makeOne(request)
-        info = controller.index()
-        self.assertEqual(info['project'], 'tutorial')
+    def test_view(self):
+        request = testing.DummyRequest()
+        handler = self._makeOne(request)
+        from tutorial.models import Page
+        request.matchdict['pagename'] = 'IDoExist'
+        page = Page('IDoExist', 'Hello CruelWorld IDoExist')
+        self.session.add(page)
+        info = handler.view()
+        self.assertEqual(info['page'], page)
+        self.assertEqual(
+            info['content'], 
+            '<div class="document">\n'
+            '<p>Hello <a href="http://example.com/wiki/CruelWorld/add">'
+            'CruelWorld</a> '
+            '<a href="http://example.com/wiki/IDoExist/view">'
+            'IDoExist</a>'
+            '</p>\n</div>\n')
+        self.assertEqual(info['edit_url'],
+                         'http://example.com/wiki/IDoExist/edit')
+        
+    
+    def test_add_notsubmitted(self):
+        request = testing.DummyRequest()
+        request.matchdict = {'pagename':'AnotherPage'}
+        handler = self._makeOne(request)
+        info = handler.add()
+        self.assertEqual(info['page'].data,'')
+        self.assertEqual(info['save_url'],
+                         'http://example.com/wiki/AnotherPage/add')
+        
+    def test_add_submitted(self):
+        from tutorial.models import Page
+        request = testing.DummyRequest({'form.submitted':True,
+                                        'body':'Hello yo!'})
+        request.matchdict = {'pagename':'AnotherPage'}
+        handler = self._makeOne(request)
+        handler.add()
+        page = self.session.query(Page).filter_by(name='AnotherPage').one()
+        self.assertEqual(page.data, 'Hello yo!')
 
-class DummyRequest(object):
-    pass
+    def test_edit_notsubmitted(self):
+        from tutorial.models import Page
+        request = testing.DummyRequest()
+        request.matchdict = {'pagename':'abc'}
+        page = Page('abc', 'hello')
+        self.session.add(page)
+        handler = self._makeOne(request)
+        info = handler.edit()
+        self.assertEqual(info['page'], page)
+        self.assertEqual(info['save_url'],
+                         'http://example.com/wiki/abc/edit')
+        
+    def test_edit_submitted(self):
+        from tutorial.models import Page
+        request = testing.DummyRequest({'form.submitted':True,
+                                        'body':'Hello yo!'})
+        request.matchdict = {'pagename':'abc'}
+        page = Page('abc', 'hello')
+        self.session.add(page)
+        handler = self._makeOne(request)
+        response = handler.edit()
+        self.assertEqual(response.location, 'http://example.com/wiki/abc/view')
+        self.assertEqual(page.data, 'Hello yo!')

src/layout/tutorial/tests.py

 import unittest
 
-class WikiHandlerTests(unittest.TestCase):
+class MyHandlerTests(unittest.TestCase):
     def setUp(self):
         from pylons.configuration import Configurator
         from tutorial.models import initialize_sql

src/models/tutorial/tests.py

 import unittest
 
-class WikiHandlerTests(unittest.TestCase):
+class MyHandlerTests(unittest.TestCase):
     def setUp(self):
         from pylons.configuration import Configurator
         from tutorial.models import initialize_sql