This is my method below that returns a dataframe
def refactorReport( df):
"""Enhance with additional information as needed"""
for userclass in [ 'ClassA', 'ClassB', 'ClassC' ]:
df[ '%s_Region'%userclass ] = df[ userclass ].apply(lambda x: commutils.UserNameMapper( x ).Region() )
return df
How do I mock this commutils.UserNameMapper(x)
?
I tried this but both are giving me “self param expected”:
class DummyUserNameMapper():
def __init__( self, thename ):
self.thename = thename
def Region( self ):
print(self.thename)
return 'Region%s'%self.thename[-1]
class ModuleTests(unittest.TestCase):
def test_refactorReport(self):
with mock.patch("commutils.UserNameMapper", return_value=DummyUserNameMapper):
print(refactorReport(self.records))
@mock.patch('commutils.UserNameMapper')
def test_refactorReport_New(self, mockUser):
mockUser.return_value = DummyUserNameMapper
print(refactorReport(self.records))
Where self.records
>>> import pandas as pd
>>> data = [()]
>>> data = [
... ('Name 1', 'Apple', 'Mango', 'Orange'),
... ('Name 2', 'Pear', 'Apple', 'Banana'),
... ('Name 3', 'Banana', 'Mango', 'Orange'),
... ('Name 4', 'Apple', 'Pear', 'Orange'),
... ('Name 5', 'Pear', 'Mango', 'Orange'),]
...
>>> df = pd.DataFrame(data,columns=['Name', 'ClassA', 'ClassB', 'ClassC'])
0
Not sure what was wrong earlier, but this worked
def test_refactorReport(self):
with mock.patch(“commutils.UserNameMapper”, return_value=DummyUserNameMapper):
print(refactorReport(self.records))