# -*- coding: utf-8; -*- import datetime import decimal from unittest.mock import patch import colander import deform from pyramid import testing from wuttaweb import grids from wuttaweb.forms import widgets as mod from wuttaweb.forms import schema from wuttaweb.forms.schema import (FileDownload, PersonRef, RoleRefs, UserRefs, Permissions, WuttaDateTime, EmailRecipients) from tests.util import WebTestCase class TestObjectRefWidget(WebTestCase): def make_field(self, node, **kwargs): # TODO: not sure why default renderer is in use even though # pyramid_deform was included in setup? but this works.. kwargs.setdefault('renderer', deform.Form.default_renderer) return deform.Field(node, **kwargs) def make_widget(self, **kwargs): return mod.ObjectRefWidget(self.request, **kwargs) def test_serialize(self): model = self.app.model person = model.Person(full_name="Betty Boop") self.session.add(person) self.session.commit() with patch.object(schema, 'Session', return_value=self.session): # standard (editable) node = colander.SchemaNode(PersonRef(self.request)) widget = self.make_widget() field = self.make_field(node) html = widget.serialize(field, person.uuid) self.assertIn('$12.34') # readonly w/ null value result = widget.serialize(field, None, readonly=True) self.assertEqual(result, '') class TestWuttaQuantityWidget(WebTestCase): def make_field(self, node, **kwargs): # TODO: not sure why default renderer is in use even though # pyramid_deform was included in setup? but this works.. kwargs.setdefault('renderer', deform.Form.default_renderer) return deform.Field(node, **kwargs) def make_widget(self, **kwargs): return mod.WuttaQuantityWidget(self.request, **kwargs) def test_serialize(self): node = colander.SchemaNode(schema.WuttaQuantity(self.request)) field = self.make_field(node) widget = self.make_widget() amount = decimal.Decimal('42.00') # editable widget has normal text input result = widget.serialize(field, str(amount)) self.assertIn('42') # readonly w/ null value result = widget.serialize(field, None, readonly=True) self.assertEqual(result, '') class TestFileDownloadWidget(WebTestCase): def make_field(self, node, **kwargs): # TODO: not sure why default renderer is in use even though # pyramid_deform was included in setup? but this works.. kwargs.setdefault('renderer', deform.Form.default_renderer) return deform.Field(node, **kwargs) def test_serialize(self): # nb. we let the field construct the widget via our type # (nb. at first we do not provide a url) node = colander.SchemaNode(FileDownload(self.request)) field = self.make_field(node) widget = field.widget # null value html = widget.serialize(field, None, readonly=True) self.assertNotIn('', html) # path to nonexistent file html = widget.serialize(field, '/this/path/does/not/exist', readonly=True) self.assertNotIn('', html) # path to actual file datfile = self.write_file('data.txt', "hello\n" * 1000) html = widget.serialize(field, datfile, readonly=True) self.assertNotIn('', html) self.assertIn('data.txt', html) self.assertIn('kB)', html) # path to file, w/ url node = colander.SchemaNode(FileDownload(self.request, url='/download/blarg')) field = self.make_field(node) widget = field.widget html = widget.serialize(field, datfile, readonly=True) self.assertNotIn('', html) self.assertIn('', html) self.assertIn('data.txt', html) self.assertIn('kB)', html) # nb. same readonly output even if we ask for editable html2 = widget.serialize(field, datfile, readonly=False) self.assertEqual(html2, html) class TestGridWidget(WebTestCase): def make_field(self, node, **kwargs): # TODO: not sure why default renderer is in use even though # pyramid_deform was included in setup? but this works.. kwargs.setdefault('renderer', deform.Form.default_renderer) return deform.Field(node, **kwargs) def test_serialize(self): grid = grids.Grid(self.request, columns=['foo', 'bar'], data=[{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4}]) node = colander.SchemaNode(colander.String()) widget = mod.GridWidget(self.request, grid) field = self.make_field(node) # readonly works okay html = widget.serialize(field, None, readonly=True) self.assertIn('') # with data, no actions user = model.User(username='barney') self.session.add(user) self.session.commit() html = widget.serialize(field, {user.uuid}, readonly=True) self.assertIn('', result) self.assertIn('
  • alice@example.com
  • ', result) # editable result = widget.serialize(field, recips_str) self.assertIn('