import scipy.sparse as sp
import numpy as np

from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils.testing import assert_raises_regex
from sklearn.utils.estimator_checks import check_estimator
from sklearn.linear_model import LogisticRegression
from sklearn.utils.validation import check_X_y, check_array


class BaseBadClassifier(BaseEstimator, ClassifierMixin):
    def fit(self, X, y):
        return self

    def predict(self, X):
        return np.ones(X.shape[0])


class NoCheckinPredict(BaseBadClassifier):
    def fit(self, X, y):
        X, y = check_X_y(X, y)
        return self


class NoSparseClassifier(BaseBadClassifier):
    def fit(self, X, y):
        X, y = check_X_y(X, y, accept_sparse=['csr', 'csc'])
        if sp.issparse(X):
            raise ValueError("Nonsensical Error")
        return self

    def predict(self, X):
        X = check_array(X)
        return np.ones(X.shape[0])


def test_check_estimator():
    # check that we have a set_params and can clone
    msg = "it does not implement a 'get_params' methods"
    assert_raises_regex(TypeError, msg, check_estimator, object)
    msg = "object has no attribute 'fit'"
    assert_raises_regex(AttributeError, msg, check_estimator, BaseEstimator)
    msg = "argument must be a string or a number"
    assert_raises_regex(AssertionError, msg, check_estimator, BaseBadClassifier)
    msg = "Estimator doesn't check for NaN and inf in predict"
    assert_raises_regex(AssertionError, msg, check_estimator, NoCheckinPredict)
    msg = "Estimator type doesn't seem to fail gracefully on sparse data"
    assert_raises_regex(TypeError, msg, check_estimator, NoSparseClassifier)

    # doesn't error on actual estimator
    check_estimator(LogisticRegression)

@@ -37,16 +37,89 @@
 py as np
+%0Aimport sys%0Afrom sklearn.externals.six.moves import cStringIO as StringIO
 %0A%0Afrom s
@@ -221,16 +221,29 @@
 es_regex
+, assert_true
 %0Afrom sk
@@ -1042,16 +1042,151 @@
 ator():%0A
+    # tests that the estimator actually fails on %22bad%22 estimators.%0A    # not a complete test of all checks, which are very extensive.%0A%0A
     # ch
@@ -1826,43 +1826,318 @@
     
-assert_raises_regex(TypeError, msg,
+# the check for sparse input handling prints to the stdout,%0A    # instead of raising an error, so as not to remove the original traceback.%0A    # that means we need to jump through some hoops to catch it.%0A    old_stdout = sys.stdout%0A    string_buffer = StringIO()%0A    sys.stdout = string_buffer%0A    try:%0A       
  che
@@ -2148,18 +2148,17 @@
 stimator
-, 
+(
 NoSparse
@@ -2168,16 +2168,135 @@
 ssifier)
+%0A    except:%0A        pass%0A    finally:%0A        sys.stdout = old_stdout%0A    assert_true(msg in string_buffer.getvalue())
 %0A%0A    # 

check output, not error on sparse data handling