Ejemplo n.º 1
0
    def test_required_integer(self):
        data = set([11, 8])
        integer_val = 11
        result = _compare_other(data, integer_val)
        self.assertEqual(result, [Deviation(-3, integer_val)])

        data = {'foo': 11, 'bar': 8}
        integer_val = 11
        result = _compare_other(data, integer_val)
        self.assertEqual(result, {'bar': Deviation(-3, integer_val)})
Ejemplo n.º 2
0
    def test_iterable(self):
        isalpha = lambda x: x.isalpha()

        data = iter(['a', 'b', 'c'])
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [])

        data = iter(['a', 'b', 'c', '9'])
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [Invalid('9')])
Ejemplo n.º 3
0
    def test_sequence(self):
        isalpha = lambda x: x.isalpha()

        data = ['a', 'b', 'c']
        result = _compare_other(data, isalpha)
        self.assertEqual(result, {})

        data = ['a', 'b', 'c', '9']
        result = _compare_other(data, isalpha)
        self.assertEqual(result, {3: Invalid('9')})
Ejemplo n.º 4
0
    def test_mapping(self):
        isalpha = lambda x: x.isalpha()

        data = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c'}
        result = _compare_other(data, isalpha)
        self.assertEqual(result, {})

        data = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c', 'DDD': '3'}
        result = _compare_other(data, isalpha)
        self.assertEqual(result, {'DDD': Invalid('3')})
Ejemplo n.º 5
0
    def test_set(self):
        isalpha = lambda x: x.isalpha()

        data = set(['a', 'b', 'c'])
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [])

        data = set(['a', 'b', 'c', '3'])
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [Invalid('3')])
Ejemplo n.º 6
0
    def test_multiargument_callable(self):
        """Should unpack arguments if callable expects multiple
        parameters.
        """
        data = set([(5, 2), (1, 4), (10, 8)])

        required = lambda x, y: x > y  # <- Multiple positional parameters.
        result = _compare_other(data, required)
        self.assertEqual(result, [Invalid((1, 4))])

        required = lambda *z: z[0] > z[1]  # <- Variable parameters.
        result = _compare_other(data, required)
        self.assertEqual(result, [Invalid((1, 4))])

        required = lambda a: a[0] > a[1]  # <- Single parameter.
        result = _compare_other(data, required)
        self.assertEqual(result, [Invalid((1, 4))])

        data = [[], [], []]
        required = lambda x, y: x > y  # <- Multiple positional params.
        with self.assertRaisesRegex(TypeError, 'missing 2|0 given'):
            _compare_other(data, required)

        data = (5, 2)
        required = lambda x, y: x > y  # <- Multiple positional params.
        with self.assertRaisesRegex(TypeError, 'missing 1|1 given'):
            _compare_other(data, required)

        data = set([(5, 2), (1, 4), (10, 8)])  # Args and params match

        def required(x, y):  # but function raises
            raise TypeError('other error')  # some other TypeError.

        with self.assertRaisesRegex(TypeError, 'other error'):
            _compare_other(data, required)
Ejemplo n.º 7
0
    def test_error_condition(self):
        """If callable raises an Exception, the result is counted as
        False.
        """
        isalpha = lambda x: x.isalpha()  # Raises TypeError if given
        # a non-string value.

        data = set(['a', 'b', 3, '4'])  # <- Value 3 raises an error.
        result = _compare_other(data, isalpha)
        expected = [Invalid(3), Invalid('4')]
        self.assertEqual(set(result), set(expected))

        data = 10
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [Invalid(data)])
Ejemplo n.º 8
0
    def test_str_or_noniterable(self):
        isalpha = lambda x: x.isalpha()

        data = 'ABCD'
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [])

        data = '!@#$'
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [Invalid('!@#$')])

        data = 5
        required = lambda x: 10 < x
        result = _compare_other(data, required)
        self.assertEqual(result, [Invalid(5)])
Ejemplo n.º 9
0
 def test_required_string(self):
     data = set(['AAA', 'BBB'])
     string_val = 'AAA'
     result = _compare_other(data, string_val)
     self.assertEqual(result, [Invalid('BBB')])
Ejemplo n.º 10
0
 def test_required_regex(self):
     data = set(['a1', 'b2', 'c3', 'd', 'e5'])
     regex = re.compile('[a-z][0-9]+')
     result = _compare_other(data, regex)
     self.assertEqual(result, [Invalid('d')])