Skip to content

Commit

Permalink
Add test for direct call to a view
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Feb 14, 2018
1 parent c99971b commit 3346323
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion coaster/views/classview.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""

from __future__ import unicode_literals
import types

__all__ = ['route', 'ClassView', 'ModelView']

Expand Down Expand Up @@ -125,6 +124,7 @@ def __init__(self, viewd, obj, cls=None):
self.__cls = cls

def __call__(self, *args, **kwargs):
"""Treat this like a call to the method (and not to the view)"""
return self.__viewd.func(self.__obj, *args, **kwargs)

def __getattr__(self, attr):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_views_classview.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def test_document_view(self):
doc = ViewDocument(name='test1', title="Test")
self.session.add(doc)
self.session.commit()

rv = self.client.get('/doc/test1')
assert rv.status_code == 200
data = json.loads(rv.data)
Expand All @@ -171,13 +172,30 @@ def test_document_edit(self):
doc = ViewDocument(name='test1', title="Test")
self.session.add(doc)
self.session.commit()

self.client.post('/doc/test1/edit', data={'title': "Edit 1"})
assert doc.title == "Edit 1"
self.client.post('/edit/test1', data={'title': "Edit 2"})
assert doc.title == "Edit 2"
self.client.post('/doc/test1', data={'title': "Edit 3"})
assert doc.title == "Edit 3"

def test_callable_view(self):
"""View handlers are callable as regular methods"""
doc = ViewDocument(name='test1', title="Test")
self.session.add(doc)
self.session.commit()

rv = DocumentView().view('test1')
assert rv.status_code == 200
data = json.loads(rv.data)
assert data['name'] == 'test1'
assert data['title'] == "Test"

rv = DocumentView().edit('test1', "Edited")
assert rv == 'edited!'
assert doc.title == "Edited"

def test_rerouted(self):
"""Subclass replaces view handler"""
rv = self.client.get('/subclasstest')
Expand Down

0 comments on commit 3346323

Please sign in to comment.