def test_min_max_len(self):
        str_schema = {
            'checker': {
                'type': 'string',
                'min_length': 2,
                'max_length': 4,
            }
        }

        array_schema = {
            'checker': {
                'type': 'array',
                'min_length': 2,
                'max_length': 4,
            }
        }

        dict_schema = {
            'xx': {
                'checker': {
                    'type': 'dict',
                    'min_length': 2,
                    'max_length': 4,
                }
            }
        }

        cases = (
            ('12', str_schema),
            ('123', str_schema),
            ('1234', str_schema),

            (['a', 'b'], array_schema),
            (['a', 'b', 'c'], array_schema),
            (['a', 'b', 'c', 'd'], array_schema),

            ({'xx': {'a': '', 'b': ''}}, dict_schema),
            ({'xx': {'a': '', 'b': '', 'c': ''}}, dict_schema),
            ({'xx': {'a': '', 'b': '', 'c': '', 'd': ''}}, dict_schema),
        )

        for v, s in cases:
            argchecker.check_arguments(v, s)

        ex_cases = (
            ('1', str_schema),
            ('12345', str_schema),

            (['a'], array_schema),
            (['a', 'b', 'c', 'd', 'e'], array_schema),

            ({'xx': {'a': ''}}, dict_schema),
            ({'xx': {'a': '', 'b': '', 'c': '', 'd': '', 'e': ''}}, dict_schema),
        )

        for v, s in ex_cases:
            self.assertRaises(argchecker.InvalidLengthError,
                              argchecker.check_arguments,
                              v,
                              s)
예제 #2
0
    def test_str_enum(self):
        enum = [
            'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
            'Sunday'
        ]
        schema = {
            'week_day': {
                'checker': {
                    'type': 'string',
                    'enum': enum,
                }
            }
        }

        ex_cases = (
            'xx',
            'foo',
            'bar',
            'today',
            'monday',
            'tUesday',
        )

        for c in ex_cases:
            self.assertRaises(argchecker.NotInEnumError,
                              argchecker.check_arguments, {'week_day': c},
                              schema)

        for c in enum:
            argchecker.check_arguments({'week_day': c}, schema)
예제 #3
0
    def test_checker_with_multi_types(self):
        schema = {
            'str_or_int': {
                'checker': [
                    {
                        'type': 'string'
                    },
                    {
                        'type': 'integer'
                    },
                ]
            }
        }

        cases = (
            'xx',
            123,
        )

        for val in cases:
            argchecker.check_arguments({'str_or_int': val}, schema)

        ex_cases = (
            [],
            (),
            {},
            0.1,
        )

        for val in ex_cases:
            self.assertRaises(argchecker.InvalidTypeError,
                              argchecker.check_arguments, {'str_or_int': val},
                              schema)
예제 #4
0
    def test_string_number(self):
        schema = {
            'num': {
                'checker': {
                    'type': 'string_number',
                    'min': 0,
                    'max': 10,
                    'not': ['5'],
                }
            }
        }

        ex_cases = (
            (1, argchecker.InvalidTypeError),
            ('a', argchecker.InvalidTypeError),
            ('11', argchecker.OutOfRangeError),
            ('-1', argchecker.OutOfRangeError),
            ('5', argchecker.InvalidValueError),
        )

        for val, err in ex_cases:
            self.assertRaises(err, argchecker.check_arguments, {'num': val},
                              schema)

        cases = (
            '10',
            '0',
            '9.99',
            '4.30',
        )

        for val in cases:
            argchecker.check_arguments({'num': val}, schema)
    def test_checker_with_multi_types(self):
        schema = {
            'str_or_int': {
                'checker': [
                    {'type': 'string'},
                    {'type': 'integer'},
                ]
            }
        }

        cases = (
            'xx',
            123,
        )

        for val in cases:
            argchecker.check_arguments({'str_or_int': val}, schema)

        ex_cases = (
            [],
            (),
            {},
            0.1,
        )

        for val in ex_cases:
            self.assertRaises(argchecker.InvalidTypeError,
                              argchecker.check_arguments,
                              {'str_or_int': val},
                              schema)
    def test_str_enum(self):
        enum = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
                'Friday', 'Saturday', 'Sunday']
        schema = {
            'week_day': {
                'checker': {
                    'type': 'string',
                    'enum': enum,
                }
            }
        }

        ex_cases = (
            'xx',
            'foo',
            'bar',
            'today',
            'monday',
            'tUesday',
        )

        for c in ex_cases:
            self.assertRaises(argchecker.NotInEnumError,
                              argchecker.check_arguments,
                              {'week_day': c},
                              schema)

        for c in enum:
            argchecker.check_arguments({'week_day': c}, schema)
    def test_check_arguments(self):
        cases = (
            (True, {'checker': {'type': 'bool'}}),
            (False, {'checker': {'type': 'bool'}}),

            (1, {'checker': {'type': 'integer'}}),
            (100, {'checker': {'type': 'integer'}}),

            (1, {'checker': {'type': 'float'}}),
            (50, {'checker': {'type': 'float'}}),
            (100.0, {'checker': {'type': 'float'}}),

            ('1', {'checker': {'type': 'string_number'}}),
            ('50', {'checker': {'type': 'string_number'}}),
            ('100.0', {'checker': {'type': 'string_number'}}),

            ('20', {'checker': {'type': 'string'}}),
            ('100.0', {'checker': {'type': 'string'}}),
            ('xxx', {'checker': {'type': 'string'}}),

            ([], {'checker': {'type': 'array'}}),
            ([1, 2], {'checker': {'type': 'array'}}),
            (['a', 'b'], {'checker': {'type': 'array'}}),

            ({'xx': {}}, {'xx': {'checker': {'type': 'dict'}}}),
            ({'xx': {'a': ''}}, {'xx': {'checker': {'type': 'dict'}}}),
        )

        for val, schema in cases:
            argchecker.check_arguments(val, schema)
    def test_string_number(self):
        schema = {
            'num': {
                'checker': {
                    'type': 'string_number',
                    'min': 0,
                    'max': 10,
                    'not': ['5'],
                }
            }
        }

        ex_cases = (
            (1, argchecker.InvalidTypeError),
            ('a', argchecker.InvalidTypeError),
            ('11', argchecker.OutOfRangeError),
            ('-1', argchecker.OutOfRangeError),
            ('5', argchecker.InvalidValueError),
        )

        for val, err in ex_cases:
            self.assertRaises(err,
                              argchecker.check_arguments,
                              {'num': val},
                              schema)

        cases = (
            '10',
            '0',
            '9.99',
            '4.30',
        )

        for val in cases:
            argchecker.check_arguments({'num': val}, schema)
예제 #9
0
    def test_any_dict(self):
        schema = {'anything': {'checker': {'type': 'dict'}}}

        argchecker.check_arguments({'anything': {
            '1': 'a',
            2: 'b',
            3: [4]
        }}, schema)
    def test_any_array(self):
        schema = {
            'anything': {
                'checker': {'type': 'array'}
            }
        }

        argchecker.check_arguments({'anything': [[], {}, 0, '']}, schema)
    def test_undefined_arg(self):
        args = {'undefined': 0}
        schema = {'x': {'checker': {'type': 'any'}}}

        with self.assertRaises(argchecker.UnexpectedArgumentError) as e:
            argchecker.check_arguments(args, schema)

        self.assertEqual(e.exception[0], 'undefined')
 def test_any_arg(self):
     schema = {
         'anything': {
             'checker': {'type': 'any'}
         }
     }
     for x in ([], {}, 0, ''):
         argchecker.check_arguments({'anything': x}, schema)
예제 #13
0
    def test_undefined_arg(self):
        args = {'undefined': 0}
        schema = {'x': {'checker': {'type': 'any'}}}

        with self.assertRaises(argchecker.UnexpectedArgumentError) as e:
            argchecker.check_arguments(args, schema)

        self.assertEqual(e.exception[0], 'undefined')
    def test_any_dict(self):
        schema = {
            'anything': {
                'checker': {'type': 'dict'}
            }
        }

        argchecker.check_arguments({'anything': {'1': 'a', 2: 'b', 3: [4]}}, schema)
    def test_required_arg(self):
        schema = {
            'x': {
                'checker': {'type': 'any'},
                'required': True
            }
        }
        with self.assertRaises(argchecker.LackArgumentError) as e:
            argchecker.check_arguments({}, schema)

        self.assertEqual(e.exception[0], 'x', "'x' is required")
    def test_fixed_len(self):
        str_schema = {
            'checker': {
                'type': 'string',
                'fixed_length': 3,
            },
        }

        array_schema = {
            'checker': {
                'type': 'array',
                'fixed_length': 3,
            },
        }

        dict_schema = {
            'xx': {
                'checker': {
                    'type': 'dict',
                    'fixed_length': 3,
                },
            }
        }

        cases = (
            ('abc', str_schema),
            (['a', 'b', 'abc'], array_schema),
            ({'xx': {'a': 'a', 'b': 'b', 'c': 'c'}}, dict_schema),
        )

        for v, s in cases:
            argchecker.check_arguments(v, s)

        ex_cases = (
            ('a', str_schema),
            ('ab', str_schema),
            ('abcd', str_schema),

            (['a'], array_schema),
            (['a', 'b'], array_schema),
            (['a', 'b', 'abc', 'abcd'], array_schema),

            ({'xx': {'a': ''}}, dict_schema),
            ({'xx': {'a': '', 'b': ''}}, dict_schema),
            ({'xx': {'a': '', 'b': '', 'c': '', 'd': ''}}, dict_schema),
        )

        for v, s in ex_cases:
            self.assertRaises(argchecker.InvalidLengthError,
                              argchecker.check_arguments,
                              v,
                              s)
예제 #17
0
    def test_str_regexp(self):
        schema = {
            'alphabet_only': {
                'checker': {
                    'type': 'string',
                    'regexp': "^[a-z]+$"
                }
            }
        }

        with self.assertRaises(argchecker.PatternNotMatchError):
            argchecker.check_arguments({'alphabet_only': 'abcd123'}, schema)

        argchecker.check_arguments({'alphabet_only': 'abcd'}, schema)
    def test_str_regexp(self):
        schema = {
            'alphabet_only': {
                'checker': {
                    'type': 'string',
                    'regexp': "^[a-z]+$"
                }
            }
        }

        with self.assertRaises(argchecker.PatternNotMatchError):
            argchecker.check_arguments({'alphabet_only': 'abcd123'}, schema)

        argchecker.check_arguments({'alphabet_only': 'abcd'}, schema)
예제 #19
0
    def test_string_and_string_number(self):
        schema = {
            'num': {
                'checker': [{
                    'type': 'string_number'
                }, {
                    'type': 'string',
                    'enum': ['auto']
                }]
            }
        }

        cases = (
            '123',
            'auto',
        )

        for val in cases:
            argchecker.check_arguments({'num': val}, schema)
예제 #20
0
    def test_dict_arg(self):
        schema = {
            'log_counter': {
                'checker': {
                    'type': 'dict',
                    'key_checker': {
                        'type': 'string',
                        'enum': ['info', 'error', 'warn']
                    },
                    'value_checker': {
                        'type': 'integer',
                        'min': 0
                    }
                }
            }
        }

        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'log_counter': {1: 2}}, schema)

        with self.assertRaises(argchecker.NotInEnumError):
            argchecker.check_arguments({'log_counter': {'err': 1}}, schema)

        argchecker.check_arguments({'log_counter': {
            'error': 2,
            'info': 1
        }}, schema)
    def test_dict_arg(self):
        schema = {
            'log_counter': {
                'checker': {
                    'type': 'dict',
                    'key_checker': {
                        'type': 'string',
                        'enum': ['info', 'error', 'warn']
                    },
                    'value_checker': {
                        'type': 'integer',
                        'min': 0
                    }
                }
            }
        }

        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'log_counter': {1: 2}}, schema)

        with self.assertRaises(argchecker.NotInEnumError):
            argchecker.check_arguments({'log_counter': {'err': 1}}, schema)

        argchecker.check_arguments({'log_counter': {'error': 2, 'info': 1}},
                                   schema)
    def test_float_arg(self):
        schema = {
            'num': {
                'required': True,
                'checker': {
                    'type': 'float',
                    'min': -100,
                    'max': 100,
                    'not': [0]
                }
            }
        }

        ex_cases = (
            ('1', argchecker.InvalidTypeError),
            (False, argchecker.InvalidTypeError),
            (-101, argchecker.OutOfRangeError),
            (101, argchecker.OutOfRangeError),
            (0, argchecker.InvalidValueError),
        )

        for val, err in ex_cases:
            self.assertRaises(err,
                              argchecker.check_arguments,
                              {'num': val},
                              schema)

        cases = (
            -100,
            -50,
            -10.88,
            50,
            99.99,
            100,
        )

        for val in cases:
            argchecker.check_arguments({'num': val}, schema)
    def test_string_and_string_number(self):
        schema = {
            'num': {
                'checker': [
                    {
                        'type': 'string_number'
                    },

                    {
                        'type': 'string',
                        'enum': ['auto']
                    }
                ]
            }
        }

        cases = (
            '123',
            'auto',
        )

        for val in cases:
            argchecker.check_arguments({'num': val}, schema)
예제 #24
0
    def test_integer_arg(self):
        schema = {'int': {'checker': {'type': 'integer'}}}
        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'int': 1.1}, schema)

        argchecker.check_arguments({'int': 100}, schema)

        long_num = 10**21
        argchecker.check_arguments({'int': long_num}, schema)
예제 #25
0
    def test_float_arg(self):
        schema = {
            'num': {
                'required': True,
                'checker': {
                    'type': 'float',
                    'min': -100,
                    'max': 100,
                    'not': [0]
                }
            }
        }

        ex_cases = (
            ('1', argchecker.InvalidTypeError),
            (False, argchecker.InvalidTypeError),
            (-101, argchecker.OutOfRangeError),
            (101, argchecker.OutOfRangeError),
            (0, argchecker.InvalidValueError),
        )

        for val, err in ex_cases:
            self.assertRaises(err, argchecker.check_arguments, {'num': val},
                              schema)

        cases = (
            -100,
            -50,
            -10.88,
            50,
            99.99,
            100,
        )

        for val in cases:
            argchecker.check_arguments({'num': val}, schema)
    def test_array_arg(self):
        schema = {
            'numbers': {
                'checker': {
                    'type': 'array',
                    'element_checker': {
                        'type': 'integer'
                    }
                }
            }
        }
        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'numbers': 123}, schema)

        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'numbers': [1, 2, 'a']}, schema)

        argchecker.check_arguments({'numbers': []}, schema)
        argchecker.check_arguments({'numbers': [1, 2]}, schema)
예제 #27
0
    def test_array_arg(self):
        schema = {
            'numbers': {
                'checker': {
                    'type': 'array',
                    'element_checker': {
                        'type': 'integer'
                    }
                }
            }
        }
        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'numbers': 123}, schema)

        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'numbers': [1, 2, 'a']}, schema)

        argchecker.check_arguments({'numbers': []}, schema)
        argchecker.check_arguments({'numbers': [1, 2]}, schema)
    def test_integer_arg(self):
        schema = {
            'int': {
                'checker': {
                    'type': 'integer'
                }
            }
        }
        with self.assertRaises(argchecker.InvalidTypeError):
            argchecker.check_arguments({'int': 1.1}, schema)

        argchecker.check_arguments({'int': 100}, schema)

        long_num = 10 ** 21
        argchecker.check_arguments({'int': long_num}, schema)
예제 #29
0
    def test_nested_shema(self):
        sub_schema = {'nested_arg': {'checker': {'type': 'string'}}}
        schema = {
            'arg': {
                'checker': {
                    'type': 'dict',
                    'sub_schema': sub_schema
                }
            }
        }

        with self.assertRaises(argchecker.UnexpectedArgumentError) as e:
            argchecker.check_arguments({'arg': {'invalid_arg': '123'}}, schema)
        self.assertEqual(e.exception[0], 'invalid_arg')

        with self.assertRaises(argchecker.InvalidTypeError) as e:
            argchecker.check_arguments({'arg': {'nested_arg': 1}}, schema)
        self.assertEqual(e.exception[0], 'nested_arg')

        argchecker.check_arguments({'arg': {'nested_arg': '123'}}, schema)
    def test_nested_shema(self):
        sub_schema = {
            'nested_arg': {
                'checker': {'type': 'string'}
            }
        }
        schema = {
            'arg': {
                'checker': {
                    'type': 'dict',
                    'sub_schema': sub_schema
                }
            }
        }

        with self.assertRaises(argchecker.UnexpectedArgumentError) as e:
            argchecker.check_arguments({'arg': {'invalid_arg': '123'}}, schema)
        self.assertEqual(e.exception[0], 'invalid_arg')

        with self.assertRaises(argchecker.InvalidTypeError) as e:
            argchecker.check_arguments({'arg': {'nested_arg': 1}}, schema)
        self.assertEqual(e.exception[0], 'nested_arg')

        argchecker.check_arguments({'arg': {'nested_arg': '123'}}, schema)
예제 #31
0
    def test_check_arguments(self):
        cases = (
            (True, {
                'checker': {
                    'type': 'bool'
                }
            }),
            (False, {
                'checker': {
                    'type': 'bool'
                }
            }),
            (1, {
                'checker': {
                    'type': 'integer'
                }
            }),
            (100, {
                'checker': {
                    'type': 'integer'
                }
            }),
            (1, {
                'checker': {
                    'type': 'float'
                }
            }),
            (50, {
                'checker': {
                    'type': 'float'
                }
            }),
            (100.0, {
                'checker': {
                    'type': 'float'
                }
            }),
            ('1', {
                'checker': {
                    'type': 'string_number'
                }
            }),
            ('50', {
                'checker': {
                    'type': 'string_number'
                }
            }),
            ('100.0', {
                'checker': {
                    'type': 'string_number'
                }
            }),
            ('20', {
                'checker': {
                    'type': 'string'
                }
            }),
            ('100.0', {
                'checker': {
                    'type': 'string'
                }
            }),
            ('xxx', {
                'checker': {
                    'type': 'string'
                }
            }),
            ([], {
                'checker': {
                    'type': 'array'
                }
            }),
            ([1, 2], {
                'checker': {
                    'type': 'array'
                }
            }),
            (['a', 'b'], {
                'checker': {
                    'type': 'array'
                }
            }),
            ({
                'xx': {}
            }, {
                'xx': {
                    'checker': {
                        'type': 'dict'
                    }
                }
            }),
            ({
                'xx': {
                    'a': ''
                }
            }, {
                'xx': {
                    'checker': {
                        'type': 'dict'
                    }
                }
            }),
        )

        for val, schema in cases:
            argchecker.check_arguments(val, schema)
예제 #32
0
    def test_fixed_len(self):
        str_schema = {
            'checker': {
                'type': 'string',
                'fixed_length': 3,
            },
        }

        array_schema = {
            'checker': {
                'type': 'array',
                'fixed_length': 3,
            },
        }

        dict_schema = {
            'xx': {
                'checker': {
                    'type': 'dict',
                    'fixed_length': 3,
                },
            }
        }

        cases = (
            ('abc', str_schema),
            (['a', 'b', 'abc'], array_schema),
            ({
                'xx': {
                    'a': 'a',
                    'b': 'b',
                    'c': 'c'
                }
            }, dict_schema),
        )

        for v, s in cases:
            argchecker.check_arguments(v, s)

        ex_cases = (
            ('a', str_schema),
            ('ab', str_schema),
            ('abcd', str_schema),
            (['a'], array_schema),
            (['a', 'b'], array_schema),
            (['a', 'b', 'abc', 'abcd'], array_schema),
            ({
                'xx': {
                    'a': ''
                }
            }, dict_schema),
            ({
                'xx': {
                    'a': '',
                    'b': ''
                }
            }, dict_schema),
            ({
                'xx': {
                    'a': '',
                    'b': '',
                    'c': '',
                    'd': ''
                }
            }, dict_schema),
        )

        for v, s in ex_cases:
            self.assertRaises(argchecker.InvalidLengthError,
                              argchecker.check_arguments, v, s)
예제 #33
0
    def test_min_max_len(self):
        str_schema = {
            'checker': {
                'type': 'string',
                'min_length': 2,
                'max_length': 4,
            }
        }

        array_schema = {
            'checker': {
                'type': 'array',
                'min_length': 2,
                'max_length': 4,
            }
        }

        dict_schema = {
            'xx': {
                'checker': {
                    'type': 'dict',
                    'min_length': 2,
                    'max_length': 4,
                }
            }
        }

        cases = (
            ('12', str_schema),
            ('123', str_schema),
            ('1234', str_schema),
            (['a', 'b'], array_schema),
            (['a', 'b', 'c'], array_schema),
            (['a', 'b', 'c', 'd'], array_schema),
            ({
                'xx': {
                    'a': '',
                    'b': ''
                }
            }, dict_schema),
            ({
                'xx': {
                    'a': '',
                    'b': '',
                    'c': ''
                }
            }, dict_schema),
            ({
                'xx': {
                    'a': '',
                    'b': '',
                    'c': '',
                    'd': ''
                }
            }, dict_schema),
        )

        for v, s in cases:
            argchecker.check_arguments(v, s)

        ex_cases = (
            ('1', str_schema),
            ('12345', str_schema),
            (['a'], array_schema),
            (['a', 'b', 'c', 'd', 'e'], array_schema),
            ({
                'xx': {
                    'a': ''
                }
            }, dict_schema),
            ({
                'xx': {
                    'a': '',
                    'b': '',
                    'c': '',
                    'd': '',
                    'e': ''
                }
            }, dict_schema),
        )

        for v, s in ex_cases:
            self.assertRaises(argchecker.InvalidLengthError,
                              argchecker.check_arguments, v, s)
예제 #34
0
 def test_any_arg(self):
     schema = {'anything': {'checker': {'type': 'any'}}}
     for x in ([], {}, 0, ''):
         argchecker.check_arguments({'anything': x}, schema)
예제 #35
0
    def test_any_array(self):
        schema = {'anything': {'checker': {'type': 'array'}}}

        argchecker.check_arguments({'anything': [[], {}, 0, '']}, schema)
예제 #36
0
    def test_required_arg(self):
        schema = {'x': {'checker': {'type': 'any'}, 'required': True}}
        with self.assertRaises(argchecker.LackArgumentError) as e:
            argchecker.check_arguments({}, schema)

        self.assertEqual(e.exception[0], 'x', "'x' is required")