Source

crosswiki / crosswiki / tests.py

Full commit
import unittest
import mock
import webtest

from pyramid import testing


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

    def tearDown(self):
        testing.tearDown()

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

    def test_it(self):
        self.config.add_route('page', '/{page_name}')
        mockPage = mock.Mock()
        mockPage.page_name = 'FrontPage'
        context = mock.Mock()
        context.get_page.return_value = mockPage
        request = testing.DummyRequest(context=context, 
            matchdict=dict(page_name=mockPage.page_name),
            params=dict(content='updated'))
        result = self._callFUT(request)
        context.get_page.assert_called_with(mockPage.page_name)
        self.assertEqual(result.location, 'http://example.com/FrontPage')
        self.assertEqual(mockPage.content, 'updated')

    def test_not_found(self):
        context = mock.Mock()
        context.get_page.return_value = None
        request = testing.DummyRequest(context=context, 
            location='not_found_location',
            matchdict=dict(page_name='NoPage'),
            params=dict(content='updated'))

        from pyramid.httpexceptions import HTTPNotFound
        self.assertRaises(HTTPNotFound, self._callFUT, request)


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)

    def test_it(self):
        mockPage = mock.Mock()
        mockPage.page_name = u'this is page name'
        mockPage.html_content = u'this is content'
        context = mock.Mock()
        context.get_page.return_value = mockPage
        request = testing.DummyRequest(context=context, matchdict=dict(page_name='FrontPage'))
        result = self._callFUT(request)
        context.get_page.assert_called_with('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 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('FrontPage')
        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('FrontPage')
        self.assertIsNone(result)

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

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