Added some tests.
This commit is contained in:
parent
84225f00e1
commit
d07aee345a
61
rattail/pyramid/forms/simpleform.py
Normal file
61
rattail/pyramid/forms/simpleform.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2012 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
# Rattail is free software: you can redistribute it and/or modify it under the
|
||||
# terms of the GNU Affero General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option)
|
||||
# any later version.
|
||||
#
|
||||
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
||||
# more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
################################################################################
|
||||
|
||||
"""
|
||||
``rattail.pyramid.forms.simpleform`` -- ``pyramid_simpleform`` Forms
|
||||
"""
|
||||
|
||||
from pyramid_simpleform import renderers
|
||||
|
||||
from webhelpers.html import tags
|
||||
from webhelpers.html import HTML
|
||||
|
||||
from edbob.util import prettify
|
||||
|
||||
|
||||
__all__ = ['FormRenderer']
|
||||
|
||||
|
||||
class FormRenderer(renderers.FormRenderer):
|
||||
"""
|
||||
Customized form renderer. Provides some extra methods for convenience.
|
||||
"""
|
||||
|
||||
def field_div(self, name, field, label=None):
|
||||
errors = self.errors_for(name)
|
||||
if errors:
|
||||
errors = [HTML.tag('div', class_='field-error', c=x) for x in errors]
|
||||
errors = tags.literal('').join(errors)
|
||||
|
||||
label = HTML.tag('label', for_=name, c=label or prettify(name))
|
||||
inner = HTML.tag('div', class_='field', c=field)
|
||||
|
||||
outer_class = 'field-wrapper'
|
||||
if errors:
|
||||
outer_class += ' error'
|
||||
outer = HTML.tag('div', class_=outer_class, c=(errors or '') + label + inner)
|
||||
return outer
|
||||
|
||||
def referrer_field(self):
|
||||
return self.hidden('referrer', value=self.form.request.get_referrer())
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
import unittest
|
||||
from mock import Mock
|
||||
|
||||
from pyramid import testing
|
||||
|
||||
|
@ -22,6 +23,21 @@ class TestCase(unittest.TestCase):
|
|||
testing.tearDown()
|
||||
|
||||
|
||||
def mock_query():
|
||||
"""
|
||||
Mock object used to simulate a ``sqlalchemy.Query`` instance.
|
||||
"""
|
||||
|
||||
query = Mock()
|
||||
query.return_value = query
|
||||
query.outerjoin.return_value = query
|
||||
query.join.return_value = query
|
||||
query.filter.return_value = query
|
||||
query.distinct.return_value = query
|
||||
query.order_by.return_value = query
|
||||
return query
|
||||
|
||||
|
||||
# class DataTestCase(TestCase):
|
||||
# """
|
||||
# Base class for all test suites which require fixture data.
|
||||
|
|
0
rattail/pyramid/tests/forms/__init__.py
Normal file
0
rattail/pyramid/tests/forms/__init__.py
Normal file
25
rattail/pyramid/tests/forms/test_simpleform.py
Normal file
25
rattail/pyramid/tests/forms/test_simpleform.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
from mock import Mock
|
||||
|
||||
from rattail.pyramid.tests import TestCase
|
||||
from rattail.pyramid.forms import simpleform
|
||||
|
||||
|
||||
class FormRendererTests(TestCase):
|
||||
|
||||
def test_field_div(self):
|
||||
form = Mock()
|
||||
form.errors_for = Mock(return_value=[])
|
||||
renderer = simpleform.FormRenderer(form)
|
||||
renderer.field_div('test', '<p>test</p>')
|
||||
|
||||
def test_field_div_with_errors(self):
|
||||
form = Mock()
|
||||
form.errors_for = Mock(return_value=["Bogus testing error."])
|
||||
renderer = simpleform.FormRenderer(form)
|
||||
renderer.field_div('test', '<p>test</p>')
|
||||
|
||||
def test_referrer_field(self):
|
||||
form = Mock()
|
||||
renderer = simpleform.FormRenderer(form)
|
||||
renderer.referrer_field()
|
|
@ -1,46 +0,0 @@
|
|||
|
||||
from pyramid import testing
|
||||
|
||||
from rattail.pyramid.tests import TestCase
|
||||
from rattail.pyramid.views import departments
|
||||
|
||||
from rattail.db.model import Department
|
||||
|
||||
|
||||
class DepartmentTests(TestCase):
|
||||
"""
|
||||
Test Department views.
|
||||
"""
|
||||
|
||||
def test_grid(self):
|
||||
request = testing.DummyRequest(has_perm=lambda p: True)
|
||||
view = departments.DepartmentsGrid(request)
|
||||
view._data = None
|
||||
view._sort_config = None
|
||||
self.assertIsInstance(view, departments.DepartmentsGrid)
|
||||
self.assertIsInstance(view.filter_map(), dict)
|
||||
self.assertIsInstance(view.filter_config(), dict)
|
||||
self.assertIsInstance(view.sort_map(), dict)
|
||||
self.assertIsInstance(view.grid(), object)
|
||||
|
||||
def test_crud(self):
|
||||
request = testing.DummyRequest()
|
||||
view = departments.DepartmentCrud(request)
|
||||
self.assertIsInstance(view, departments.DepartmentCrud)
|
||||
self.assertIsInstance(view.fieldset(Department), object)
|
||||
|
||||
def test_autocomplete(self):
|
||||
request = testing.DummyRequest()
|
||||
view = departments.DepartmentsAutocomplete(request)
|
||||
self.assertIsInstance(view, departments.DepartmentsAutocomplete)
|
||||
|
||||
def test_vendor_grid(self):
|
||||
request = testing.DummyRequest(params={'uuid': 'bogus'})
|
||||
view = departments.DepartmentsByVendorGrid(request)
|
||||
view._data = None
|
||||
self.assertIsInstance(view, departments.DepartmentsByVendorGrid)
|
||||
self.assertIsInstance(view.query(), object)
|
||||
self.assertIsInstance(view.grid(), object)
|
||||
|
||||
def test_includeme(self):
|
||||
self.config.include(departments)
|
11
rattail/pyramid/tests/test_helpers.py
Normal file
11
rattail/pyramid/tests/test_helpers.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
|
||||
import datetime
|
||||
|
||||
from rattail.pyramid.tests import TestCase
|
||||
from rattail.pyramid import helpers
|
||||
|
||||
|
||||
class HelpersTests(TestCase):
|
||||
|
||||
def test_pretty_date(self):
|
||||
helpers.pretty_date(datetime.date(2013, 6, 19))
|
13
rattail/pyramid/tests/test_subscribers.py
Normal file
13
rattail/pyramid/tests/test_subscribers.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
|
||||
from mock import Mock
|
||||
|
||||
from rattail.pyramid.tests import TestCase
|
||||
from rattail.pyramid import subscribers
|
||||
|
||||
|
||||
class SubscribersTests(TestCase):
|
||||
|
||||
def test_before_render(self):
|
||||
event = Mock()
|
||||
event.__setitem__ = Mock()
|
||||
subscribers.before_render(event)
|
0
rattail/pyramid/tests/views/__init__.py
Normal file
0
rattail/pyramid/tests/views/__init__.py
Normal file
95
rattail/pyramid/tests/views/test_autocomplete.py
Normal file
95
rattail/pyramid/tests/views/test_autocomplete.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
|
||||
from mock import Mock
|
||||
from pyramid import testing
|
||||
|
||||
from rattail.pyramid.tests import TestCase, mock_query
|
||||
from rattail.pyramid.views import autocomplete
|
||||
|
||||
|
||||
class BareAutocompleteViewTests(TestCase):
|
||||
|
||||
def view(self, **kwargs):
|
||||
request = testing.DummyRequest(**kwargs)
|
||||
return autocomplete.AutocompleteView(request)
|
||||
|
||||
def test_attributes(self):
|
||||
view = self.view()
|
||||
self.assertRaises(AttributeError, getattr, view, 'mapped_class')
|
||||
self.assertRaises(AttributeError, getattr, view, 'fieldname')
|
||||
|
||||
def test_filter_query(self):
|
||||
view = self.view()
|
||||
query = Mock()
|
||||
filtered = view.filter_query(query)
|
||||
self.assertIs(filtered, query)
|
||||
|
||||
def test_make_query(self):
|
||||
view = self.view()
|
||||
# No mapped_class defined for view.
|
||||
self.assertRaises(AttributeError, view.make_query, 'test')
|
||||
|
||||
def test_query(self):
|
||||
view = self.view()
|
||||
query = Mock()
|
||||
view.make_query = Mock(return_value=query)
|
||||
filtered = view.query('test')
|
||||
self.assertIs(filtered, query)
|
||||
|
||||
def test_display(self):
|
||||
view = self.view()
|
||||
instance = Mock()
|
||||
# No fieldname defined for view.
|
||||
self.assertRaises(AttributeError, view.display, instance)
|
||||
|
||||
def test_call(self):
|
||||
# Empty or missing query term yields empty list.
|
||||
view = self.view(params={})
|
||||
self.assertEqual(view(), [])
|
||||
view = self.view(params={'term': None})
|
||||
self.assertEqual(view(), [])
|
||||
view = self.view(params={'term': ''})
|
||||
self.assertEqual(view(), [])
|
||||
view = self.view(params={'term': '\t'})
|
||||
self.assertEqual(view(), [])
|
||||
# No mapped_class defined for view.
|
||||
view = self.view(params={'term': 'bogus'})
|
||||
self.assertRaises(AttributeError, view)
|
||||
|
||||
|
||||
class SampleAutocompleteViewTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(SampleAutocompleteViewTests, self).setUp()
|
||||
self.Session_query = autocomplete.Session.query
|
||||
self.query = mock_query()
|
||||
autocomplete.Session.query = self.query
|
||||
|
||||
def tearDown(self):
|
||||
super(SampleAutocompleteViewTests, self).tearDown()
|
||||
autocomplete.Session.query = self.Session_query
|
||||
|
||||
def view(self, **kwargs):
|
||||
request = testing.DummyRequest(**kwargs)
|
||||
view = autocomplete.AutocompleteView(request)
|
||||
view.mapped_class = Mock()
|
||||
view.fieldname = 'thing'
|
||||
return view
|
||||
|
||||
def test_make_query(self):
|
||||
view = self.view()
|
||||
view.mapped_class.thing.ilike.return_value = 'whatever'
|
||||
self.assertIs(view.make_query('test'), self.query)
|
||||
view.mapped_class.thing.ilike.assert_called_with('%test%')
|
||||
self.query.filter.assert_called_with('whatever')
|
||||
self.query.order_by.assert_called_with(view.mapped_class.thing)
|
||||
|
||||
def test_call(self):
|
||||
self.query.all.return_value = [
|
||||
Mock(uuid='1', thing='first'),
|
||||
Mock(uuid='2', thing='second'),
|
||||
]
|
||||
view = self.view(params={'term': 'bogus'})
|
||||
self.assertEqual(view(), [
|
||||
{'label': 'first', 'value': '1'},
|
||||
{'label': 'second', 'value': '2'},
|
||||
])
|
93
rattail/pyramid/tests/views/test_departments.py
Normal file
93
rattail/pyramid/tests/views/test_departments.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
|
||||
from mock import Mock
|
||||
from pyramid import testing
|
||||
|
||||
from rattail.pyramid.tests import TestCase, mock_query
|
||||
from rattail.pyramid.views import departments
|
||||
|
||||
|
||||
class DepartmentsGridTests(TestCase):
|
||||
|
||||
def view(self, **kwargs):
|
||||
request = testing.DummyRequest(**kwargs)
|
||||
return departments.DepartmentsGrid(request)
|
||||
|
||||
def test_filter_map(self):
|
||||
view = self.view()
|
||||
view.make_filter_map = Mock(return_value='gotcha')
|
||||
self.assertEqual(view.filter_map(), 'gotcha')
|
||||
view.make_filter_map.assert_called_with(ilike=['name'])
|
||||
|
||||
def test_filter_config(self):
|
||||
view = self.view()
|
||||
view.make_filter_config = Mock(return_value='yep')
|
||||
self.assertEqual(view.filter_config(), 'yep')
|
||||
view.make_filter_config.assert_called_with(
|
||||
include_filter_name=True, filter_type_name='lk')
|
||||
|
||||
def test_sort_map(self):
|
||||
view = self.view()
|
||||
view.make_sort_map = Mock(return_value='really')
|
||||
self.assertEqual(view.sort_map(), 'really')
|
||||
view.make_sort_map.assert_called_with('number', 'name')
|
||||
|
||||
def test_grid_allowed(self):
|
||||
view = self.view()
|
||||
view.request.has_perm = Mock(return_value=True)
|
||||
view.make_grid = Mock()
|
||||
g = view.grid()
|
||||
self.assertTrue(g.clickable)
|
||||
self.assertEqual(g.click_route_name, 'department.read')
|
||||
self.assertTrue(g.editable)
|
||||
self.assertEqual(g.edit_route_name, 'department.update')
|
||||
self.assertTrue(g.deletable)
|
||||
self.assertEqual(g.delete_route_name, 'department.delete')
|
||||
|
||||
def test_grid_restricted(self):
|
||||
view = self.view()
|
||||
view.request.has_perm = Mock(return_value=False)
|
||||
grid = Mock(
|
||||
clickable=False, click_route_name=None,
|
||||
editable=False, edit_route_name=None,
|
||||
deletable=False, delete_route_name=None)
|
||||
view.make_grid = Mock(return_value=grid)
|
||||
g = view.grid()
|
||||
self.assertFalse(g.clickable)
|
||||
self.assertEqual(g.click_route_name, None)
|
||||
self.assertFalse(g.editable)
|
||||
self.assertEqual(g.edit_route_name, None)
|
||||
self.assertFalse(g.deletable)
|
||||
self.assertEqual(g.delete_route_name, None)
|
||||
|
||||
|
||||
class DepartmentCrudTests(TestCase):
|
||||
|
||||
def view(self, **kwargs):
|
||||
request = testing.DummyRequest(**kwargs)
|
||||
return departments.DepartmentCrud(request)
|
||||
|
||||
def test_fieldset(self):
|
||||
view = self.view()
|
||||
fieldset = Mock()
|
||||
view.make_fieldset = Mock(return_value=fieldset)
|
||||
fs = view.fieldset(Mock())
|
||||
self.assertIs(fs, fieldset)
|
||||
|
||||
|
||||
class DepartmentsByVendorGridTests(TestCase):
|
||||
|
||||
def view(self, **kwargs):
|
||||
request = testing.DummyRequest(**kwargs)
|
||||
return departments.DepartmentsByVendorGrid(request)
|
||||
|
||||
def test_query(self):
|
||||
query = mock_query()
|
||||
view = self.view(params={'uuid': '1'})
|
||||
view.make_query = Mock(return_value=query)
|
||||
self.assertIs(view.query(), query)
|
||||
|
||||
def test_grid(self):
|
||||
view = self.view()
|
||||
grid = Mock()
|
||||
view.make_grid = Mock(return_value=grid)
|
||||
self.assertIs(view.grid(), grid)
|
Loading…
Reference in a new issue