0

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

asked Jan 10, 2023 at 7:43
4
  • May be you are patching functions in file1 and file2 while update_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. Commented Jan 10, 2023 at 9:56
  • All my functions are in different files. Updated my question to include imports Commented Jan 10, 2023 at 12:36
  • Change the import to: from file1 import get_cutover_date and from 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. Commented Jan 10, 2023 at 13:15
  • 1. Post full stack trace, is the exception thrown from transformed_dataframe['date']>max_date or 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 of file2.get_dataframe_from_db. So patch it to return a proper dataframe if you really want to use it. Commented Jan 10, 2023 at 22:32

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.