def test_fit_clusters__base_inspector(mock_clusterer, global_small_df): # Base Inspector case algotype is kmeans inspector = BaseInspector(algotype='kmeans') inspector.clusterer = mock_clusterer X = global_small_df inspector.fit_clusters(X) # Check if has been called with correct argument mock_clusterer.fit.assert_called_once() pd.testing.assert_frame_equal(mock_clusterer.fit.call_args[0][0], X) # Check if it has not been modified pd.testing.assert_frame_equal(X, global_small_df) # Check if fitted flag has been changed correctly assert inspector.fitted is True
def test_predict_clusters__base_inspector(mock_clusterer, global_small_df): mock_clusterer.predict.return_value = [1, 0] inspector = BaseInspector(algotype='kmeans') inspector.clusterer = mock_clusterer inspector.fitted = True X = global_small_df # Check if the prediction is correct according to the Mock clusterer assert inspector.predict_clusters(X) == [1, 0] # Check if the clusterer was called with correct input mock_clusterer.predict.assert_called_once() pd.testing.assert_frame_equal(mock_clusterer.predict.call_args[0][0], X) # Check if the X has not been modified pd.testing.assert_frame_equal(X, global_small_df)
def test_assert_is_dataframe(global_small_df): X_df = global_small_df X_list = X_df.values.tolist() X_array = np.asarray(X_list) X_array_flat = np.asarray(X_list[0]) pd.testing.assert_frame_equal(X_df, BaseInspector.assert_is_dataframe(X_df)) pd.testing.assert_frame_equal( X_df, BaseInspector.assert_is_dataframe(X_array)) with pytest.raises(NotImplementedError): BaseInspector.assert_is_dataframe(X_list) with pytest.raises(NotImplementedError): BaseInspector.assert_is_dataframe(X_array_flat)
def test_assert_is_series(global_small_df, global_small_df_flat): X_df = global_small_df X_df_flat = global_small_df_flat X_list = X_df.values.tolist() X_list_flat = X_df_flat.values.tolist() X_series = pd.Series(X_list_flat) X_array = np.asarray(X_list) X_array_flat = np.asarray(X_list_flat) index = [0, 1, 2, 3] pd.testing.assert_series_equal( X_series, BaseInspector.assert_is_series(X_series)) pd.testing.assert_series_equal( X_series, BaseInspector.assert_is_series(X_df_flat)) pd.testing.assert_series_equal( X_series, BaseInspector.assert_is_series(X_array_flat, index=index)) with pytest.raises(TypeError): BaseInspector.assert_is_series(X_list) with pytest.raises(TypeError): BaseInspector.assert_is_series(X_list_flat) with pytest.raises(TypeError): BaseInspector.assert_is_series(X_df) with pytest.raises(TypeError): BaseInspector.assert_is_series(X_array) with pytest.raises(TypeError): BaseInspector.assert_is_series(X_array, index=[0, 1]) with pytest.raises(TypeError): BaseInspector.assert_is_series(X_array_flat)