def test_data_type_exception(self, np_2x1_with_label,
                                 xgb_regressor_1_split_1_tree):
        """Test an exception is raised if data is not a np.ndarray or pd.DataFrame object."""

        confo_model = XGBSklearnLeafNodeScaledConformalPredictor(
            xgb_regressor_1_split_1_tree)

        with pytest.raises(
                TypeError,
                match=re.escape(
                    f"data is not in expected types {[np.ndarray, pd.DataFrame]}, got {list}"
                ),
        ):

            confo_model._generate_leaf_node_predictions([])
    def test_output_2d(self, mocker, np_2x1_with_label,
                       xgb_regressor_1_split_1_tree):
        """Test the array returned from _generate_leaf_node_predictions is a 2d array
        even if the output from predict is 1d.
        """

        confo_model = XGBSklearnLeafNodeScaledConformalPredictor(
            xgb_regressor_1_split_1_tree)

        confo_model.calibrate(np_2x1_with_label[0], np_2x1_with_label[1])

        # set the return value from predict to be a 1d array
        predict_return_value = np.array([200, 101])

        mocker.patch.object(xgb.XGBRegressor,
                            "apply",
                            return_value=predict_return_value)

        results = confo_model._generate_leaf_node_predictions(
            np_2x1_with_label[0])

        expected_results = predict_return_value.reshape(
            predict_return_value.shape[0], 1)

        np.testing.assert_array_equal(results, expected_results)
    def test_predict_call(self, mocker, np_2x1_with_label,
                          xgb_regressor_1_split_1_tree):
        """Test that the output from xgb.XGBRegressor.predict with ntree_limit = best_iteration + 1
        and pred_leaf = True is returned from the method.
        """

        confo_model = XGBSklearnLeafNodeScaledConformalPredictor(
            xgb_regressor_1_split_1_tree)

        confo_model.calibrate(np_2x1_with_label[0], np_2x1_with_label[1])

        predict_return_value = np.array([[200, 101], [5, 6]])

        mocked = mocker.patch.object(xgb.XGBRegressor,
                                     "apply",
                                     return_value=predict_return_value)

        results = confo_model._generate_leaf_node_predictions(
            np_2x1_with_label[0])

        assert (mocked.call_count == 1
                ), "incorrect number of calls to xgb.XGBRegressor.apply"

        np.testing.assert_array_equal(results, predict_return_value)

        call_args = mocked.call_args_list[0]
        call_pos_args = call_args[0]
        call_kwargs = call_args[1]

        assert (
            len(call_pos_args) == 0
        ), "incorrect number of positional args in xgb.XGBRegressor.apply call"

        assert (
            len(list(call_kwargs.keys())) == 2
        ), "incorrect number of keyword args in xgb.XGBRegressor.apply call"

        assert ("ntree_limit" in call_kwargs.keys()
                and "X" in call_kwargs.keys()
                ), "incorrect keyword args in xgb.XGBRegressor.apply call"

        np.testing.assert_array_equal(call_kwargs["X"], np_2x1_with_label[0])

        assert (
            call_kwargs["ntree_limit"] ==
            xgb_regressor_1_split_1_tree.best_iteration + 1
        ), "ntree_limit keyword arg incorrect in xgb.XGBRegressor.apply call"