diff --git a/rattail/pyramid/forms/simpleform.py b/rattail/pyramid/forms/simpleform.py
new file mode 100644
index 00000000..ab186952
--- /dev/null
+++ b/rattail/pyramid/forms/simpleform.py
@@ -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
test
') + + 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', 'test
') + + def test_referrer_field(self): + form = Mock() + renderer = simpleform.FormRenderer(form) + renderer.referrer_field() diff --git a/rattail/pyramid/tests/test_departments.py b/rattail/pyramid/tests/test_departments.py deleted file mode 100644 index dfbc909e..00000000 --- a/rattail/pyramid/tests/test_departments.py +++ /dev/null @@ -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) diff --git a/rattail/pyramid/tests/test_helpers.py b/rattail/pyramid/tests/test_helpers.py new file mode 100644 index 00000000..eab4bf24 --- /dev/null +++ b/rattail/pyramid/tests/test_helpers.py @@ -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)) diff --git a/rattail/pyramid/tests/test_subscribers.py b/rattail/pyramid/tests/test_subscribers.py new file mode 100644 index 00000000..fc833c05 --- /dev/null +++ b/rattail/pyramid/tests/test_subscribers.py @@ -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) diff --git a/rattail/pyramid/tests/views/__init__.py b/rattail/pyramid/tests/views/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/rattail/pyramid/tests/views/test_autocomplete.py b/rattail/pyramid/tests/views/test_autocomplete.py new file mode 100644 index 00000000..0264314e --- /dev/null +++ b/rattail/pyramid/tests/views/test_autocomplete.py @@ -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'}, + ]) diff --git a/rattail/pyramid/tests/views/test_departments.py b/rattail/pyramid/tests/views/test_departments.py new file mode 100644 index 00000000..1820b786 --- /dev/null +++ b/rattail/pyramid/tests/views/test_departments.py @@ -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) diff --git a/setup.py b/setup.py index b905fbf6..0a880d70 100644 --- a/setup.py +++ b/setup.py @@ -94,7 +94,7 @@ setup( ], install_requires = requires, - tests_require = requires + ['nose', 'coverage'], + tests_require = requires + ['mock', 'nose', 'coverage'], test_suite = 'nose.collector', namespace_packages = ['rattail'],