def test_empty_value_handling(self): # Test NoneType. with allow_percent_deviation(0): # <- Pass without failure. raise DataError('example error', [xDeviation(None, 0)]) with allow_percent_deviation(0): # <- Pass without failure. raise DataError('example error', [xDeviation(0, None)]) # Test empty string. with allow_percent_deviation(0): # <- Pass without failure. raise DataError('example error', [xDeviation('', 0)]) with allow_percent_deviation(0): # <- Pass without failure. raise DataError('example error', [xDeviation(0, '')]) # Test NaN (not a number) values. with self.assertRaises( DataError): # <- NaN values should not be caught! with allow_percent_deviation(0): raise DataError('example error', [xDeviation(float('nan'), 0)]) with self.assertRaises( DataError): # <- NaN values should not be caught! with allow_percent_deviation(0): raise DataError('example error', [xDeviation(0, float('nan'))])
def test_nested_allowances(self): """A quick integration test to make sure allowances nest as required. """ with allow_only(Deviation(-4, 70, label1='b')): # <- specified diff only with allow_deviation(3): # <- tolerance of +/- 3 with allow_percent_deviation(0.02): # <- tolerance of +/- 2% differences = [ Deviation(+3, 65, label1='a'), Deviation(-4, 70, label1='b'), Deviation(+5, 250, label1='c'), ] raise DataError('example error', differences)
def test_lowerupper_syntax(self): differences = [ Deviation(-1, 10, label='aaa'), # <- Not in allowed range. Deviation(+3, 10, label='bbb'), ] with self.assertRaises(DataError) as cm: with allow_percent_deviation(0.0, 0.3, 'example allowance'): # <- Allows from 0 to 30%. raise DataError('example error', differences) result_string = str(cm.exception) self.assertTrue(result_string.startswith('example allowance: example error')) result_diffs = list(cm.exception.differences) self.assertEqual([Deviation(-1, 10, label='aaa')], result_diffs)
def test_tolerance_syntax(self): differences = [ xDeviation(-1, 10, label='aaa'), xDeviation(+3, 10, label='bbb'), # <- Not in allowed range. ] with self.assertRaises(DataError) as cm: with allow_percent_deviation( 0.2, 'example message'): # <- Allows +/- 20%. raise DataError('example error', differences) result_string = str(cm.exception) self.assertTrue( result_string.startswith('example message: example error')) result_diffs = list(cm.exception.differences) self.assertEqual([xDeviation(+3, 10, label='bbb')], result_diffs)
def test_single_value_message(self): differences = [ xDeviation(+2.9, 10, label='aaa'), # <- Not accepted. xDeviation(+3.0, 10, label='bbb'), xDeviation(+6.0, 20, label='ccc'), xDeviation(+3.1, 10, label='ddd'), # <- Not accepted. ] with self.assertRaises(DataError) as cm: with allow_percent_deviation(0.3, 0.3): # <- Allows +30% only. raise DataError('example error', differences) result_diffs = set(cm.exception.differences) expected_diffs = set([ xDeviation(+2.9, 10, label='aaa'), xDeviation(+3.1, 10, label='ddd'), ]) self.assertEqual(expected_diffs, result_diffs)
def test_kwds_handling(self): differences = [ Deviation(-1, 10, label='aaa'), Deviation(+2, 10, label='aaa'), Deviation(+2, 10, label='bbb'), Deviation(+3, 10, label='aaa'), ] with self.assertRaises(DataError) as cm: with allow_percent_deviation(0.2, 'example allowance', label='aaa'): # <- Allows +/- 20%. raise DataError('example error', differences) result_set = set(cm.exception.differences) expected_set = set([ Deviation(+2, 10, label='bbb'), # <- Keyword value not 'aaa'. Deviation(+3, 10, label='aaa'), # <- Not in allowed range. ]) self.assertEqual(expected_set, result_set)
def test_invalid_tolerance(self): with self.assertRaises(ValueError) as cm: with allow_percent_deviation(-0.5): # <- invalid pass exc = str(cm.exception) self.assertTrue(exc.startswith('tolerance should not be negative'))