Repository: pandas-dev/pandas
File: pandas/tests/extension/test_arrow.py (Python)

Code before:
    A < B and NA missing.
    """
    return type(data_for_grouping)._from_sequence(
        [data_for_grouping[0], data_for_grouping[2], data_for_grouping[4]],
        dtype=data_for_grouping.dtype,
    )


@pytest.fixture
def data_for_twos(data):
    """Length-100 array in which all the elements are two."""
    pa_dtype = data.dtype.pyarrow_dtype
    if (
        pa.types.is_integer(pa_dtype)
        or pa.types.is_floating(pa_dtype)
        or pa.types.is_decimal(pa_dtype)
        or pa.types.is_duration(pa_dtype)
    ):
        return pd.array([2] * 10, dtype=data.dtype)
    # tests will be xfailed where 2 is not a valid scalar for pa_dtype
    return data
    # TODO: skip otherwise?


@pytest.fixture
def arrow_to_numpy_tracker(monkeypatch):
    """
    Fixture to track if ArrowExtensionArray.to_numpy() was called.

    Returns a callable that returns True if to_numpy was called since the last check,
    and resets the tracker.
    """
    from pandas.core.arrays.arrow import ArrowExtensionArray

    called = False
    original_to_numpy = ArrowExtensionArray.to_numpy

    def tracked_to_numpy(self, *args, **kwargs):
        nonlocal called
        called = True
        return original_to_numpy(self, *args, **kwargs)

    monkeypatch.setattr(ArrowExtensionArray, "to_numpy", tracked_to_numpy)

    def was_called():
        nonlocal called
        result = called
        called = False
        return result

    return was_called

Review comment (performance):
I think it's OK to omit a test for this. We'll just call this a performance improvement that's already tested for behavior

Code after:
    A < B and NA missing.
    """
    return type(data_for_grouping)._from_sequence(
        [data_for_grouping[0], data_for_grouping[2], data_for_grouping[4]],
        dtype=data_for_grouping.dtype,
    )


@pytest.fixture
def data_for_twos(data):
    """Length-100 array in which all the elements are two."""
    pa_dtype = data.dtype.pyarrow_dtype
    if (
        pa.types.is_integer(pa_dtype)
        or pa.types.is_floating(pa_dtype)
        or pa.types.is_decimal(pa_dtype)
        or pa.types.is_duration(pa_dtype)
    ):
        return pd.array([2] * 10, dtype=data.dtype)
    # tests will be xfailed where 2 is not a valid scalar for pa_dtype
    return data
    # TODO: skip otherwise?


class TestArrowArray(base.ExtensionTests):
    def _construct_for_combine_add(self, left, right):
        dtype = left.dtype

        # in a couple cases, addition is not dtype-preserving
        if dtype == "bool[pyarrow]":
            dtype = pandas_dtype("int64[pyarrow]")
        elif dtype == "int8[pyarrow]" and isinstance(right, type(left)):
            dtype = pandas_dtype("int64[pyarrow]")

        if isinstance(right, type(left)):
            return left._from_sequence(
                [a + b for (a, b) in zip(list(left), list(right), strict=True)],
                dtype=dtype,
            )
        else:
            return left._from_sequence(
                [a + right for a in list(left)],
                dtype=dtype,
            )

    def test_compare_scalar(self, data, comparison_op):
        ser = pd.Series(data)
        self._compare_other(ser, data, comparison_op, data[0])

    @pytest.mark.parametrize("na_action", [None, "ignore"])
    def test_map(self, data_missing, na_action, using_nan_is_na):