I have a version of zope.schema.Orderable (in
zope.schema._bootstrapfields.py) that passes additional tests having to
do with combination of missing_value with other constraints. (Released
version fails these.)

What should I do with it? I am new, and haven't contributed anything
before. Does someone want to see it? (Orderable included below.)
Can/should I just put it in SVN? 

Thanks, - Shaun

------------------------------------------------------------------------
----
Here are the additional tests (from zope.schema.tests.test_intfield.py):
------------------------------------------------------------------------
----

    def testValidateMissing( self ):
        field = self._Field_Factory(title=u'Int field', description=u'',
                                    readonly=False, required=False,
                                    missing_value = -1)

        field.validate(-1)
        field.validate(-10)
        field.validate(10)

        self.assertRaises( WrongType, field.validate, None )

    def testValidateRequiredAndMissing(self):
        field = self._Field_Factory(title=u'Int field', description=u'',
                                    readonly=False, required=True,
                                    missing_value = -1)
        
        self.assertRaises(WrongType, field.validate, None)
        self.assertRaises(RequiredMissing, field.validate, -1)

    def testValidateMinAndMissing(self):
        field = self._Field_Factory(title=u'Int field', description=u'',
                                    readonly=False, required=False,
                                    missing_value = -2, min = 0)
        field.validate(-2)
        field.validate( 10 )
        self.assertRaises(TooSmall, field.validate, -1)

    def testValidateMaxAndMissing(self):
        field = self._Field_Factory(title=u'Int field', description=u'',
                                    readonly=False, required=False,
                                    missing_value = 100, max = 0)
        field.validate(0)
        field.validate(100)
        self.assertRaises(TooBig, field.validate, 1)

    def testValidateMinAndMaxAndMissing(self):
        field = self._Field_Factory(title=u'Int field', description=u'',
                                    readonly=False, required=False,
                                    min=0, max=10, missing_value = -1)

        field.validate(-1)
        field.validate(0)
        field.validate(5)
        field.validate(10)
                                     
        self.assertRaises(TooSmall, field.validate, -10)
        self.assertRaises(WrongType, field.validate, None)
        self.assertRaises(TooBig, field.validate, 11)
        self.assertRaises(TooBig, field.validate, 20)

        # in theory, missing_value could be in middle of range
        # (maybe derived type defines invalid subranges, for instance)
        field = self._Field_Factory(title=u'Int field', description=u'',
                                    readonly=False, required=False,
                                    min=0, max=10, missing_value = 5)

        field.validate(0)
        field.validate(5)
        field.validate(10)
                                     
        self.assertRaises(TooSmall, field.validate, -10)
        self.assertRaises(TooSmall, field.validate, -1)
        self.assertRaises(WrongType, field.validate, None)
        self.assertRaises(TooBig, field.validate, 11)
        self.assertRaises(TooBig, field.validate, 20)

        # however, when missing_value is equal to min or max, the
        # bound is undefined
        field = self._Field_Factory(title=u'Int field', description=u'',
                                    readonly=False, required=False,
                                    min=0, max=10, missing_value = 0)

        field.validate(-10)
        field.validate(-1)
        field.validate(0)
        field.validate(5)
        field.validate(10)
                                     
        self.assertRaises(WrongType, field.validate, None)
        self.assertRaises(TooBig, field.validate, 11)
        self.assertRaises(TooBig, field.validate, 20)


------------------------------------------------------------------------
----
new version of Orderable (from _bootstrapfields.py):
------------------------------------------------------------------------
----


class Orderable(object):
    """Values of ordered fields can be sorted.

    They can be restricted to a range of values.

    Orderable is a mixin used in combination with Field.
    """

    min = ValidatedProperty('min')
    max = ValidatedProperty('max')

    __missing_value_marker = object()
    
    def __init__(self, min=__missing_value_marker,
max=__missing_value_marker,
                 default=__missing_value_marker, **kw):

        # Set min and max to None so that we can validate if
        # one of the super methods invoke validation.
        self.min = None
        self.max = None

        super(Orderable, self).__init__(**kw)

        # make sure that unspecified min, max, default get correct
missing value
        if min is self.__missing_value_marker: min = self.missing_value
        if max is self.__missing_value_marker: max = self.missing_value
        if default is self.__missing_value_marker: default =
self.missing_value

        # Now really set min and max
        # NOTE: order matters here with _validate as is -- see _validate
below
        self.min = min
        self.max = max


        # We've taken over setting default so it can be limited by min
        # and max.
        self.default = default


    def _validate(self, value):
        super(Orderable, self)._validate(value)

        if self.min != self.missing_value and value < self.min:
            raise TooSmall(value, self.min)

        if self.max != self.missing_value and self.max < value:
            # when initially setting min, max not set yet,
            # so skip validation when max is missing OR None.
            if self.max != None:
                 raise TooBig(value, self.max)
        


_______________________________________________
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to