def test_validate_decimal_to_float(self): rf = ResourceField('f', { 'example': 123, 'type': float, }) r = rf.validate(mock.Mock(spec=object), decimal.Decimal('13.2')) self.assertEqual(r, 13.2)
def test_validate(self): rf = ResourceField('f', { 'example': 123, 'type': int, }) r = rf.validate(mock.Mock(spec=object), 132) self.assertEqual(r, 132)
def test_validate_int_to_decimal(self): rf = ResourceField('f', { 'example': decimal.Decimal('12.3'), 'type': decimal.Decimal, }) r = rf.validate(mock.Mock(spec=object), 1) self.assertEqual(r, 1)
def test_check_type_static(self): rf = ResourceField('f', { 'example': 123, 'type': int }) self.assertTrue(rf.check_type(13)) self.assertFalse(rf.check_type('mallard'))
def test_validate_int_to_decimal(self): rf = ResourceField('f', { 'example': decimal.Decimal('12.3'), 'type': decimal.Decimal, }) r = rf.validate(mock.Mock(spec=object), 1) self.assertEqual(r, 1)
def test_validate_decimal_to_float(self): rf = ResourceField('f', { 'example': 123, 'type': float, }) r = rf.validate(mock.Mock(spec=object), decimal.Decimal('13.2')) self.assertEqual(r, 13.2)
def test_validate(self): rf = ResourceField('f', { 'example': 123, 'type': int, }) r = rf.validate(mock.Mock(spec=object), 132) self.assertEqual(r, 132)
def test_choice(self): rf = ResourceField('f', { 'example': 'mpm', 'choices': [ 'prefork', 'worker' ] }) rf.check_choice('prefork')
def test_validate_validator(self): rf = ResourceField('f', { 'example': 123, 'type': int, }) vrf = mock.Mock() r = rf.validate(mock.Mock(spec=object, validate_resource_f=vrf), 132) vrf.assert_called_once_with(132) self.assertEqual(r, vrf.return_value)
def test_choice_default_on_null(self): rf = ResourceField( 'f', { 'example': 'mpm', 'default_on_null': True, 'choices': ['prefork', 'worker'] }) manager = mock.Mock() self.assertEqual(rf.validate(manager, None), manager.validate_resource_f.return_value)
def test_validate_validator(self): rf = ResourceField('f', { 'example': 123, 'type': int, }) vrf = mock.Mock() r = rf.validate(mock.Mock(spec=object, validate_resource_f=vrf), 132) vrf.assert_called_once_with(132) self.assertEqual(r, vrf.return_value)
def test_choice_callable(self): callable = mock.Mock(return_value=[ 'prefork', 'worker' ]) rf = ResourceField('f', { 'example': 'mpm', 'choices': callable }) assert not callable.reset_mock() rf.check_choice('prefork') callable.assert_called_once_with()
def test_choice_default_on_null(self): rf = ResourceField('f', { 'example': 'mpm', 'default_on_null': True, 'choices': [ 'prefork', 'worker' ] }) manager = mock.Mock() self.assertEqual(rf.validate(manager, None), manager.validate_resource_f.return_value)
def test_validate_validator_validators(self): validator1 = mock.Mock() rf = ResourceField('f', { 'example': 123, 'type': int, 'validators': [ validator1, ] }) vrf = mock.Mock() rf.validate(mock.Mock(spec=object, validate_resource_f=vrf), 132) vrf.assert_called_once_with(validator1.return_value)
def test_validate_validator_validators(self): validator1 = mock.Mock() rf = ResourceField('f', { 'example': 123, 'type': int, 'validators': [ validator1, ] }) vrf = mock.Mock() rf.validate(mock.Mock(spec=object, validate_resource_f=vrf), 132) vrf.assert_called_once_with(validator1.return_value)
def test_validate_validators(self): validator1, validator2 = mock.Mock(), mock.Mock() rf = ResourceField('f', { 'example': 123, 'type': int, 'validators': [ validator1, validator2, ] }) r = rf.validate(mock.Mock(spec=object), 132) validator1.assert_called_once_with(132) validator2.assert_called_once_with(validator1.return_value) self.assertEqual(r, validator2.return_value)
def test_validate_validators(self): validator1, validator2 = mock.Mock(), mock.Mock() rf = ResourceField('f', { 'example': 123, 'type': int, 'validators': [ validator1, validator2, ] }) r = rf.validate(mock.Mock(spec=object), 132) validator1.assert_called_once_with(132) validator2.assert_called_once_with(validator1.return_value) self.assertEqual(r, validator2.return_value)
def test_no_example_ok(self): rf = ResourceField('f', { 'computed': True, 'type': int, }) self.assertEqual(rf.computed, True) self.assertEqual(rf.example, '')
def test_validate_validator_returns_None(self): rf = ResourceField('f', { 'example': 123, 'type': int, }) manager = mock.Mock(spec=object, validate_resource_f=mock.MagicMock( return_value=None, __name__='validare_resource_f', )) self.assertRaises(ValueError, rf.validate, manager, 123)
def test_all_parameters(self): rf = ResourceField('f', { 'type': int, 'example': 1, 'optional': True, 'typing': 'dynamic', }) self.assertEqual(rf.type, int) self.assertEqual(rf.example, 1) self.assertEqual(rf.optional, True) self.assertEqual(rf.typing, 'dynamic') self.assertEqual(rf.extra, {})
def test_extra(self): def a(): return rf = ResourceField( 'f', { 'example': 'quack', 'unicode': u'unicode', 'func_doc': mock.MagicMock(__doc__='my func doc'), 'func': a, }) self.assertEqual(rf.extra, { 'unicode': u'unicode', 'func_doc': 'my func doc', 'func': unicode(a), })
def inner_action_parameter(fn): if not hasattr(fn, 'resource_fields'): fn.resource_fields = {} if isinstance(fn.resource_fields, ResourceFields): warnings.warn('''You should use the decorator @action before the decorator @parameter:: @action @parameter(length, example=3, typing='static', type=int) def cut_text(self, resource, length): return resource.text[:length] ''') rfs = list(fn.resource_fields) for i, rf in enumerate(rfs): if rf.name == name: rfs[i] = ResourceField(name, rf, **kw) fn.resource_fields = ResourceFields(rfs) break else: rf = fn.resource_fields.setdefault(name, {}) rf.update(kw) return fn
def test_example_from_choice(self): rf = ResourceField('f', {'type': int, 'choices': [1, 2, 3]}) self.assertEqual(rf.example, 1)
def test_int_is_long(self): rf = ResourceField('f', {'example': 123, 'type': int}) self.assertTrue(rf.check_type(long(13)))
def test_check_type_dynamic(self): rf = ResourceField('f', { 'example': 'quack', 'typing': 'dynamic', }) self.assertTrue(rf.check_type(13))
def test_validate_fail_check_type(self): rf = ResourceField('f', {'example': 123, 'type': int}) self.assertRaises(ValidationError, rf.validate, mock.Mock(), '123')
def test_check_type_static(self): rf = ResourceField('f', {'example': 123, 'type': int}) self.assertTrue(rf.check_type(13)) self.assertFalse(rf.check_type('mallard'))
def test_example_unicode(self): rf = ResourceField('f', {'example': 'dudem', 'type': unicode}) self.assertEqual(rf.type, unicode) self.assertEqual(rf.example, u'dudem') self.assertTrue(isinstance(rf.example, unicode))
def test_validate_float_to_decimal(self): rf = ResourceField('f', { 'example': decimal.Decimal('12.3'), 'type': decimal.Decimal, }) self.assertRaises(ValidationError, rf.validate, mock.Mock(), 12.3)
def test_choice(self): rf = ResourceField('f', { 'example': 'mpm', 'choices': ['prefork', 'worker'] }) rf.check_choice('prefork')
def test_choice_false(self): rf = ResourceField('f', { 'example': 'mpm', 'choices': ['prefork', 'worker'] }) self.assertRaises(ValidationError, rf.check_choice, 'patoum')
def test_choice_callable(self): callable = mock.Mock(return_value=['prefork', 'worker']) rf = ResourceField('f', {'example': 'mpm', 'choices': callable}) assert not callable.reset_mock() rf.check_choice('prefork') callable.assert_called_once_with()
def test_check_type_dynamic(self): rf = ResourceField('f', { 'example': 'quack', 'typing': 'dynamic', }) self.assertTrue(rf.check_type(13))
def test_int_is_long(self): rf = ResourceField('f', { 'example': 123, 'type': int }) self.assertTrue(rf.check_type(long(13)))