Source

crosswiki / crosswiki / tests.py

Full commit
import unittest
import mock

from pyramid import testing


class ShowPageTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    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 CreatePageTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def _callFUT(self, *args, **kwargs):
        from .views import create_page
        return create_page(*args, **kwargs)

    def test_it(self):
        self.config.add_route('page', '/{page_name}')
        context = mock.Mock()
        request = testing.DummyRequest(context=context, matchdict=dict(page_name='FrontPage'),
            params=dict(content='this is content'))
        result = self._callFUT(request)

        self.assertEqual(result.location, 'http://example.com/FrontPage')
        created = context.add.call_args[0][0]
        self.assertEqual(created.page_name, 'FrontPage')
        self.assertEqual(created.content, 'this is content')


class PageUrlTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def _callFUT(self, *args, **kwargs):
        from .helpers import page_url
        return page_url(*args, **kwargs)

    def test_it(self):
        self.config.add_route('page', '/{page_name}')
        request = testing.DummyRequest()
        page = mock.Mock()
        page.page_name = 'FrontPage'
        result = self._callFUT(request, page)
        self.assertEqual(result, 'http://example.com/FrontPage')

class PageTests(unittest.TestCase):
    def _getTarget(self):
        from .models import Page
        return Page

    def _makeOne(self, *args, **kwargs):
        return self._getTarget()(*args, **kwargs)

    def test_html_content(self):
        target = self._makeOne(page_name=u'FrontPage',
            content=u"""\
FrontPage
---------------------

this is FrontPage
""")

        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')