I have a function which throws an exception when the max of column A is equal to a number (say 5). I want to unittest this function to check if it throws the Exception.
main.py
import pandas as pd
class DuplicateRunError(Exception):
def __init__(self, value):
self.value = value
def pd_max(df1):
max_a = df1['A'].max()
if max_a == 5:
raise DuplicateRunError("Max Value for A reached")
else:
return "All Good"
if __name__ == '__main__':
print(pd_max(pd.read_csv("file1.csv")))
I created a unittest for this function like below.
main_test.py
import unittest
from unittest import mock
import pandas as pd
class TestRaiseException(unittest.TestCase):
@mock.patch('df1["A"].max()')
def test_pd_max(self, mock_max_a):
mock_max_a.return_value = 5
with self.assertRaises(DuplicateRunError):
pd_max(pd.read_csv("file1.csv"))
if __name__ == '__main__':
unittest.main()
But I get an error ModuleNotFoundError: No module named 'df1["A"]'
I want to mock the value of df1["A"].max()
What is missing here? What is the best way to set the value for df1["A"].max() I think I could get it working if i mock the dataframe object by passing a dict and then passing it to the function. But I want to know if there is way to directly set the value as 5 for df1["A"].max()