[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form/tests - test_vocabularywidget.py:1.1.2.17
Fred L. Drake, Jr.
fred@zope.com
Fri, 16 May 2003 12:09:40 -0400
Update of /cvs-repository/Zope3/src/zope/app/browser/form/tests
In directory cvs.zope.org:/tmp/cvs-serv2031/tests
Modified Files:
Tag: schema-vocabulary-branch
test_vocabularywidget.py
Log Message:
- start support for the new way of separating vocabulary types and query
support
- move the vocabulary support from widget to vocabularywidget
- refactor the tests into still more little bitty classes that mix together
to produce interesting flavors
=== Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py 1.1.2.16 => 1.1.2.17 ===
--- Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py:1.1.2.16 Fri May 9 10:02:04 2003
+++ Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py Fri May 16 12:09:39 2003
@@ -16,15 +16,16 @@
import unittest
-from zope.app.browser.form import widget
+from zope.app.browser.form import vocabularywidget
from zope.app.interfaces.browser.form import IBrowserWidget
+from zope.app.interfaces.browser.form import IVocabularyQueryView
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.component import getView
from zope.component.view import provideView
from zope.publisher.browser import TestRequest
from zope.publisher.interfaces.browser import IBrowserPresentation
-from zope.schema.interfaces import IVocabulary, ITerm
+from zope.schema.interfaces import IVocabulary, ITerm, IVocabularyQuery
from zope.schema.interfaces import IVocabularyField, IVocabularyMultiField
from zope.schema import vocabulary
@@ -56,6 +57,9 @@
def __len__(self):
return len(self._values)
+ def getQuery(self):
+ return None
+
def getTerm(self, value):
if value in self._values:
return SampleTerm(value)
@@ -78,7 +82,7 @@
__implements__ = ISampleVocabulary
-class SampleDisplayWidget(widget.VocabularyWidgetBase):
+class SampleDisplayWidget(vocabularywidget.VocabularyWidgetBase):
"""Widget used to test that vocabulary-based specialization works.
This is not intended to be a useful widget.
@@ -93,6 +97,35 @@
"""Stub content object used by makeField()."""
+class QueryVocabulary(BasicVocabulary):
+
+ def getQuery(self):
+ return MyVocabularyQuery(self)
+
+class IMyVocabularyQuery(IVocabularyQuery):
+ """Specialized query type."""
+
+class MyVocabularyQuery:
+ __implements__ = IMyVocabularyQuery
+
+ def __init__(self, vocabulary):
+ self.vocabulary = vocabulary
+
+
+class MyQueryViewSingle(vocabularywidget.VocabularyQueryViewBase):
+ __implements__ = IVocabularyQueryView
+
+ def getLabel(self):
+ return "single"
+
+
+class MyQueryViewMulti(vocabularywidget.VocabularyQueryViewBase):
+ __implements__ = IVocabularyQueryView
+
+ def getLabel(self):
+ return "multi"
+
+
class VocabularyWidgetTestBase(PlacelessSetup, unittest.TestCase):
def setUp(self):
@@ -113,22 +146,23 @@
content.f = value
return field.bind(content)
+ def makeRequest(self, querystring=None):
+ if querystring is None:
+ return TestRequest()
+ else:
+ tr = TestRequest(QUERY_STRING=querystring)
+ tr.processInputs()
+ return tr
+
# modified from test_browserwidget.BrowserWidgetTest:
def verifyResult(self, result, check_list):
for check in check_list:
self.assert_(result.find(check) >= 0,
"%r not found in %r" % (check, result))
- def test_vocabulary_specialization(self):
- bound = self.makeField(SampleVocabulary(["frobnication"]))
- w = getView(bound, "display", TestRequest())
- self.assertEqual(w(), "foo")
-
-class SingleSelectionTests(VocabularyWidgetTestBase):
-
- defaultFieldValue = "splat"
- fieldClass = vocabulary.VocabularyField
+class SingleSelectionViews:
+ """Mixin that registers single-selection views."""
def registerViews(self):
# This is equivalent to the default configuration for
@@ -136,20 +170,20 @@
provideView(IVocabularyField,
"display",
IBrowserPresentation,
- widget.VocabularyFieldDisplayWidget)
+ vocabularywidget.VocabularyFieldDisplayWidget)
provideView(IVocabularyField,
"edit",
IBrowserPresentation,
- widget.VocabularyFieldEditWidget)
+ vocabularywidget.VocabularyFieldEditWidget)
# Register the "basic" widgets:
provideView(IVocabulary,
"field-display-widget",
IBrowserPresentation,
- widget.VocabularyDisplayWidget)
+ vocabularywidget.VocabularyDisplayWidget)
provideView(IVocabulary,
"field-edit-widget",
IBrowserPresentation,
- widget.VocabularyEditWidget)
+ vocabularywidget.VocabularyEditWidget)
# The following widget registration supports the specific
# sample vocabulary we're using, used to demonstrate how to
# override widget selection based on vocabulary:
@@ -158,22 +192,63 @@
IBrowserPresentation,
SampleDisplayWidget)
+
+class MultiSelectionViews:
+
+ def registerViews(self):
+ provideView(IVocabularyMultiField,
+ "display",
+ IBrowserPresentation,
+ vocabularywidget.VocabularyMultiFieldDisplayWidget)
+ provideView(IVocabularyMultiField,
+ "edit",
+ IBrowserPresentation,
+ vocabularywidget.VocabularyMultiFieldEditWidget)
+ # Bind widgets to the vocabulary fields:
+ provideView(IVocabulary,
+ "field-display-multi-widget",
+ IBrowserPresentation,
+ vocabularywidget.VocabularyMultiDisplayWidget)
+ provideView(IVocabulary,
+ "field-edit-multi-widget",
+ IBrowserPresentation,
+ vocabularywidget.VocabularyMultiEditWidget)
+ # The following widget registration supports the specific
+ # sample vocabulary we're using, used to demonstrate how to
+ # override widget selection based on vocabulary:
+ provideView(ISampleVocabulary,
+ "field-display-multi-widget",
+ IBrowserPresentation,
+ SampleDisplayWidget)
+
+
+class SelectionTestBase(VocabularyWidgetTestBase):
+ def test_vocabulary_specialization(self):
+ bound = self.makeField(SampleVocabulary(["frobnication"]))
+ w = getView(bound, "display", self.makeRequest())
+ self.assertEqual(w(), "foo")
+
+
+class SingleSelectionTests(SingleSelectionViews, SelectionTestBase):
+
+ defaultFieldValue = "splat"
+ fieldClass = vocabulary.VocabularyField
+
def test_display(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
- w = getView(bound, "display", TestRequest())
+ w = getView(bound, "display", self.makeRequest())
self.assertEqual(w(), "splat")
def test_display_with_form_value(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
- request = TestRequest(QUERY_STRING='field.f=foobar')
- request.processInputs()
+ request = self.makeRequest('field.f=foobar')
w = getView(bound, "display", request)
self.assert_(w.haveData())
self.assertEqual(w(), "foobar")
def test_edit(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
- w = getView(bound, "edit", TestRequest())
+ w = getView(bound, "edit", self.makeRequest())
self.assert_(not w.haveData())
self.verifyResult(w(), [
'selected="selected"',
@@ -199,12 +274,11 @@
def test_edit_with_form_value(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
- request = TestRequest(QUERY_STRING='field.f=foobar')
- request.processInputs()
+ request = self.makeRequest('field.f=foobar')
w = getView(bound, "edit", request)
self.assert_(w.haveData())
self.assertEqual(w._showData(), "foobar")
- self.assert_(isinstance(w, widget.VocabularyEditWidget))
+ self.assert_(isinstance(w, vocabularywidget.VocabularyEditWidget))
self.verifyResult(w.hidden(), [
'<input',
'id="field.f"',
@@ -213,43 +287,15 @@
])
-class MultiSelectionTests(VocabularyWidgetTestBase):
+class MultiSelectionTests(MultiSelectionViews, SelectionTestBase):
defaultFieldValue = ["splat"]
fieldClass = vocabulary.VocabularyMultiField
- def registerViews(self):
- # This is equivalent to the default configuration for
- # vocabulary field view registration from configure.zcml.
- provideView(IVocabularyMultiField,
- "display",
- IBrowserPresentation,
- widget.VocabularyMultiFieldDisplayWidget)
- provideView(IVocabularyMultiField,
- "edit",
- IBrowserPresentation,
- widget.VocabularyMultiFieldEditWidget)
- # Bind widgets to the vocabulary fields:
- provideView(IVocabulary,
- "field-display-multi-widget",
- IBrowserPresentation,
- widget.VocabularyMultiDisplayWidget)
- provideView(IVocabulary,
- "field-edit-multi-widget",
- IBrowserPresentation,
- widget.VocabularyMultiEditWidget)
- # The following widget registration supports the specific
- # sample vocabulary we're using, used to demonstrate how to
- # override widget selection based on vocabulary:
- provideView(ISampleVocabulary,
- "field-display-multi-widget",
- IBrowserPresentation,
- SampleDisplayWidget)
-
def test_display_without_value(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]),
None)
- w = getView(bound, "display", TestRequest())
+ w = getView(bound, "display", self.makeRequest())
self.assert_(not w.haveData())
self.verifyResult(w(), [
'<span',
@@ -262,7 +308,7 @@
def test_display_with_value(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]),
["foobar", "frob"])
- w = getView(bound, "display", TestRequest())
+ w = getView(bound, "display", self.makeRequest())
self.assert_(not w.haveData())
self.verifyResult(w(), [
'<ol',
@@ -284,8 +330,7 @@
def test_display_with_form_data(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]),
["foobar", "frob"])
- request = TestRequest(QUERY_STRING='field.f:list=splat')
- request.processInputs()
+ request = self.makeRequest('field.f:list=splat')
w = getView(bound, "display", request)
self.assert_(w.haveData())
s = w()
@@ -304,7 +349,7 @@
def test_edit(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]))
- w = getView(bound, "edit", TestRequest())
+ w = getView(bound, "edit", self.makeRequest())
self.assert_(not w.haveData())
self.verifyResult(w(), [
'id="field.f"',
@@ -336,9 +381,7 @@
def test_edit_with_form_value(self):
bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]))
- request = TestRequest(
- QUERY_STRING='field.f:list=foobar&field.f:list=splat')
- request.processInputs()
+ request = self.makeRequest('field.f:list=foobar&field.f:list=splat')
w = getView(bound, "edit", request)
self.assert_(w.haveData())
L = w._showData()
@@ -355,9 +398,56 @@
self.assert_(s.find("frob") < 0)
+class QuerySupportTestBase(VocabularyWidgetTestBase):
+ """Base class defining tests that can be used for both single- and
+ multi-select query support.
+
+ Derived classes must specialize to support specific selection
+ mechanics.
+ """
+
+ def test_get_query_helper(self):
+ bound = self.makeField(QueryVocabulary(["splat", "foobar", "frob"]))
+ request = self.makeRequest()
+ w = getView(bound, "edit", request)
+ self.assertEqual(w.queryview.getLabel(), self.queryViewLabel)
+
+
+class SingleSelectionQuerySupportTests(SingleSelectionViews,
+ QuerySupportTestBase):
+
+ defaultFieldValue = "splat"
+ fieldClass = vocabulary.VocabularyField
+ queryViewLabel = "single"
+
+ def registerViews(self):
+ SingleSelectionViews.registerViews(self)
+ provideView(IMyVocabularyQuery,
+ "widget-query-helper",
+ IBrowserPresentation,
+ MyQueryViewSingle)
+
+
+class MultiSelectionQuerySupportTests(MultiSelectionViews,
+ QuerySupportTestBase):
+
+ defaultFieldValue = ["splat"]
+ fieldClass = vocabulary.VocabularyMultiField
+ queryViewLabel = "multi"
+
+ def registerViews(self):
+ MultiSelectionViews.registerViews(self)
+ provideView(IMyVocabularyQuery,
+ "widget-query-multi-helper",
+ IBrowserPresentation,
+ MyQueryViewMulti)
+
+
def test_suite():
suite = unittest.makeSuite(SingleSelectionTests)
suite.addTest(unittest.makeSuite(MultiSelectionTests))
+ suite.addTest(unittest.makeSuite(SingleSelectionQuerySupportTests))
+ suite.addTest(unittest.makeSuite(MultiSelectionQuerySupportTests))
return suite
if __name__ == '__main__':