Repository: pandas-dev/pandas
File: pandas/tests/indexes/test_setops.py (Python)

Code before:
        # GH#51365
        idx = Index([1, 2, 3], dtype=any_numeric_ea_and_arrow_dtype)
        idx2 = Index([3, 4, 5], dtype=any_numeric_ea_and_arrow_dtype)
        result = idx.union(idx2)
        expected = Index([1, 2, 3, 4, 5], dtype=any_numeric_ea_and_arrow_dtype)
        tm.assert_index_equal(result, expected)

    def test_union_string_array(self, any_string_dtype):
        idx1 = Index(["a"], dtype=any_string_dtype)
        idx2 = Index(["b"], dtype=any_string_dtype)
        result = idx1.union(idx2)
        expected = Index(["a", "b"], dtype=any_string_dtype)
        tm.assert_index_equal(result, expected)

    @td.skip_if_no("pyarrow")
    def test_union_pyarrow_timestamp(self):
        # GH#58421
        left = Index(["2020-01-01"], dtype="timestamp[s][pyarrow]")
        right = Index(["2020-01-02"], dtype="timestamp[s][pyarrow]")

        res = left.union(right)
        expected = Index(["2020-01-01", "2020-01-02"], dtype=left.dtype)
        tm.assert_index_equal(res, expected)


class TestSetOpsMutation:
    def test_intersection_mutation_safety(self):
        # GH#63169
        index1 = Index([0, 1], name="original")
        index2 = Index([0, 1], name="original")

        result = index1.intersection(index2)

        assert result is not index1
        assert result is not index2

        tm.assert_index_equal(result, index1)
        assert result.name == "original"

        index1.name = "changed"

        assert result.name == "original"
        assert index1.name == "changed"

    def test_union_mutation_safety(self):
        # GH#63169
        index1 = Index([0, 1], name="original")
        index2 = Index([0, 1], name="original")

        result = index1.union(index2)


Review comment (suggestion):
We'd like to move away from having test classes in cases where the class itself isn't useful. Can you just make these functions.

Code after:
        # GH#51365
        idx = Index([1, 2, 3], dtype=any_numeric_ea_and_arrow_dtype)
        idx2 = Index([3, 4, 5], dtype=any_numeric_ea_and_arrow_dtype)
        result = idx.union(idx2)
        expected = Index([1, 2, 3, 4, 5], dtype=any_numeric_ea_and_arrow_dtype)
        tm.assert_index_equal(result, expected)

    def test_union_string_array(self, any_string_dtype):
        idx1 = Index(["a"], dtype=any_string_dtype)
        idx2 = Index(["b"], dtype=any_string_dtype)
        result = idx1.union(idx2)
        expected = Index(["a", "b"], dtype=any_string_dtype)
        tm.assert_index_equal(result, expected)

    @td.skip_if_no("pyarrow")
    def test_union_pyarrow_timestamp(self):
        # GH#58421
        left = Index(["2020-01-01"], dtype="timestamp[s][pyarrow]")
        right = Index(["2020-01-02"], dtype="timestamp[s][pyarrow]")

        res = left.union(right)
        expected = Index(["2020-01-01", "2020-01-02"], dtype=left.dtype)
        tm.assert_index_equal(res, expected)


def test_intersection_mutation_safety():
    # GH#63169
    index1 = Index([0, 1], name="original")
    index2 = Index([0, 1], name="original")

    result = index1.intersection(index2)

    assert result is not index1
    assert result is not index2

    tm.assert_index_equal(result, index1)
    assert result.name == "original"

    index1.name = "changed"

    assert result.name == "original"
    assert index1.name == "changed"


def test_union_mutation_safety():
    # GH#63169
    index1 = Index([0, 1], name="original")
    index2 = Index([0, 1], name="original")

    result = index1.union(index2)