2024-08-05 21:49:18 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
2024-08-11 12:41:22 -05:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2024-08-18 14:05:10 -05:00
|
|
|
import colander
|
2024-08-07 19:47:24 -05:00
|
|
|
from pyramid.httpexceptions import HTTPNotFound
|
|
|
|
|
2024-08-18 14:05:10 -05:00
|
|
|
from wuttaweb.views import settings as mod
|
2024-08-12 21:17:08 -05:00
|
|
|
from tests.util import WebTestCase
|
2024-08-05 21:49:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TestAppInfoView(WebTestCase):
|
|
|
|
|
2024-08-14 15:10:54 -05:00
|
|
|
def setUp(self):
|
|
|
|
self.setup_web()
|
|
|
|
self.pyramid_config.include('wuttaweb.views.essential')
|
|
|
|
|
2024-08-07 14:00:53 -05:00
|
|
|
def make_view(self):
|
2024-08-18 14:05:10 -05:00
|
|
|
return mod.AppInfoView(self.request)
|
2024-08-07 14:00:53 -05:00
|
|
|
|
2024-08-20 21:26:38 -05:00
|
|
|
def test_get_grid_data(self):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# empty data by default
|
|
|
|
data = view.get_grid_data()
|
|
|
|
self.assertEqual(data, [])
|
|
|
|
|
|
|
|
# 'partial' request returns data
|
|
|
|
self.request.GET = {'partial': '1'}
|
|
|
|
data = view.get_grid_data()
|
|
|
|
self.assertIsInstance(data, list)
|
|
|
|
self.assertTrue(data)
|
|
|
|
|
2024-08-05 21:49:18 -05:00
|
|
|
def test_index(self):
|
2024-08-06 19:59:30 -05:00
|
|
|
# sanity/coverage check
|
2024-08-07 14:00:53 -05:00
|
|
|
view = self.make_view()
|
2024-08-05 21:49:18 -05:00
|
|
|
response = view.index()
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
def test_configure_get_simple_settings(self):
|
2024-08-06 19:59:30 -05:00
|
|
|
# sanity/coverage check
|
2024-08-07 14:00:53 -05:00
|
|
|
view = self.make_view()
|
2024-08-06 18:52:54 -05:00
|
|
|
simple = view.configure_get_simple_settings()
|
2024-08-06 19:59:30 -05:00
|
|
|
|
|
|
|
def test_configure_get_context(self):
|
|
|
|
# sanity/coverage check
|
2024-08-07 14:00:53 -05:00
|
|
|
view = self.make_view()
|
2024-08-06 19:59:30 -05:00
|
|
|
context = view.configure_get_context()
|
2024-08-07 14:00:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TestSettingView(WebTestCase):
|
|
|
|
|
|
|
|
def make_view(self):
|
2024-08-18 14:05:10 -05:00
|
|
|
return mod.SettingView(self.request)
|
2024-08-07 14:00:53 -05:00
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
def test_get_grid_data(self):
|
2024-08-07 14:00:53 -05:00
|
|
|
|
|
|
|
# empty data by default
|
|
|
|
view = self.make_view()
|
2024-08-18 14:05:10 -05:00
|
|
|
query = view.get_grid_data(session=self.session)
|
|
|
|
data = query.all()
|
2024-08-07 14:00:53 -05:00
|
|
|
self.assertEqual(len(data), 0)
|
|
|
|
|
|
|
|
# unless we save some settings
|
|
|
|
self.app.save_setting(self.session, 'foo', 'bar')
|
|
|
|
self.session.commit()
|
2024-08-18 14:05:10 -05:00
|
|
|
query = view.get_grid_data(session=self.session)
|
|
|
|
data = query.all()
|
2024-08-07 14:00:53 -05:00
|
|
|
self.assertEqual(len(data), 1)
|
2024-08-07 19:47:24 -05:00
|
|
|
|
2024-08-21 11:46:38 -05:00
|
|
|
def test_configure_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
grid = view.make_grid(model_class=model.Setting)
|
|
|
|
self.assertFalse(grid.is_linked('name'))
|
|
|
|
view.configure_grid(grid)
|
|
|
|
self.assertTrue(grid.is_linked('name'))
|
|
|
|
|
2024-08-11 12:41:22 -05:00
|
|
|
def test_configure_form(self):
|
|
|
|
view = self.make_view()
|
|
|
|
form = view.make_form(fields=view.get_form_fields())
|
|
|
|
self.assertNotIn('value', form.required_fields)
|
|
|
|
view.configure_form(form)
|
|
|
|
self.assertIn('value', form.required_fields)
|
|
|
|
self.assertFalse(form.required_fields['value'])
|
2024-08-18 14:05:10 -05:00
|
|
|
|
|
|
|
def test_unique_name(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
setting = model.Setting(name='foo')
|
|
|
|
self.session.add(setting)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
with patch.object(view, 'Session', return_value=self.session):
|
|
|
|
|
|
|
|
# invalid if same name in data
|
|
|
|
node = colander.SchemaNode(colander.String(), name='name')
|
|
|
|
self.assertRaises(colander.Invalid, view.unique_name, node, 'foo')
|
|
|
|
|
|
|
|
# but not if name belongs to current setting
|
|
|
|
view.editing = True
|
|
|
|
self.request.matchdict = {'name': 'foo'}
|
|
|
|
node = colander.SchemaNode(colander.String(), name='name')
|
|
|
|
self.assertIsNone(view.unique_name(node, 'foo'))
|