def test_pandas_method_called(self, mocker, df, new_column_name, pd_method_name, columns, pd_method_kwargs): """Test that the pandas.Series.str method is called as expected (with kwargs passed) during transform.""" spy = mocker.spy(pd.Series.str, pd_method_name) x = SeriesStrMethodTransformer( new_column_name=new_column_name, pd_method_name=pd_method_name, columns=columns, pd_method_kwargs=pd_method_kwargs, ) x.transform(df) # pull out positional and keyword args to target the call call_args = spy.call_args_list[0] call_kwargs = call_args[1] # test keyword are as expected h.assert_dict_equal_msg( actual=call_kwargs, expected=pd_method_kwargs, msg_tag=f"""Keyword arg assert for {pd_method_name}""", )
def test_inpsect_is_class_call(): """Test the call to inspect.isclass.""" with mock.patch("inspect.isclass") as mocked_method: h.check_is_class(int) assert ( mocked_method.call_count == 1 ), f"Unexpected number of call to inspect.isclass -\n Expected: 1\n Actual: {mocked_method.call_count}" call_1_args = mocked_method.call_args_list[0] call_1_pos_args = call_1_args[0] call_1_kwargs = call_1_args[1] h.assert_dict_equal_msg( actual=call_1_kwargs, expected={}, msg_tag="Keyword arg assert for inspect.isclass", ) assert ( len(call_1_pos_args) == 1 ), f"Incorrect number of positional arguments in inspect.isclass call -\n Expected: 2\n Actual: {len(call_1_pos_args)}" assert ( call_1_pos_args[0] is int ), f"Incorrect first positional arg in inspect.isclass call -\n Expected: {int}\n Actual: {call_1_pos_args[0]}"
def test_actual_not_dict_error(): """'Test that a TypeError is raised if actual is not a tuple or list.""" with pytest.raises( TypeError, match=f"actual should be of type dict, but got {type(1)}" ): h.assert_dict_equal_msg(expected={}, actual=1, msg_tag="test_msg")
def test_keys_not_same_error(): """Test that an exception is raised if there are keys in expected not in actual.""" expected_value = {"a": 1, "b": 2, "c": 3} actual_value = {"c": 4, "d": 5, "e": 6} with pytest.raises( AssertionError, match=f"Keys in expected not in actual: {set(expected_value.keys()) - set(actual_value.keys())}\nKeys in actual not in expected: {set(actual_value.keys()) - set(expected_value.keys())}", ): h.assert_dict_equal_msg( expected=expected_value, actual=actual_value, msg_tag="test_msg" )
def test_different_n_key_error(): """Test that a ValueError is raised if actual and expected have numbers of keys.""" expected_value = {"a": 1, "b": 2} actual_value = {} with pytest.raises( AssertionError, match=f"Unequal number of keys -\n Expected: {len(expected_value.keys())}\n Actual: {len(actual_value.keys())}", ): h.assert_dict_equal_msg( expected=expected_value, actual=actual_value, msg_tag="test_msg" )
def test_assert_equal_dispatch_calls(): """Test the calls to tubular.testing.helpers.assert_equal_dispatch.""" expected_value = {"a": 1, "b": 2, "c": 3} actual_value = {"a": 1, "b": 2, "c": 3} msg_tag_value = "test_msg" with mock.patch( target="tubular.testing.helpers.assert_equal_dispatch" ) as mocked_method: h.assert_dict_equal_msg( expected=expected_value, actual=actual_value, msg_tag=msg_tag_value ) assert mocked_method.call_count == len( expected_value ), f"Unexpeted number of calls to tubular.testing.helpers.assert_equal_dispatch -\n Expected: {len(expected_value)}\n Actual: {mocked_method.call_count}" for i, k in enumerate(actual_value.keys()): call_n_args = mocked_method.call_args_list[i] call_n_pos_args = call_n_args[0] call_n_kwargs = call_n_args[1] expected_pos_args = ( expected_value[k], actual_value[k], f"{msg_tag_value} key {k}", ) assert ( call_n_kwargs == {} ), f"Unexpected call keyword args in call {i} to tubular.testing.helpers.assert_equal_dispatch -\n Expected: None\n Actual: {call_n_kwargs}" assert len(call_n_pos_args) == len( expected_pos_args ), f"Difference in number of positional arguments in call {i} to tubular.testing.helpers.assert_equal_dispatch -\n Expected: {len(expected_pos_args)}\n Actual: {len(call_n_pos_args)}" for i, (e, a) in enumerate(zip(call_n_pos_args, expected_pos_args)): assert ( e == a ), f"Difference in positional args call {i} to tubular.testing.helpers.assert_equal_dispatch (for key {k}) -\n Expected: {e}\n Actual: {a}"
def test_check_is_fitted_call(self): """Test the call to tubular.base.check_is_fitted (sklearn.utils.validation.check_is_fitted).""" x = BaseTransformer(columns=None) with mock.patch("tubular.base.check_is_fitted") as mocked_method: attributes = "columns" x.check_is_fitted(attributes) assert ( mocked_method.call_count == 1 ), f"Incorrect number of calls to tubular.base.check_is_fitted -\n Expected: 1\n Actual: {mocked_method.call_count}" call_1_args = mocked_method.call_args_list[0] call_1_pos_args = call_1_args[0] call_1_kwargs = call_1_args[1] h.assert_dict_equal_msg( actual=call_1_kwargs, expected={}, msg_tag="Keyword arg assert for tubular.base.check_is_fitted", ) assert ( len(call_1_pos_args) == 2 ), f"Incorrect number of positional arguments in check_is_fitted call -\n Expected: 2\n Actual: {len(call_1_pos_args)}" assert ( call_1_pos_args[0] is x ), f"Incorrect first positional arg in check_is_fitted call -\n Expected: {x}\n Actual: {call_1_pos_args[0]}" assert ( call_1_pos_args[1] == attributes ), f"Incorrect second positional arg in check_is_fitted call -\n Expected: {attributes}\n Actual: {call_1_pos_args[1]}"