def test_string_predicate(self): with self.assertRaises(ValidationError) as cm: with AcceptedArgs('bbb'): # <- Acceptance! raise ValidationError([ Missing('aaa'), Missing('bbb'), Extra('bbb'), ]) remaining_diffs = cm.exception.differences self.assertEqual(list(remaining_diffs), [Missing('aaa')])
def test_multiarg_predicate(self): with self.assertRaises(ValidationError) as cm: def func(diff): return diff < 2 with AcceptedArgs((func, 5)): raise ValidationError([ Deviation(+1, 5), Deviation(+2, 5), ]) remaining_diffs = cm.exception.differences self.assertEqual(list(remaining_diffs), [Deviation(+2, 5)])
def test_function_predicate(self): with self.assertRaises(ValidationError) as cm: def function(args): diff, expected = args return diff < 2 and expected == 5 with AcceptedArgs(function): # <- Acceptance! raise ValidationError([ Deviation(+1, 5), Deviation(+2, 5), ]) remaining_diffs = cm.exception.differences self.assertEqual(list(remaining_diffs), [Deviation(+2, 5)])
def test_integration_examples(self): # Test acceptance of +/- 2 OR +/- 6%. with self.assertRaises(ValidationError) as cm: differences = [ Deviation(+2, 1), # 200% Deviation(+4, 8), # 50% Deviation(+8, 32), # 25% ] with AcceptedTolerance(2) | AcceptedPercent(0.25): raise ValidationError(differences) remaining = cm.exception.differences self.assertEqual(remaining, [Deviation(+4, 8)]) # Test missing-type AND matching-value. with self.assertRaises(ValidationError) as cm: differences = [ Missing('A'), Missing('B'), Extra('C'), ] with AcceptedDifferences(Missing) & AcceptedArgs( lambda x: x == 'A'): raise ValidationError(differences) remaining = cm.exception.differences self.assertEqual(remaining, [Missing('B'), Extra('C')]) # Test missing-type OR accepted-limit. with self.assertRaises(ValidationError) as cm: differences = [ Extra('A'), Missing('B'), Extra('C'), Missing('D'), ] with AcceptedCount(1) | AcceptedDifferences(Missing): raise ValidationError(differences) remaining = cm.exception.differences self.assertEqual(remaining, [Extra('C')]) # Test missing-type AND accepted-limit. with self.assertRaises(ValidationError) as cm: differences = [ Extra('A'), Missing('B'), Missing('C'), ] with AcceptedCount(1) & AcceptedDifferences( Missing): # Accepts only 1 missing. raise ValidationError(differences) remaining = cm.exception.differences self.assertEqual(remaining, [Extra('A'), Missing('C')]) # Test missing-type OR accepted-limit. with self.assertRaises(ValidationError) as cm: differences = [ Extra('A'), Missing('B'), Extra('C'), Missing('D'), ] with AcceptedCount(1) | AcceptedDifferences(Extra('A')): raise ValidationError(differences) remaining = cm.exception.differences self.assertEqual(remaining, [Extra('C'), Missing('D')])