예제 #1
0
 def test_lambda(self):
     template = {
         'id':   lambda obj: isinstance(obj, int),
     }
     
     obiwan.duckable({'id': 1}, template)
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {'id': 'a'}, template)
예제 #2
0
    def test_dict(self):
        template = {
            'person': [{
                'id': int,
                obiwan.noneable('name'): str,
                obiwan.optional('age'): int
            }]
        }

        tests = [{
            'person': [{
                'id': 1,
                'name': None
            }]
        }, {
            'person': [{
                'id': 1,
                'name': None,
                'age': 14
            }]
        }, {
            'person': [{
                'id': 1,
                'name': "Adam"
            }]
        }]

        for test in tests:
            obiwan.duckable(test, template)
예제 #3
0
 def test_dict_of_types(self):
     obiwan.duckable({"a": 1, "b": 2}, {str: int})
     obiwan.duckable({"a": 1, "b": 2}, {"a": int, str: int})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable,
         {"a": 1, "b": None}, {str: int})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable,
         {"a": 1, 2: 2}, {str: int})
예제 #4
0
 def test_set_of_type(self):
     obiwan.duckable(set(), {str})
     obiwan.duckable({"a", "b"}, {str})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable,
         {1, 2}, {str})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable,
         {"a", "b", ("a",)}, {str})
예제 #5
0
    def test_lambda(self):
        template = {
            'id': lambda obj: isinstance(obj, int),
        }

        obiwan.duckable({'id': 1}, template)
        self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {'id': 'a'},
                          template)
예제 #6
0
 def test_strict(self):
     template = {
         obiwan.options: [obiwan.strict],
         'id': int,
         obiwan.optional('opt'): int,
     }
     
     obiwan.duckable({'id': 1}, template)
     obiwan.duckable({'id': 1, 'opt': 2}, template)
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {'id': 1, 'x': 2}, template)
예제 #7
0
 def test_dict_of_types(self):
     obiwan.duckable({"a": 1, "b": 2}, {str: int})
     obiwan.duckable({"a": 1, "b": 2}, {"a": int, str: int})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {
         "a": 1,
         "b": None
     }, {str: int})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {
         "a": 1,
         2: 2
     }, {str: int})
예제 #8
0
    def test_strict(self):
        template = {
            obiwan.options: [obiwan.strict],
            'id': int,
            obiwan.optional('opt'): int,
        }

        obiwan.duckable({'id': 1}, template)
        obiwan.duckable({'id': 1, 'opt': 2}, template)
        self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {
            'id': 1,
            'x': 2
        }, template)
예제 #9
0
    def test_dict(self):
        template = {
            'person': [{
                'id': int,
                obiwan.noneable('name'): str,
                obiwan.optional('age'): int
            }]
        }

        tests = [
            {'person': [{'id': 1, 'name': None}]},
            {'person': [{'id': 1, 'name': None, 'age': 14}]},
            {'person': [{'id': 1, 'name': "Adam"}]}
        ]

        for test in tests:
            obiwan.duckable(test, template)
예제 #10
0
    def test_subtype(self):
        base = {
            'id': int,
        }
        parent = {
            obiwan.options: [obiwan.subtype(base)],
            obiwan.optional('y'): int,
        }
        template = {
            obiwan.options: [obiwan.strict, obiwan.subtype(parent)],
            'x': int,
        }

        obiwan.duckable({'id': 1, 'x': 2}, template)
        obiwan.duckable({'id': 1, 'x': 2, 'y': 3}, template)
        self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {'id': 1}, template)        
        self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {'id': 1, 'x': 2, 'z': 3}, template)
예제 #11
0
 def test_bad(self):
     template = {
         'person': [{
                 'id': int,
                 'name': str,
                 'age': int,
         }],
     }
     
     tests = [
         {'person': None },
         {'person': 1 },
         {'person': {'id': 1 }},
         {'person': [{'id': 'str'}]},
         {'person': [{'id': 1, 'name': 'abc', 'age': 14}, 2]},
     ]
     
     for test in tests:
         with self.assertRaises(obiwan.ObiwanError, msg=test):
             obiwan.duckable(test, template)
예제 #12
0
    def test_bad(self):
        template = {
            'person': [{
                'id': int,
                'name': str,
                'age': int,
            }],
        }

        tests = [
            {
                'person': None
            },
            {
                'person': 1
            },
            {
                'person': {
                    'id': 1
                }
            },
            {
                'person': [{
                    'id': 'str'
                }]
            },
            {
                'person': [{
                    'id': 1,
                    'name': 'abc',
                    'age': 14
                }, 2]
            },
        ]

        for test in tests:
            with self.assertRaises(obiwan.ObiwanError, msg=test):
                obiwan.duckable(test, template)
예제 #13
0
    def test_subtype(self):
        base = {
            'id': int,
        }
        parent = {
            obiwan.options: [obiwan.subtype(base)],
            obiwan.optional('y'): int,
        }
        template = {
            obiwan.options: [obiwan.strict,
                             obiwan.subtype(parent)],
            'x': int,
        }

        obiwan.duckable({'id': 1, 'x': 2}, template)
        obiwan.duckable({'id': 1, 'x': 2, 'y': 3}, template)
        self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {'id': 1},
                          template)
        self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {
            'id': 1,
            'x': 2,
            'z': 3
        }, template)
예제 #14
0
 def test_noneable(self):
     obiwan.duckable(None, obiwan.noneable(str))
     obiwan.duckable(None, {str, None}) # using None instead of NoneType
예제 #15
0
 def test_noneable(self):
     obiwan.duckable(None, obiwan.noneable(str))
     obiwan.duckable(None, {str, None})  # using None instead of NoneType
예제 #16
0
 def test_set_of_type(self):
     obiwan.duckable(set(), {str})
     obiwan.duckable({"a", "b"}, {str})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable, {1, 2}, {str})
     self.assertRaises(obiwan.ObiwanError, obiwan.duckable,
                       {"a", "b", ("a", )}, {str})