dav_base: improved tests for RootView
This commit is contained in:
@@ -1,24 +1,68 @@
|
||||
from django.test import SimpleTestCase
|
||||
from unittest.mock import patch
|
||||
from django.test import SimpleTestCase, override_settings
|
||||
from django.urls import NoReverseMatch
|
||||
|
||||
from ..views import RootView
|
||||
|
||||
|
||||
class DummyModuleMeta:
|
||||
def __init__(self, package, url_namespace=None):
|
||||
self.package = package
|
||||
self.url_namespace = url_namespace
|
||||
|
||||
@property
|
||||
def url_name(self):
|
||||
if self.url_namespace:
|
||||
return self.url_namespace + ':root'
|
||||
else:
|
||||
return 'root'
|
||||
|
||||
class DummyModuleConfig:
|
||||
def __init__(self, modules):
|
||||
# modules: dict-like mapping name -> DummyMeta
|
||||
self._modules = modules
|
||||
|
||||
@property
|
||||
def modules(self):
|
||||
return self._modules
|
||||
|
||||
|
||||
class ViewsTestCase(SimpleTestCase):
|
||||
def test_root(self):
|
||||
view = RootView()
|
||||
template_names = view.get_template_names()
|
||||
self.assertEqual(len(template_names), 1)
|
||||
self.assertIn('dav_base/root.html', template_names)
|
||||
context = view.get_context_data()
|
||||
self.assertIn('root_urls', context)
|
||||
self.assertIsInstance(context['root_urls'], list)
|
||||
modules = {
|
||||
'module1': DummyModuleMeta('pkg1', url_namespace='ns1'),
|
||||
'module2': DummyModuleMeta('pkg2'),
|
||||
'moduleC': DummyModuleMeta('pkgC', url_namespace='nsC'),
|
||||
'moduleD': DummyModuleMeta('pkgD', url_namespace='nsD'),
|
||||
}
|
||||
expected_root_urls = [
|
||||
('pkg1', 'ns1:root'), ('pkg2', 'root'), ('pkgD', 'nsD:root')
|
||||
]
|
||||
|
||||
def fake_reverse(name):
|
||||
if name == 'nsC:root':
|
||||
raise NoReverseMatch()
|
||||
return '/'
|
||||
|
||||
with override_settings(MODULE_CONFIG=DummyModuleConfig(modules)):
|
||||
with patch('dav_base.views.reverse', side_effect=fake_reverse) as mocked_reverse:
|
||||
view = RootView()
|
||||
template_names = view.get_template_names()
|
||||
self.assertEqual(len(template_names), 1)
|
||||
self.assertIn('dav_base/root.html', template_names)
|
||||
context = view.get_context_data()
|
||||
self.assertIn('root_urls', context)
|
||||
self.assertIsInstance(context['root_urls'], list)
|
||||
self.assertEqual(context['root_urls'], expected_root_urls)
|
||||
called_names = [call.args[0] for call in mocked_reverse.call_args_list]
|
||||
self.assertEqual(len(called_names), len(modules))
|
||||
for m in modules.values():
|
||||
self.assertIn(m.url_name, called_names)
|
||||
|
||||
def test_integrated_root(self):
|
||||
response = self.client.get('/')
|
||||
self.assertTemplateUsed(response, 'dav_base/root.html')
|
||||
self.assertIn('root_urls', response.context, '\'root_urls\' not in context of root view')
|
||||
self.assertIsInstance(response.context['root_urls'], list)
|
||||
|
||||
# TODO
|
||||
# Maybe we should set a defined module config, so we could
|
||||
# test the content of the root_urls context variable.
|
||||
with override_settings(MODULE_CONFIG=DummyModuleConfig({})):
|
||||
response = self.client.get('/')
|
||||
self.assertTemplateUsed(response, 'dav_base/root.html')
|
||||
self.assertIn('root_urls', response.context, '\'root_urls\' not in context of root view')
|
||||
self.assertIsInstance(response.context['root_urls'], list)
|
||||
self.assertEqual(response.context['root_urls'], [])
|
||||
|
||||
Reference in New Issue
Block a user