def custom_function_test(self, value, custom_function): """ Confirm type checking behavior when custom type-checking functions are in use. """ ioprocessor = IOProcessor( typecheck_functions={CustomType: custom_function}, required=self.wrap_iospec(CustomType), ) ioprocessor.verify(iovalue=self.wrap_iovalue(value))
def test_error(self): expected_value = object() def success_error_coercion_function(value, expected_type): raise CoercionSuccessError(expected_value) processor = IOProcessor(required=YesCoercionType, coercion_functions={ YesCoercionType: success_error_coercion_function, }) result = processor.coerce(iovalue=YesCoercionType()) assert result is expected_value
def ioprocessor(self, **kwargs): kwargs.update({ 'coercion_functions': { YesCoercionType: custom_coercion_function } }) return IOProcessor(**kwargs)
def unlimited_extra_nested_item_raises_test(self, parameter_name): """ When 'unlimited' is True, only top-level keyword arguments are unlimited. 'dict'-type iovalues should still be checked for unknown keys. """ with pytest.raises(VerificationFailureError): IOProcessor(unlimited=True, **{ parameter_name: self.make_iospec(1) }).verify(iovalue=self.make_iovalue(2))
def test_result(self): result = iospecs_from_callable(self.callable) # Construct dummy iovals. For each key in 'required', 'optional', set # that key value to 'None' in the dummy ioval. iovals = {} for item in result.values(): for ikey in item: iovals.setdefault(ikey, None) IOProcessor(**result).verify(iovals)
def test_no_iospec_passes(self): IOProcessor().verify(iovalue=object())
def preservation_test(self, parameter_name, initial, expected, iospec): result = IOProcessor(**{ parameter_name: iospec }).coerce(iovalue=initial) assert result == expected
def anytype_passes_test(self, parameter_name): IOProcessor(**{ parameter_name: self.wrap_iospec(iomanager.AnyType) }).verify(iovalue=self.wrap_iovalue(object()))
def correct_type_subclass_passes_test(self, parameter_name): IOProcessor(**{ parameter_name: self.wrap_iospec(CustomType) }).verify(iovalue=self.wrap_iovalue(CustomSubclassType()))
def test_required_overrides_optional(self): with pytest.raises(VerificationFailureError): IOProcessor( required=self.make_iospec(1), optional=self.make_iospec(1), ).verify(iovalue=self.make_iovalue(0))
def parameter_test(self, parameter_name, iovalue): IOProcessor(**{ parameter_name: self.make_iospec(1) }).verify(iovalue=iovalue)
def test_unlimited_ignored_no_iospec(self): """ When no 'iospec' is provided, verification and 'unlimited' is ignored ('True' and 'False' both pass). Similar to the situation with non-container IOSpecs.""" IOProcessor(unlimited=True, ).verify(iovalue=object())
def unlimited_ignored_test(self, parameter_name): with pytest.raises(VerificationFailureError): IOProcessor(unlimited=True, **{ parameter_name: CustomType }).verify(iovalue=object())
def none_value_passes_test(self, parameter_name): IOProcessor(**{ parameter_name: self.wrap_iospec(object) }).verify(iovalue=self.wrap_iovalue(None))
def test_required_overrides_optional(self): IOProcessor( required=self.wrap_iospec(object), optional=self.wrap_iospec(CustomType), ).verify(iovalue=self.wrap_iovalue(object()))
def test_no_iospec_passes(self): IOProcessor().coerce(iovalue=object())
def correct_type_passes_test(self, parameter_name): IOProcessor(**{ parameter_name: self.wrap_iospec(object) }).verify(iovalue=self.wrap_iovalue(object()))
def test_optional_extends_required(self): IOProcessor( required=self.make_iospec(1), optional=self.make_iospec(2), ).verify(iovalue=self.make_iovalue(2))
def unlimited_test(self, parameter_name): IOProcessor(unlimited=True, **{ parameter_name: self.make_iospec(0) }).verify(iovalue=self.make_iovalue(1))
def wrong_type_raises_test(self, parameter_name): with pytest.raises(VerificationFailureError): IOProcessor(**{ parameter_name: self.wrap_iospec(CustomType) }).verify(iovalue=self.wrap_iovalue(object()))