|
| 1 | +import re |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 |
|
3 | 5 | from pandas.core.indexes.frozen import FrozenList
|
4 |
| -from pandas.tests.test_base import CheckImmutable, CheckStringMixin |
5 | 6 |
|
6 | 7 |
|
7 |
| -class TestFrozenList(CheckImmutable, CheckStringMixin): |
| 8 | +class CheckImmutableMixin: |
| 9 | + mutable_regex = re.compile("does not support mutable operations") |
| 10 | + |
| 11 | + def check_mutable_error(self, *args, **kwargs): |
| 12 | + # Pass whatever function you normally would to pytest.raises |
| 13 | + # (after the Exception kind). |
| 14 | + with pytest.raises(TypeError): |
| 15 | + self.mutable_regex(*args, **kwargs) |
| 16 | + |
| 17 | + def test_no_mutable_funcs(self): |
| 18 | + def setitem(): |
| 19 | + self.container[0] = 5 |
| 20 | + |
| 21 | + self.check_mutable_error(setitem) |
| 22 | + |
| 23 | + def setslice(): |
| 24 | + self.container[1:2] = 3 |
| 25 | + |
| 26 | + self.check_mutable_error(setslice) |
| 27 | + |
| 28 | + def delitem(): |
| 29 | + del self.container[0] |
| 30 | + |
| 31 | + self.check_mutable_error(delitem) |
| 32 | + |
| 33 | + def delslice(): |
| 34 | + del self.container[0:3] |
| 35 | + |
| 36 | + self.check_mutable_error(delslice) |
| 37 | + mutable_methods = getattr(self, "mutable_methods", []) |
| 38 | + |
| 39 | + for meth in mutable_methods: |
| 40 | + self.check_mutable_error(getattr(self.container, meth)) |
| 41 | + |
| 42 | + def test_slicing_maintains_type(self): |
| 43 | + result = self.container[1:2] |
| 44 | + expected = self.lst[1:2] |
| 45 | + self.check_result(result, expected) |
| 46 | + |
| 47 | + def check_result(self, result, expected, klass=None): |
| 48 | + klass = klass or self.klass |
| 49 | + assert isinstance(result, klass) |
| 50 | + assert result == expected |
| 51 | + |
| 52 | + |
| 53 | +class CheckStringMixin: |
| 54 | + def test_string_methods_dont_fail(self): |
| 55 | + repr(self.container) |
| 56 | + str(self.container) |
| 57 | + bytes(self.container) |
| 58 | + |
| 59 | + def test_tricky_container(self): |
| 60 | + if not hasattr(self, "unicode_container"): |
| 61 | + pytest.skip("Need unicode_container to test with this") |
| 62 | + repr(self.unicode_container) |
| 63 | + str(self.unicode_container) |
| 64 | + |
| 65 | + |
| 66 | +class TestFrozenList(CheckImmutableMixin, CheckStringMixin): |
8 | 67 | mutable_methods = ("extend", "pop", "remove", "insert")
|
9 | 68 | unicode_container = FrozenList(["\u05d0", "\u05d1", "c"])
|
10 | 69 |
|
|
0 commit comments