class TestFilterDF(unittest.TestCase):
@patch('plugins.qa_plugins.preprocessing.read_df')
def test_filter_df(self, read_df_mock):
# Mocking read_df function to return a DataFrame directly
read_df_mock.return_value = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8],
'C': ['x', 'y', 'z', 'w']
})
# Mocking save_output_to_file decorator
save_output_to_file_mock = Mock()
expected_df = pd.DataFrame({
'A': [3],
'B': [7],
'C': ['z']
})
save_output_to_file_mock.return_value = expected_df
result_df, kwargs = pr.filter_df("dummy_path.csv", [
{'column': 'A', 'condition': '> 2'},
{'column': 'C', 'condition': "== 'z'"}
])
# Asserting the output
read_df_mock.assert_called_with("dummy_path.csv", INPUT_FILE_EXT)
pd.testing.assert_frame_equal(result_df, expected_df)
# Asserting the kwargs
self.assertEqual(kwargs, {})
# Asserting the calls to functions
read_df_mock.assert_called_once_with("dummy_path.csv", INPUT_FILE_EXT)
save_output_to_file_mock.assert_not_called()
if name == ‘main‘:
unittest.main()
I want to mock the read_df function which takes filename as one of the arguments. I have written this code but it is showing error No Module found named dummy_path.csv. save_output_to_file is a decorator which helps read_df
New contributor
Uplabdhi Khare is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.