I have a function which reads data from db, modifies it and writes it back to another db. I am trying to write unit tests for this
import file1
import file2
def update_func(query):
max_date = file1.get_cutover_date()
data_dataframe = file2.get_dataframe_from_db(query)
transformed_dataframe = modify_df(data_dataframe)
df1 = transformed_dataframe.filter(transformed_dataframe['date']>max_date)
write_to_db(df1)
I tried to patch the function calls.
@patch('file2.get_dataframe_from_db')
@patch('file1.get_cutover_date')
test_update_func(patched_get_cutover, patched_get_df):
patched_get_cutover.return_value = '2015-01-01 00:00:00'
response = [Row(id=1,date='2016-10-12 00:00:00')]
response_df = spark.createDataFrame(response)
patched_get_df.return_value = response_df
update_func('test_query')
patched_get_cutover.assert_called
patched_get_df.assert_called
But I am getting TypeError: '>' not supported between instances of MagicMock and MagicMock
lang-py
file1andfile2whileupdate_func()is in an other file. Try to change@patch('file2.get_dataframe_from_db')to@patch('<file-of-update_func>.get_dataframe_from_db'). But I'm not sure.from file1 import get_cutover_dateandfrom file2 import get_dataframe_from_db. After that change the patches to:@patch('<file-of-update_func>.get_dataframe_from_db')and@patch('<file-of-update_func>.get_cutover_date'). I think that with these changes you can solve your problem.transformed_dataframe['date']>max_dateor elsewhere? 2. If you're using the values returned by a patched function, then you need to ensure the value it returns is "usable". E.g. in this case you're using the output offile2.get_dataframe_from_db. So patch it to return a proper dataframe if you really want to use it.