PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/views/tutorial/tests.py

https://bitbucket.org/blaflamme/pylonswiking
Python | 101 lines | 92 code | 9 blank | 0 comment | 0 complexity | f083565c6c3cf6015d5f0eb7feff9ee9 MD5 | raw file
  1import unittest
  2
  3from pylons.configuration import Configurator
  4from repoze.bfg import testing
  5
  6class TestWikiHandler(unittest.TestCase):
  7    def setUp(self):
  8        self.session = self._initTestingDB()
  9        self.config = Configurator()
 10        self.config.begin()
 11        self.config.add_route('root', '/')
 12        self.config.add_route('wiki_action', '/wiki/{pagename}/{action}')
 13
 14    def tearDown(self):
 15        self.session.remove()
 16        self.config.end()
 17
 18    def _initTestingDB(self):
 19        from tutorial.models import DBSession
 20        from tutorial.models import Base
 21        from sqlalchemy import create_engine
 22        engine = create_engine('sqlite://')
 23        DBSession.configure(bind=engine)
 24        Base.metadata.bind = engine
 25        Base.metadata.create_all(engine)
 26        return DBSession
 27
 28    def _makeOne(self, request):
 29        from tutorial.handlers import WikiHandler
 30        return WikiHandler(request)
 31        
 32    def test___call__(self):
 33        request = testing.DummyRequest()
 34        handler = self._makeOne(request)
 35        response = handler()
 36        self.assertEqual(response.location,
 37                         'http://example.com/wiki/FrontPage/view')
 38
 39    def test_view(self):
 40        request = testing.DummyRequest()
 41        handler = self._makeOne(request)
 42        from tutorial.models import Page
 43        request.matchdict['pagename'] = 'IDoExist'
 44        page = Page('IDoExist', 'Hello CruelWorld IDoExist')
 45        self.session.add(page)
 46        info = handler.view()
 47        self.assertEqual(info['page'], page)
 48        self.assertEqual(
 49            info['content'], 
 50            '<div class="document">\n'
 51            '<p>Hello <a href="http://example.com/wiki/CruelWorld/add">'
 52            'CruelWorld</a> '
 53            '<a href="http://example.com/wiki/IDoExist/view">'
 54            'IDoExist</a>'
 55            '</p>\n</div>\n')
 56        self.assertEqual(info['edit_url'],
 57                         'http://example.com/wiki/IDoExist/edit')
 58        
 59    
 60    def test_add_notsubmitted(self):
 61        request = testing.DummyRequest()
 62        request.matchdict = {'pagename':'AnotherPage'}
 63        handler = self._makeOne(request)
 64        info = handler.add()
 65        self.assertEqual(info['page'].data,'')
 66        self.assertEqual(info['save_url'],
 67                         'http://example.com/wiki/AnotherPage/add')
 68        
 69    def test_add_submitted(self):
 70        from tutorial.models import Page
 71        request = testing.DummyRequest({'form.submitted':True,
 72                                        'body':'Hello yo!'})
 73        request.matchdict = {'pagename':'AnotherPage'}
 74        handler = self._makeOne(request)
 75        handler.add()
 76        page = self.session.query(Page).filter_by(name='AnotherPage').one()
 77        self.assertEqual(page.data, 'Hello yo!')
 78
 79    def test_edit_notsubmitted(self):
 80        from tutorial.models import Page
 81        request = testing.DummyRequest()
 82        request.matchdict = {'pagename':'abc'}
 83        page = Page('abc', 'hello')
 84        self.session.add(page)
 85        handler = self._makeOne(request)
 86        info = handler.edit()
 87        self.assertEqual(info['page'], page)
 88        self.assertEqual(info['save_url'],
 89                         'http://example.com/wiki/abc/edit')
 90        
 91    def test_edit_submitted(self):
 92        from tutorial.models import Page
 93        request = testing.DummyRequest({'form.submitted':True,
 94                                        'body':'Hello yo!'})
 95        request.matchdict = {'pagename':'abc'}
 96        page = Page('abc', 'hello')
 97        self.session.add(page)
 98        handler = self._makeOne(request)
 99        response = handler.edit()
100        self.assertEqual(response.location, 'http://example.com/wiki/abc/view')
101        self.assertEqual(page.data, 'Hello yo!')