def test_allow_all(self): differences = [Missing('xxx'), Extra('yyy')] with allow_only(differences): raise DataError('example error', [Missing('xxx'), Extra('yyy')]) # Order of differences should not matter! differences = [Extra('yyy'), Missing('xxx')] with allow_only(differences): raise DataError('example error', reversed(differences))
def test_no_exception(self): with self.assertRaises(AssertionError) as cm: with allow_only([Missing('xxx')]): pass # No exceptions raised exc = cm.exception self.assertEqual('No differences found: allow_only', str(exc))
def test_allow_one_but_find_duplicate(self): with self.assertRaises(DataError) as cm: with allow_only(Extra('xxx')): raise DataError('example error', [Extra('xxx'), Extra('xxx')]) result_string = str(cm.exception) self.assertEqual("example error:\n xExtra('xxx')", result_string)
def test_allow_duplicate_but_find_only_one(self): with self.assertRaises(DataError) as cm: with allow_only([Extra('xxx'), Extra('xxx')]): raise DataError('example error', [Extra('xxx')]) result_string = str(cm.exception) self.assertEqual("Allowed difference not found:\n Extra('xxx')", result_string)
def test_not_found(self): with self.assertRaises(DataError) as cm: with allow_only([Extra('xxx'), Missing('yyy')]): raise DataError('example error', [Extra('xxx')]) result_str = str(cm.exception) self.assertTrue(result_str.startswith('Allowed difference not found')) result_diffs = list(cm.exception.differences) self.assertEqual([Missing('yyy')], result_diffs)
def test_allow_some(self): with self.assertRaises(DataError) as cm: with allow_only(Extra('xxx'), 'example allowance'): raise DataError('example error', [Extra('xxx'), Missing('yyy')]) result_str = str(cm.exception) self.assertEqual("example allowance: example error:\n Missing('yyy')", result_str) result_diffs = list(cm.exception.differences) self.assertEqual([Missing('yyy')], result_diffs)
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)