tailbone/tests/views/test_autocomplete.py

96 lines
3.1 KiB
Python
Raw Permalink Normal View History

2013-06-20 00:23:15 -05:00
from mock import Mock
from pyramid import testing
2013-07-22 21:22:18 -05:00
from .. import TestCase, mock_query
2013-09-01 09:27:47 -05:00
from tailbone.views import autocomplete
2013-06-20 00:23:15 -05:00
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)
2014-02-16 00:39:27 -06:00
self.assertTrue(filtered is query)
2013-06-20 00:23:15 -05:00
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')
2014-02-16 00:39:27 -06:00
self.assertTrue(filtered is query)
2013-06-20 00:23:15 -05:00
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'
2014-02-16 00:39:27 -06:00
self.assertTrue(view.make_query('test') is self.query)
2013-06-20 00:23:15 -05:00
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'},
])