[Checkins] SVN: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_ Coverage for z.s._f.Choice.
Tres Seaver
cvs-admin at zope.org
Wed Apr 25 18:19:52 UTC 2012
Log message for revision 125282:
Coverage for z.s._f.Choice.
Remove spurious tests.
Changed:
U zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py
D zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_boolfield.py
D zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_choice.py
-=-
Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py 2012-04-25 11:07:26 UTC (rev 125281)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py 2012-04-25 18:19:48 UTC (rev 125282)
@@ -155,6 +155,14 @@
class ChoiceTests(unittest.TestCase):
+ def setUp(self):
+ from zope.schema.vocabulary import _clear
+ _clear()
+
+ def tearDown(self):
+ from zope.schema.vocabulary import _clear
+ _clear()
+
def _getTargetClass(self):
from zope.schema._field import Choice
return Choice
@@ -184,11 +192,85 @@
choose = self._makeOne(values=[1, 2])
self.assertTrue(isinstance(choose.vocabulary, SimpleVocabulary))
self.assertEqual(sorted(choose.vocabulary.by_value.keys()), [1, 2])
+ self.assertEqual(sorted(choose.source.by_value.keys()), [1, 2])
def test_ctor_w_named_vocabulary(self):
- choose = self._makeOne(vocabulary='voc.name')
- self.assertEqual(choose.vocabularyName, 'voc.name')
+ choose = self._makeOne(vocabulary="vocab")
+ self.assertEqual(choose.vocabularyName, 'vocab')
+ def test_ctor_w_preconstructed_vocabulary(self):
+ v = _makeSampleVocabulary()
+ choose = self._makeOne(vocabulary=v)
+ self.assertTrue(choose.vocabulary is v)
+ self.assertTrue(choose.vocabularyName is None)
+
+ def test_bind_w_preconstructed_vocabulary(self):
+ from zope.schema.interfaces import ValidationError
+ from zope.schema.vocabulary import setVocabularyRegistry
+ v = _makeSampleVocabulary()
+ setVocabularyRegistry(_makeDummyRegistry(v))
+ choose = self._makeOne(vocabulary='vocab')
+ bound = choose.bind(None)
+ self.assertEqual(bound.vocabulary, v)
+ self.assertEqual(bound.vocabularyName, 'vocab')
+ bound.default = 1
+ self.assertEqual(bound.default, 1)
+ def _provoke(bound):
+ bound.default = 42
+ self.assertRaises(ValidationError, _provoke, bound)
+
+ def test_bind_w_voc_not_ICSB(self):
+ from zope.interface import implementer
+ from zope.schema.interfaces import ISource
+ from zope.schema.interfaces import IBaseVocabulary
+
+ @implementer(IBaseVocabulary)
+ @implementer(ISource)
+ class Vocab(object):
+ def __init__(self):
+ pass
+ source = self._makeOne(vocabulary=Vocab())
+ instance = DummyInstance()
+ target = source.bind(instance)
+ self.assertTrue(target.vocabulary is source.vocabulary)
+
+ def test_bind_w_voc_is_ICSB(self):
+ from zope.interface import implementer
+ from zope.schema.interfaces import IContextSourceBinder
+ from zope.schema.interfaces import ISource
+
+ @implementer(IContextSourceBinder)
+ @implementer(ISource)
+ class Vocab(object):
+ def __init__(self, context):
+ self.context = context
+ def __call__(self, context):
+ return self.__class__(context)
+ # Chicken-egg
+ source = self._makeOne(vocabulary='temp')
+ source.vocabulary = Vocab(source)
+ source.vocabularyName = None
+ instance = DummyInstance()
+ target = source.bind(instance)
+ self.assertEqual(target.vocabulary.context, instance)
+
+ def test_bind_w_voc_is_ICSB_but_not_ISource(self):
+ from zope.interface import implementer
+ from zope.schema.interfaces import IContextSourceBinder
+
+ @implementer(IContextSourceBinder)
+ class Vocab(object):
+ def __init__(self, context):
+ self.context = context
+ def __call__(self, context):
+ return self.__class__(context)
+ # Chicken-egg
+ source = self._makeOne(vocabulary='temp')
+ source.vocabulary = Vocab(source)
+ source.vocabularyName = None
+ instance = DummyInstance()
+ self.assertRaises(ValueError, source.bind, instance)
+
def test_fromUnicode_miss(self):
from zope.schema.interfaces import ConstraintNotSatisfied
from zope.schema._compat import u
@@ -204,7 +286,126 @@
self.assertEqual(flt.fromUnicode(u('bar')), u('bar'))
self.assertEqual(flt.fromUnicode(u('baz')), u('baz'))
+ def test__validate_int(self):
+ from zope.schema.interfaces import ConstraintNotSatisfied
+ choice = self._makeOne(values=[1, 3])
+ choice._validate(1) #doesn't raise
+ choice._validate(3) #doesn't raise
+ self.assertRaises(ConstraintNotSatisfied, choice._validate, 4)
+ def test__validate_string(self):
+ from zope.schema._compat import u
+ from zope.schema.interfaces import ConstraintNotSatisfied
+ choice = self._makeOne(values=['a', 'c'])
+ choice._validate('a') #doesn't raise
+ choice._validate('c') #doesn't raise
+ choice._validate(u('c')) #doesn't raise
+ self.assertRaises(ConstraintNotSatisfied, choice._validate, 'd')
+
+ def test__validate_tuple(self):
+ from zope.schema.interfaces import ConstraintNotSatisfied
+ choice = self._makeOne(values=[(1, 2), (5, 6)])
+ choice._validate((1, 2)) #doesn't raise
+ choice._validate((5, 6)) #doesn't raise
+ self.assertRaises(ConstraintNotSatisfied, choice._validate, [5, 6])
+ self.assertRaises(ConstraintNotSatisfied, choice._validate, ())
+
+ def test__validate_mixed(self):
+ from zope.schema.interfaces import ConstraintNotSatisfied
+ choice = self._makeOne(values=[1, 'b', (0.2,)])
+ choice._validate(1) #doesn't raise
+ choice._validate('b') #doesn't raise
+ choice._validate((0.2,)) #doesn't raise
+ self.assertRaises(ConstraintNotSatisfied, choice._validate, '1')
+ self.assertRaises(ConstraintNotSatisfied, choice._validate, 0.2)
+
+ def test__validate_w_named_vocabulary_invalid(self):
+ choose = self._makeOne(vocabulary='vocab')
+ self.assertRaises(ValueError, choose._validate, 42)
+
+ def test__validate_w_named_vocabulary(self):
+ from zope.schema.interfaces import ConstraintNotSatisfied
+ from zope.schema.vocabulary import setVocabularyRegistry
+ v = _makeSampleVocabulary()
+ setVocabularyRegistry(_makeDummyRegistry(v))
+ choose = self._makeOne(vocabulary='vocab')
+ choose._validate(1)
+ choose._validate(3)
+ self.assertRaises(ConstraintNotSatisfied, choose._validate, 42)
+
+ def test__validate_source_is_ICSB_unbound(self):
+ from zope.interface import implementer
+ from zope.schema.interfaces import IContextSourceBinder
+ @implementer(IContextSourceBinder)
+ class SampleContextSourceBinder(object):
+ def __call__(self, context):
+ pass
+ choice = self._makeOne(source=SampleContextSourceBinder())
+ self.assertRaises(TypeError, choice.validate, 1)
+
+ def test__validate_source_is_ICSB_bound(self):
+ from zope.interface import implementer
+ from zope.schema.interfaces import IContextSourceBinder
+ from zope.schema.interfaces import ConstraintNotSatisfied
+ from zope.schema.tests.test_vocabulary import _makeSampleVocabulary
+ @implementer(IContextSourceBinder)
+ class SampleContextSourceBinder(object):
+ def __call__(self, context):
+ return _makeSampleVocabulary()
+ s = SampleContextSourceBinder()
+ choice = self._makeOne(source=s)
+ # raises not iterable with unbound field
+ self.assertRaises(TypeError, choice.validate, 1)
+ o = object()
+ clone = choice.bind(o)
+ clone._validate(1)
+ clone._validate(3)
+ self.assertRaises(ConstraintNotSatisfied, clone._validate, 42)
+
+
+class DummyInstance(object):
+ pass
+
+
+def _makeSampleVocabulary():
+ from zope.interface import implementer
+ from zope.schema.interfaces import IVocabulary
+
+ class SampleTerm(object):
+ pass
+
+ @implementer(IVocabulary)
+ class SampleVocabulary(object):
+
+ def __iter__(self):
+ return iter([self.getTerm(x) for x in range(0, 10)])
+
+ def __contains__(self, value):
+ return 0 <= value < 10
+
+ def __len__(self):
+ return 10
+
+ def getTerm(self, value):
+ if value in self:
+ t = SampleTerm()
+ t.value = value
+ t.double = 2 * value
+ return t
+ raise LookupError("no such value: %r" % value)
+
+ return SampleVocabulary()
+
+def _makeDummyRegistry(v):
+ from zope.schema.vocabulary import VocabularyRegistry
+ class DummyRegistry(VocabularyRegistry):
+ def __init__(self, vocabulary):
+ self._vocabulary = vocabulary
+ def get(self, object, name):
+ return self._vocabulary
+ return DummyRegistry(v)
+
+
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(BytesTests),
Deleted: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_boolfield.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_boolfield.py 2012-04-25 11:07:26 UTC (rev 125281)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_boolfield.py 2012-04-25 18:19:48 UTC (rev 125282)
@@ -1,75 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Boolean field tests
-"""
-import unittest
-
-from zope.schema.tests.test_field import FieldTestBase
-
-
-class BoolTest(unittest.TestCase, FieldTestBase):
- """Test the Bool Field."""
-
- def _getTargetClass(self):
- from zope.schema import Bool
- return Bool
-
- def testValidate(self):
- from zope.schema._compat import u
- field = self._makeOne(title=u('Bool field'), description=u(''),
- readonly=False, required=False)
- field.validate(None)
- field.validate(True)
- field.validate(False)
-
- def testValidateRequired(self):
- from zope.schema._compat import u
- from zope.schema.interfaces import RequiredMissing
- field = self._makeOne(title=u('Bool field'), description=u(''),
- readonly=False, required=True)
- field.validate(True)
- field.validate(False)
-
- self.assertRaises(RequiredMissing, field.validate, None)
-
- def testIBoolIsMoreImportantThanIFromUnicode(self):
- from zope.schema._compat import u
- from zope.interface import Interface
- from zope.interface.adapter import AdapterRegistry
- from zope.schema.interfaces import IBool
- from zope.schema.interfaces import IFromUnicode
- registry = AdapterRegistry()
-
- def adapt_bool(context):
- return 'bool'
-
- def adapt_from_unicode(context):
- return 'unicode'
-
- class IAdaptTo(Interface):
- pass
-
- registry.register((IBool,), IAdaptTo, u(''), adapt_bool)
- registry.register((IFromUnicode,), IAdaptTo, u(''), adapt_from_unicode)
-
- field = self._makeOne(title=u('Bool field'), description=u(''),
- readonly=False, required=True)
-
- self.assertEqual('bool', registry.queryAdapter(field, IAdaptTo))
-
-
-def test_suite():
- return unittest.TestSuite((
- unittest.makeSuite(BoolTest),
- ))
Deleted: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_choice.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_choice.py 2012-04-25 11:07:26 UTC (rev 125281)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test_choice.py 2012-04-25 18:19:48 UTC (rev 125282)
@@ -1,167 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2003 Zope Foundation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Test of the Choice field.
-"""
-import unittest
-
-
-class _Base(unittest.TestCase):
-
- def _getTargetClass(self):
- from zope.schema import Choice
- return Choice
-
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
-
-
-class Value_ChoiceFieldTests(_Base):
- """Tests of the Choice Field using values."""
-
- def test_create_vocabulary(self):
- choice = self._makeOne(values=[1, 3])
- self.assertEqual([term.value for term in choice.vocabulary], [1, 3])
-
- def test_validate_int(self):
- from zope.schema.interfaces import ConstraintNotSatisfied
- choice = self._makeOne(values=[1, 3])
- choice.validate(1)
- choice.validate(3)
- self.assertRaises(ConstraintNotSatisfied, choice.validate, 4)
-
- def test_validate_string(self):
- from zope.schema._compat import u
- from zope.schema.interfaces import ConstraintNotSatisfied
- choice = self._makeOne(values=['a', 'c'])
- choice.validate('a')
- choice.validate('c')
- choice.validate(u('c'))
- self.assertRaises(ConstraintNotSatisfied, choice.validate, 'd')
-
- def test_validate_tuple(self):
- from zope.schema.interfaces import ConstraintNotSatisfied
- choice = self._makeOne(values=[(1, 2), (5, 6)])
- choice.validate((1, 2))
- choice.validate((5, 6))
- self.assertRaises(ConstraintNotSatisfied, choice.validate, [5, 6])
- self.assertRaises(ConstraintNotSatisfied, choice.validate, ())
-
- def test_validate_mixed(self):
- from zope.schema.interfaces import ConstraintNotSatisfied
- choice = self._makeOne(values=[1, 'b', (0.2,)])
- choice.validate(1)
- choice.validate('b')
- choice.validate((0.2,))
- self.assertRaises(ConstraintNotSatisfied, choice.validate, '1')
- self.assertRaises(ConstraintNotSatisfied, choice.validate, 0.2)
-
-
-class Vocabulary_ChoiceFieldTests(_Base):
- """Tests of the Choice Field using vocabularies."""
-
- def setUp(self):
- from zope.schema.vocabulary import _clear
- _clear()
-
- def tearDown(self):
- from zope.schema.vocabulary import _clear
- _clear()
-
- def _getTargetClass(self):
- from zope.schema import Choice
- return Choice
-
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
-
- def test_preconstructed_vocabulary(self):
- from zope.schema.interfaces import ValidationError
- from zope.schema.tests.test_vocabulary import _makeSampleVocabulary
- v = _makeSampleVocabulary()
- field = self._makeOne(vocabulary=v)
- self.assertTrue(field.vocabulary is v)
- self.assertTrue(field.vocabularyName is None)
- bound = field.bind(None)
- self.assertTrue(bound.vocabulary is v)
- self.assertTrue(bound.vocabularyName is None)
- bound.default = 1
- self.assertEqual(bound.default, 1)
- self.assertRaises(ValidationError, setattr, bound, "default", 42)
-
- def test_constructed_vocabulary(self):
- from zope.schema.interfaces import ValidationError
- from zope.schema.vocabulary import setVocabularyRegistry
- from zope.schema.tests.test_vocabulary import _makeDummyRegistry
- setVocabularyRegistry(_makeDummyRegistry())
- field = self._makeOne(vocabulary="vocab")
- self.assertTrue(field.vocabulary is None)
- self.assertEqual(field.vocabularyName, "vocab")
- o = object()
- bound = field.bind(o)
- bound.default = 1
- self.assertEqual(bound.default, 1)
- self.assertRaises(ValidationError, setattr, bound, "default", 42)
-
- def test_create_vocabulary(self):
- from zope.schema.vocabulary import setVocabularyRegistry
- from zope.schema.tests.test_vocabulary import _makeDummyRegistry
- setVocabularyRegistry(_makeDummyRegistry())
- field = self._makeOne(vocabulary="vocab")
- o = object()
- bound = field.bind(o)
- self.assertEqual([term.value for term in bound.vocabulary],
- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
-
- def test_undefined_vocabulary(self):
- choice = self._makeOne(vocabulary="unknown")
- self.assertRaises(ValueError, choice.validate, "value")
-
-
-class ContextSourceBinder_ChoiceFieldTests(_Base):
- """Tests of the Choice Field using IContextSourceBinder as source."""
-
- def setUp(self):
- from zope.schema.vocabulary import _clear
- _clear()
-
- def tearDown(self):
- from zope.schema.vocabulary import _clear
- _clear()
-
- def test_validate_source(self):
- from zope.interface import implementer
- from zope.schema.interfaces import IContextSourceBinder
- from zope.schema.interfaces import ConstraintNotSatisfied
- from zope.schema.tests.test_vocabulary import _makeSampleVocabulary
- @implementer(IContextSourceBinder)
- class SampleContextSourceBinder(object):
- def __call__(self, context):
- return _makeSampleVocabulary()
- s = SampleContextSourceBinder()
- choice = self._makeOne(source=s)
- # raises not iterable with unbound field
- self.assertRaises(TypeError, choice.validate, 1)
- o = object()
- clone = choice.bind(o)
- clone.validate(1)
- clone.validate(3)
- self.assertRaises(ConstraintNotSatisfied, clone.validate, 42)
-
-
-def test_suite():
- return unittest.TestSuite((
- unittest.makeSuite(Vocabulary_ChoiceFieldTests),
- unittest.makeSuite(Value_ChoiceFieldTests),
- unittest.makeSuite(ContextSourceBinder_ChoiceFieldTests),
- ))
More information about the checkins
mailing list