dav_base: improved tests for RootView
This commit is contained in:
@@ -1,10 +1,51 @@
|
|||||||
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
|
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):
|
class ViewsTestCase(SimpleTestCase):
|
||||||
def test_root(self):
|
def test_root(self):
|
||||||
|
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()
|
view = RootView()
|
||||||
template_names = view.get_template_names()
|
template_names = view.get_template_names()
|
||||||
self.assertEqual(len(template_names), 1)
|
self.assertEqual(len(template_names), 1)
|
||||||
@@ -12,13 +53,16 @@ class ViewsTestCase(SimpleTestCase):
|
|||||||
context = view.get_context_data()
|
context = view.get_context_data()
|
||||||
self.assertIn('root_urls', context)
|
self.assertIn('root_urls', context)
|
||||||
self.assertIsInstance(context['root_urls'], list)
|
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):
|
def test_integrated_root(self):
|
||||||
|
with override_settings(MODULE_CONFIG=DummyModuleConfig({})):
|
||||||
response = self.client.get('/')
|
response = self.client.get('/')
|
||||||
self.assertTemplateUsed(response, 'dav_base/root.html')
|
self.assertTemplateUsed(response, 'dav_base/root.html')
|
||||||
self.assertIn('root_urls', response.context, '\'root_urls\' not in context of root view')
|
self.assertIn('root_urls', response.context, '\'root_urls\' not in context of root view')
|
||||||
self.assertIsInstance(response.context['root_urls'], list)
|
self.assertIsInstance(response.context['root_urls'], list)
|
||||||
|
self.assertEqual(response.context['root_urls'], [])
|
||||||
# TODO
|
|
||||||
# Maybe we should set a defined module config, so we could
|
|
||||||
# test the content of the root_urls context variable.
|
|
||||||
|
|||||||
Reference in New Issue
Block a user