|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import pandas as pd |
| 4 | +import unittest |
| 5 | +import warnings |
| 6 | +import nose |
| 7 | +import numpy as np |
| 8 | +import sys |
| 9 | + |
| 10 | +from pandas.util.testing import ( |
| 11 | + assert_almost_equal, assertRaisesRegexp, raise_with_traceback |
| 12 | +) |
| 13 | + |
| 14 | +# let's get meta. |
| 15 | + |
| 16 | +class TestAssertAlmostEqual(unittest.TestCase): |
| 17 | + _multiprocess_can_split_ = True |
| 18 | + |
| 19 | + def _assert_almost_equal_both(self, a, b, **kwargs): |
| 20 | + assert_almost_equal(a, b, **kwargs) |
| 21 | + assert_almost_equal(b, a, **kwargs) |
| 22 | + |
| 23 | + def _assert_not_almost_equal_both(self, a, b, **kwargs): |
| 24 | + self.assertRaises(AssertionError, assert_almost_equal, a, b, **kwargs) |
| 25 | + self.assertRaises(AssertionError, assert_almost_equal, b, a, **kwargs) |
| 26 | + |
| 27 | + def test_assert_almost_equal_numbers(self): |
| 28 | + self._assert_almost_equal_both(1.1, 1.1) |
| 29 | + self._assert_almost_equal_both(1.1, 1.100001) |
| 30 | + self._assert_almost_equal_both(np.int16(1), 1.000001) |
| 31 | + self._assert_almost_equal_both(np.float64(1.1), 1.1) |
| 32 | + self._assert_almost_equal_both(np.uint32(5), 5) |
| 33 | + |
| 34 | + self._assert_not_almost_equal_both(1.1, 1) |
| 35 | + self._assert_not_almost_equal_both(1.1, True) |
| 36 | + self._assert_not_almost_equal_both(1, 2) |
| 37 | + self._assert_not_almost_equal_both(1.0001, np.int16(1)) |
| 38 | + |
| 39 | + def test_assert_almost_equal_numbers_with_zeros(self): |
| 40 | + self._assert_almost_equal_both(0, 0) |
| 41 | + self._assert_almost_equal_both(0.000001, 0) |
| 42 | + |
| 43 | + self._assert_not_almost_equal_both(0.001, 0) |
| 44 | + self._assert_not_almost_equal_both(1, 0) |
| 45 | + |
| 46 | + def test_assert_almost_equal_numbers_with_mixed(self): |
| 47 | + self._assert_not_almost_equal_both(1, 'abc') |
| 48 | + self._assert_not_almost_equal_both(1, [1,]) |
| 49 | + self._assert_not_almost_equal_both(1, object()) |
| 50 | + |
| 51 | + def test_assert_almost_equal_dicts(self): |
| 52 | + self._assert_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 2}) |
| 53 | + |
| 54 | + self._assert_not_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 3}) |
| 55 | + self._assert_not_almost_equal_both( |
| 56 | + {'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3} |
| 57 | + ) |
| 58 | + self._assert_not_almost_equal_both({'a': 1}, 1) |
| 59 | + self._assert_not_almost_equal_both({'a': 1}, 'abc') |
| 60 | + self._assert_not_almost_equal_both({'a': 1}, [1,]) |
| 61 | + |
| 62 | + def test_assert_almost_equal_dict_like_object(self): |
| 63 | + class DictLikeObj(object): |
| 64 | + def keys(self): |
| 65 | + return ('a',) |
| 66 | + |
| 67 | + def __getitem__(self, item): |
| 68 | + if item == 'a': |
| 69 | + return 1 |
| 70 | + |
| 71 | + self._assert_almost_equal_both({'a': 1}, DictLikeObj()) |
| 72 | + |
| 73 | + self._assert_not_almost_equal_both({'a': 2}, DictLikeObj()) |
| 74 | + |
| 75 | + def test_assert_almost_equal_strings(self): |
| 76 | + self._assert_almost_equal_both('abc', 'abc') |
| 77 | + |
| 78 | + self._assert_not_almost_equal_both('abc', 'abcd') |
| 79 | + self._assert_not_almost_equal_both('abc', 'abd') |
| 80 | + self._assert_not_almost_equal_both('abc', 1) |
| 81 | + self._assert_not_almost_equal_both('abc', [1,]) |
| 82 | + |
| 83 | + def test_assert_almost_equal_iterables(self): |
| 84 | + self._assert_almost_equal_both([1, 2, 3], [1, 2, 3]) |
| 85 | + self._assert_almost_equal_both(np.array([1, 2, 3]), [1, 2, 3]) |
| 86 | + |
| 87 | + # Can't compare generators |
| 88 | + self._assert_not_almost_equal_both(iter([1, 2, 3]), [1, 2, 3]) |
| 89 | + |
| 90 | + self._assert_not_almost_equal_both([1, 2, 3], [1, 2, 4]) |
| 91 | + self._assert_not_almost_equal_both([1, 2, 3], [1, 2, 3, 4]) |
| 92 | + self._assert_not_almost_equal_both([1, 2, 3], 1) |
| 93 | + |
| 94 | + def test_assert_almost_equal_null(self): |
| 95 | + self._assert_almost_equal_both(None, None) |
| 96 | + self._assert_almost_equal_both(None, np.NaN) |
| 97 | + |
| 98 | + self._assert_not_almost_equal_both(None, 0) |
| 99 | + self._assert_not_almost_equal_both(np.NaN, 0) |
| 100 | + |
| 101 | + def test_assert_almost_equal_inf(self): |
| 102 | + self._assert_almost_equal_both(np.inf, np.inf) |
| 103 | + self._assert_almost_equal_both(np.inf, float("inf")) |
| 104 | + |
| 105 | + self._assert_not_almost_equal_both(np.inf, 0) |
| 106 | + |
| 107 | +class TestUtilTesting(unittest.TestCase): |
| 108 | + _multiprocess_can_split_ = True |
| 109 | + |
| 110 | + def test_raise_with_traceback(self): |
| 111 | + with assertRaisesRegexp(LookupError, "error_text"): |
| 112 | + try: |
| 113 | + raise ValueError("THIS IS AN ERROR") |
| 114 | + except ValueError as e: |
| 115 | + e = LookupError("error_text") |
| 116 | + raise_with_traceback(e) |
| 117 | + with assertRaisesRegexp(LookupError, "error_text"): |
| 118 | + try: |
| 119 | + raise ValueError("This is another error") |
| 120 | + except ValueError: |
| 121 | + e = LookupError("error_text") |
| 122 | + _, _, traceback = sys.exc_info() |
| 123 | + raise_with_traceback(e, traceback) |
0 commit comments