feat: add Users view; improve CRUD master for SQLAlchemy models
This commit is contained in:
parent
33589f1cd8
commit
eac3b81918
33 changed files with 1510 additions and 253 deletions
|
@ -11,8 +11,7 @@ from pyramid.httpexceptions import HTTPFound, HTTPNotFound
|
|||
from wuttjamaican.conf import WuttaConfig
|
||||
from wuttaweb.views import master
|
||||
from wuttaweb.subscribers import new_request_set_user
|
||||
|
||||
from tests.views.utils import WebTestCase
|
||||
from tests.util import WebTestCase
|
||||
|
||||
|
||||
class TestMasterView(WebTestCase):
|
||||
|
@ -387,6 +386,19 @@ class TestMasterView(WebTestCase):
|
|||
with patch.object(view, 'get_query', new=get_query):
|
||||
self.assertRaises(ValueError, view.get_grid_data, session=self.session)
|
||||
|
||||
def test_configure_grid(self):
|
||||
model = self.app.model
|
||||
|
||||
# uuid field is pruned
|
||||
with patch.multiple(master.MasterView, create=True,
|
||||
model_class=model.Setting):
|
||||
view = master.MasterView(self.request)
|
||||
grid = view.make_grid(model_class=model.Setting,
|
||||
columns=['uuid', 'name', 'value'])
|
||||
self.assertIn('uuid', grid.columns)
|
||||
view.configure_grid(grid)
|
||||
self.assertNotIn('uuid', grid.columns)
|
||||
|
||||
def test_get_instance(self):
|
||||
model = self.app.model
|
||||
self.app.save_setting(self.session, 'foo', 'bar')
|
||||
|
@ -454,7 +466,7 @@ class TestMasterView(WebTestCase):
|
|||
model_name='Widget',
|
||||
model_key='uuid'):
|
||||
view = master.MasterView(self.request)
|
||||
form = view.make_model_form()
|
||||
form = view.make_model_form(fields=['name', 'description'])
|
||||
form.validated = {'name': 'first'}
|
||||
obj = view.objectify(form)
|
||||
self.assertIs(obj, form.validated)
|
||||
|
@ -666,7 +678,6 @@ class TestMasterView(WebTestCase):
|
|||
self.assertIn('frazzle', response.text)
|
||||
|
||||
def delete_instance(setting):
|
||||
print(setting) # TODO
|
||||
self.app.delete_setting(self.session, setting['name'])
|
||||
|
||||
# post request to save settings
|
||||
|
|
|
@ -7,7 +7,7 @@ from sqlalchemy import orm
|
|||
from pyramid.httpexceptions import HTTPNotFound
|
||||
|
||||
from wuttaweb.views import people
|
||||
from tests.views.utils import WebTestCase
|
||||
from tests.util import WebTestCase
|
||||
|
||||
|
||||
class TestPersonView(WebTestCase):
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
|
||||
from unittest.mock import patch
|
||||
|
||||
from tests.views.utils import WebTestCase
|
||||
|
||||
from pyramid.httpexceptions import HTTPNotFound
|
||||
|
||||
from wuttaweb.views import settings
|
||||
from tests.util import WebTestCase
|
||||
|
||||
|
||||
class TestAppInfoView(WebTestCase):
|
||||
|
|
58
tests/views/test_users.py
Normal file
58
tests/views/test_users.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlalchemy import orm
|
||||
|
||||
import colander
|
||||
from pyramid.httpexceptions import HTTPNotFound
|
||||
|
||||
from wuttaweb.views import users
|
||||
from tests.util import WebTestCase
|
||||
|
||||
|
||||
class TestPersonView(WebTestCase):
|
||||
|
||||
def make_view(self):
|
||||
return users.UserView(self.request)
|
||||
|
||||
def test_get_query(self):
|
||||
view = self.make_view()
|
||||
query = view.get_query(session=self.session)
|
||||
self.assertIsInstance(query, orm.Query)
|
||||
|
||||
def test_configure_grid(self):
|
||||
model = self.app.model
|
||||
view = self.make_view()
|
||||
grid = view.make_grid(model_class=model.User)
|
||||
self.assertFalse(grid.is_linked('person'))
|
||||
view.configure_grid(grid)
|
||||
self.assertTrue(grid.is_linked('person'))
|
||||
|
||||
def test_configure_form(self):
|
||||
model = self.app.model
|
||||
view = self.make_view()
|
||||
form = view.make_form(model_class=model.Person)
|
||||
self.assertIsNone(form.is_required('person'))
|
||||
view.configure_form(form)
|
||||
self.assertFalse(form.is_required('person'))
|
||||
|
||||
def test_unique_username(self):
|
||||
model = self.app.model
|
||||
view = self.make_view()
|
||||
|
||||
user = model.User(username='foo')
|
||||
self.session.add(user)
|
||||
self.session.commit()
|
||||
|
||||
with patch.object(users, 'Session', return_value=self.session):
|
||||
|
||||
# invalid if same username in data
|
||||
node = colander.SchemaNode(colander.String(), name='username')
|
||||
self.assertRaises(colander.Invalid, view.unique_username, node, 'foo')
|
||||
|
||||
# but not if username belongs to current user
|
||||
view.editing = True
|
||||
self.request.matchdict = {'uuid': user.uuid}
|
||||
node = colander.SchemaNode(colander.String(), name='username')
|
||||
self.assertIsNone(view.unique_username(node, 'foo'))
|
|
@ -1,57 +0,0 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from pyramid import testing
|
||||
|
||||
from wuttjamaican.conf import WuttaConfig
|
||||
from wuttaweb import subscribers
|
||||
|
||||
|
||||
class WebTestCase(TestCase):
|
||||
"""
|
||||
Base class for test suites requiring a full (typical) web app.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.setup_web()
|
||||
|
||||
def setup_web(self):
|
||||
self.config = WuttaConfig(defaults={
|
||||
'wutta.db.default.url': 'sqlite://',
|
||||
'wutta.web.menus.handler_spec': 'tests.utils:NullMenuHandler',
|
||||
})
|
||||
|
||||
self.request = testing.DummyRequest()
|
||||
|
||||
self.pyramid_config = testing.setUp(request=self.request, settings={
|
||||
'wutta_config': self.config,
|
||||
'mako.directories': ['wuttaweb:templates'],
|
||||
})
|
||||
|
||||
# init db
|
||||
self.app = self.config.get_app()
|
||||
model = self.app.model
|
||||
model.Base.metadata.create_all(bind=self.config.appdb_engine)
|
||||
self.session = self.app.make_session()
|
||||
|
||||
# init web
|
||||
self.pyramid_config.include('pyramid_mako')
|
||||
self.pyramid_config.include('wuttaweb.static')
|
||||
self.pyramid_config.include('wuttaweb.views.essential')
|
||||
self.pyramid_config.add_subscriber('wuttaweb.subscribers.before_render',
|
||||
'pyramid.events.BeforeRender')
|
||||
|
||||
# setup new request w/ anonymous user
|
||||
event = MagicMock(request=self.request)
|
||||
subscribers.new_request(event)
|
||||
def user_getter(request, **kwargs): pass
|
||||
subscribers.new_request_set_user(event, db_session=self.session,
|
||||
user_getter=user_getter)
|
||||
|
||||
def tearDown(self):
|
||||
self.teardown_web()
|
||||
|
||||
def teardown_web(self):
|
||||
testing.tearDown()
|
Loading…
Add table
Add a link
Reference in a new issue