def test_validate(self): validator = Integer() self.assertRaises(ValidationError, validator.validate, 'foo') self.assertRaises(ValidationError, validator.validate, '12.34') self.assertEqual(57, validator.validate(57)) self.assertEqual(57, validator.validate('57'))
def test_option(self): validator = Integer() opt = option( 'foo', 'f', 'The foo option.', flag=True, validator=validator ) self.assertIsInstance(opt, InputOption) self.assertEqual('foo', opt.get_name()) self.assertEqual('The foo option.', opt.get_description()) self.assertTrue(opt.is_flag()) self.assertEqual(validator, opt.get_validator()) opt = option( 'foo', 'f', 'The foo option.', value_required=True, is_list=True, default=['default'], validator=validator ) self.assertIsInstance(opt, InputOption) self.assertEqual('foo', opt.get_name()) self.assertEqual('The foo option.', opt.get_description()) self.assertFalse(opt.is_flag()) self.assertTrue(opt.is_value_required()) self.assertTrue(opt.is_list()) self.assertEqual(validator, opt.get_validator())
def test_validate(self): validator = Integer() self.assertRaises( ValidationError, validator.validate, 'foo' ) self.assertRaises( ValidationError, validator.validate, '12.34' ) self.assertEqual(57, validator.validate(57)) self.assertEqual(57, validator.validate('57'))
def test_validate_arguments(self): definition = InputDefinition( [InputArgument('arg', validator=Integer())]) input = MockInput(definition) input.validate_arguments() # Wrong type input = MockWrongInput(definition) self.assertRaises(InvalidArgument, input.validate_arguments)
def test_argument(self): validator = Integer() arg = argument( 'foo', 'The foo argument.', required=False, is_list=True, default=['default'], validator=validator ) self.assertIsInstance(arg, InputArgument) self.assertEqual('foo', arg.get_name()) self.assertEqual('The foo argument.', arg.get_description()) self.assertEqual(['default'], arg.get_default()) self.assertTrue(arg.is_list()) self.assertFalse(arg.is_required()) self.assertEqual(validator, arg.get_validator())
def test_init(self): """ InputArgument.__init__() behaves properly """ argument = InputArgument('foo') self.assertEqual('foo', argument.get_name(), msg='__init__() takes a name as its first argument') # mode argument argument = InputArgument('foo') self.assertFalse( argument.is_required(), msg='__init__() gives a "InputArgument.OPTIONAL" mode by default') argument = InputArgument('foo', None) self.assertFalse( argument.is_required(), msg='__init__() can take "InputArgument.OPTIONAL" as its mode') argument = InputArgument('foo', InputArgument.OPTIONAL) self.assertFalse( argument.is_required(), msg='__init__() can take "InputArgument.OPTIONAL" as its mode') argument = InputArgument('foo', InputArgument.REQUIRED) self.assertTrue( argument.is_required(), msg='__init__() can take "InputArgument.REQUIRED" as its mode') self.assertRaises(Exception, InputArgument, 'foo', 'ANOTHER_MODE') self.assertRaises(Exception, InputArgument, 'foo', -1) validator = Integer() argument = InputArgument('foo', None, validator=validator) self.assertEqual(validator, argument.get_validator()) # Named validator validator = 'integer' argument = InputArgument('foo', None, validator=validator) self.assertIsInstance(argument.get_validator(), Integer) # Native type validator = int argument = InputArgument('foo', None, validator=validator) self.assertIsInstance(argument.get_validator(), Integer)
def test_from_dict(self): """ InputOption.from_dict() returns an InputOption instance given a dict. """ validator = Integer() option_dict = { 'foo': { 'shortcut': 'f', 'description': 'The foo option.', 'value_required': False, 'list': True, 'default': ['default'], 'validator': validator } } option = InputOption.from_dict(option_dict) self.assertTrue(InputOption, option) self.assertEqual('foo', option.get_name()) self.assertEqual('f', option.get_shortcut()) self.assertEqual('The foo option.', option.get_description()) self.assertEqual(['default'], option.get_default()) self.assertTrue(option.is_list()) self.assertFalse(option.is_value_required()) self.assertTrue(option.is_value_optional()) self.assertEqual(validator, option.get_validator()) option_dict = { 'foo': { 'value_required': True } } option = InputOption.from_dict(option_dict) self.assertFalse(option.is_list()) self.assertTrue(option.is_value_required()) option_dict = { 'foo': {} } option = InputOption.from_dict(option_dict) self.assertFalse(option.is_list()) self.assertFalse(option.is_value_required()) self.assertFalse(option.is_value_optional())
def test_from_dict_with_plain_dict(self): """ InputOption.from_dict() returns an InputOption instance given a dict. """ validator = Integer() option_dict = { 'name': 'foo', 'shortcut': 'f', 'description': 'The foo option.', 'flag': True, 'validator': validator } option = InputOption.from_dict(option_dict) self.assertTrue(InputOption, option) self.assertEqual('foo', option.get_name()) self.assertEqual('f', option.get_shortcut()) self.assertEqual('The foo option.', option.get_description()) self.assertTrue(option.is_flag()) self.assertEqual(validator, option.get_validator())
def test_from_dict_plain_dict(self): """ InputArgument.from_dict() returns an InputArgument instance given a dict. """ validator = Integer() argument_dict = { 'name': 'foo', 'description': 'The foo argument.', 'required': False, 'list': True, 'default': ['default'], 'validator': validator } argument = InputArgument.from_dict(argument_dict) self.assertTrue(InputArgument, argument) self.assertEqual('foo', argument.get_name()) self.assertEqual('The foo argument.', argument.get_description()) self.assertEqual(['default'], argument.get_default()) self.assertTrue(argument.is_list()) self.assertFalse(argument.is_required()) self.assertEqual(validator, argument.get_validator())
def test_init(self): """ InputOption.__init__() behaves properly """ option = InputOption('foo') self.assertEqual('foo', option.get_name(), msg='__init__() takes a name as its first argument') option = InputOption('--foo') self.assertEqual( 'foo', option.get_name(), msg='__init__() removes the leading -- of the option name') # shortcut argument option = InputOption('foo', 'f') self.assertEqual( 'f', option.get_shortcut(), msg='__init__() can take a shortcut as its second argument') option = InputOption('foo', '-f') self.assertEqual( 'f', option.get_shortcut(), msg='__init__() removes the leading - of the shortcut') option = InputOption('foo') self.assertEqual(None, option.get_shortcut(), msg='__init__() makes the shortcut null by default') # mode argument option = InputOption('foo', 'f') self.assertFalse( option.accept_value(), msg='__init__() gives a "InputOption.VALUE_NONE" mode by default') self.assertFalse( option.is_value_required(), msg='__init__() gives a "InputOption.VALUE_NONE" mode by default') self.assertFalse( option.is_value_optional(), msg='__init__() gives a "InputOption.VALUE_NONE" mode by default') option = InputOption('foo', 'f', None) self.assertFalse( option.accept_value(), msg='__init__() can take "InputOption.VALUE_NONE" as its mode') self.assertFalse( option.is_value_required(), msg='__init__() can take "InputOption.VALUE_NONE" as its mode') self.assertFalse( option.is_value_optional(), msg='__init__() can take "InputOption.VALUE_NONE" as its mode') option = InputOption('foo', 'f', InputOption.VALUE_NONE) self.assertFalse( option.accept_value(), msg='__init__() can take "InputOption.VALUE_NONE" as its mode') self.assertFalse( option.is_value_required(), msg='__init__() can take "InputOption.VALUE_NONE" as its mode') self.assertFalse( option.is_value_optional(), msg='__init__() can take "InputOption.VALUE_NONE" as its mode') option = InputOption('foo', 'f', InputOption.VALUE_REQUIRED) self.assertTrue( option.accept_value(), msg='__init__() can take "InputOption.VALUE_REQUIRED" as its mode') self.assertTrue( option.is_value_required(), msg='__init__() can take "InputOption.VALUE_REQUIRED" as its mode') self.assertFalse( option.is_value_optional(), msg='__init__() can take "InputOption.VALUE_REQUIRED" as its mode') option = InputOption('foo', 'f', InputOption.VALUE_OPTIONAL) self.assertTrue( option.accept_value(), msg='__init__() can take "InputOption.VALUE_OPTIONAL" as its mode') self.assertFalse( option.is_value_required(), msg='__init__() can take "InputOption.VALUE_OPTIONAL" as its mode') self.assertTrue( option.is_value_optional(), msg='__init__() can take "InputOption.VALUE_OPTIONAL" as its mode') self.assertRaises(Exception, InputOption, 'foo', 'f', 'ANOTHER_MODE') self.assertRaises(Exception, InputOption, 'foo', 'f', -1) validator = Integer() option = InputOption('foo', 'f', None, validator=validator) self.assertEqual(validator, option.get_validator()) # Named validator validator = 'integer' option = InputOption('foo', 'f', None, validator=validator) self.assertIsInstance(option.get_validator(), Integer) # Native type validator = int option = InputOption('foo', 'f', None, validator=validator) self.assertIsInstance(option.get_validator(), Integer)
def test_validate_with_invalid_type(self): validator = Range(12, 17, validator=Integer()) self.assertRaises(ValidationError, validator.validate, '12.57') self.assertEqual(15, validator.validate(15))