def test_required_integer(self): data = set([11, 8]) integer_val = 11 result = _compare_other(data, integer_val) self.assertEqual(result, [xDeviation(-3, integer_val)]) data = {'foo': 11, 'bar': 8} integer_val = 11 result = _compare_other(data, integer_val) self.assertEqual(result, {'bar': xDeviation(-3, integer_val)})
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, [xInvalid('9')])
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: xInvalid('9')})
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': xInvalid('3')})
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, [xInvalid('3')])
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, [xInvalid((1, 4))]) required = lambda *z: z[0] > z[1] # <- Variable parameters. result = _compare_other(data, required) self.assertEqual(result, [xInvalid((1, 4))]) required = lambda a: a[0] > a[1] # <- Single parameter. result = _compare_other(data, required) self.assertEqual(result, [xInvalid((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)
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 = [xInvalid(3), xInvalid('4')] self.assertEqual(set(result), set(expected)) data = 10 result = _compare_other(data, isalpha) self.assertEqual(result, [xInvalid(data)])
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, [xInvalid('!@#$')]) data = 5 required = lambda x: 10 < x result = _compare_other(data, required) self.assertEqual(result, [xInvalid(5)])
def test_required_string(self): data = set(['AAA', 'BBB']) string_val = 'AAA' result = _compare_other(data, string_val) self.assertEqual(result, [xInvalid('BBB')])
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, [xInvalid('d')])