Skip to content

CLN: Refactor test_base - part1 #30110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions pandas/tests/indexes/test_frozen.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
import re

import pytest

from pandas.core.indexes.frozen import FrozenList
from pandas.tests.test_base import CheckImmutable, CheckStringMixin


class TestFrozenList(CheckImmutable, CheckStringMixin):
class CheckImmutableMixin:
mutable_regex = re.compile("does not support mutable operations")

def check_mutable_error(self, *args, **kwargs):
# Pass whatever function you normally would to pytest.raises
# (after the Exception kind).
with pytest.raises(TypeError):
self.mutable_regex(*args, **kwargs)

def test_no_mutable_funcs(self):
def setitem():
self.container[0] = 5

self.check_mutable_error(setitem)

def setslice():
self.container[1:2] = 3

self.check_mutable_error(setslice)

def delitem():
del self.container[0]

self.check_mutable_error(delitem)

def delslice():
del self.container[0:3]

self.check_mutable_error(delslice)
mutable_methods = getattr(self, "mutable_methods", [])

for meth in mutable_methods:
self.check_mutable_error(getattr(self.container, meth))

def test_slicing_maintains_type(self):
result = self.container[1:2]
expected = self.lst[1:2]
self.check_result(result, expected)

def check_result(self, result, expected, klass=None):
klass = klass or self.klass
assert isinstance(result, klass)
assert result == expected


class CheckStringMixin:
def test_string_methods_dont_fail(self):
repr(self.container)
str(self.container)
bytes(self.container)

def test_tricky_container(self):
if not hasattr(self, "unicode_container"):
pytest.skip("Need unicode_container to test with this")
repr(self.unicode_container)
str(self.unicode_container)


class TestFrozenList(CheckImmutableMixin, CheckStringMixin):
mutable_methods = ("extend", "pop", "remove", "insert")
unicode_container = FrozenList(["\u05d0", "\u05d1", "c"])

Expand Down
59 changes: 0 additions & 59 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime, timedelta
from io import StringIO
import re
import sys

import numpy as np
Expand Down Expand Up @@ -40,64 +39,6 @@
import pandas.util.testing as tm


class CheckStringMixin:
def test_string_methods_dont_fail(self):
repr(self.container)
str(self.container)
bytes(self.container)

def test_tricky_container(self):
if not hasattr(self, "unicode_container"):
pytest.skip("Need unicode_container to test with this")
repr(self.unicode_container)
str(self.unicode_container)


class CheckImmutable:
mutable_regex = re.compile("does not support mutable operations")

def check_mutable_error(self, *args, **kwargs):
# Pass whatever function you normally would to pytest.raises
# (after the Exception kind).
with pytest.raises(TypeError):
self.mutable_regex(*args, **kwargs)

def test_no_mutable_funcs(self):
def setitem():
self.container[0] = 5

self.check_mutable_error(setitem)

def setslice():
self.container[1:2] = 3

self.check_mutable_error(setslice)

def delitem():
del self.container[0]

self.check_mutable_error(delitem)

def delslice():
del self.container[0:3]

self.check_mutable_error(delslice)
mutable_methods = getattr(self, "mutable_methods", [])

for meth in mutable_methods:
self.check_mutable_error(getattr(self.container, meth))

def test_slicing_maintains_type(self):
result = self.container[1:2]
expected = self.lst[1:2]
self.check_result(result, expected)

def check_result(self, result, expected, klass=None):
klass = klass or self.klass
assert isinstance(result, klass)
assert result == expected


class TestPandasDelegate:
class Delegator:
_properties = ["foo"]
Expand Down