Una petita introducció
Marité Guerrieri
Un objecte mock substitueix i imita un objecte real en un entorno de testing.
La llibreria unittest de Python inclou un subpaquet anomenat unittest.mock.
Aquesta llibreria provee:
Per instal·lar la llibreria unittest.mock en versions de python anteriors a 3.3: pip install mock.
La documentació de la llibraria és aquí: https://docs.python.org/3/library/unittest.mock.html
addons/gisce/SomEnergia/som_polissa_administradora/tests/tests_administradora.py
def add_contract_administrator(self, cursor, uid, polissa_id, partner_id, context=None):
if context is None:
context = {}
if not self.validate_partner(cursor, uid, partner_id):
return False
….
@mock.patch.object(giscedata_polissa.GiscedataPolissa, “validate_partner”)
def test_add_contract_administrator__polissa_invalid_partner(self, mock_validate_partner):
pol_obj = self.model('giscedata.polissa')
res_partner_obj = self.model('res.partner')
partner_id = 1
polissa_id = 1
mock_validate_partner.return_value = False
result = pol_obj.add_contract_administrator(self.cursor, self.uid, polissa_id, partner_id, context={})
self.assertFalse(result)
def add_contract_administrator(self, cursor, uid, polissa_id, partner_id, context=None):
...
if not self.validate_partner(cursor, uid, partner_id):
return False
administradora = self.read(cursor, uid, polissa_id, [‘administradora’])[‘administradora’]
….
@mock.patch.object(giscedata_polissa.GiscedataPolissa, “read”)
@mock.patch.object(giscedata_polissa.GiscedataPolissa, “validate_partner”)
def test_add_contract_administrator__polissa_without_administradora(self, mock_validate_partner, mock_read_polissa):
pol_obj = self.model('giscedata.polissa')
res_partner_obj = self.model('res.partner')
partner_id = 1
polissa_id = 1
mock_read_polissa.return_value={‘administradora’: None}
mock_validate_partner.return_value = True
result = pol_obj.add_contract_administrator(self.cursor, self.uid, polissa_id, partner_id, context={})
self.assertTrue(result)
….
def add_contract_administrator(self, cursor, uid, polissa_id, partner_id, context=None):
...
if not self.validate_partner(cursor, uid, partner_id):
return False
administradora = self.read(cursor, uid, polissa_id, [‘administradora’])[‘administradora’]
….
self.add_administrator_category(cursor, uid, administradora[0])
….
@mock.patch.object(giscedata_polissa.GiscedataPolissa, “read”)
@mock.patch.object(giscedata_polissa.GiscedataPolissa, “validate_partner”)
def test_add_contract_administrator__polissa_without_administradora(self, mock_validate_partner, mock_read_polissa):
pol_obj = self.model('giscedata.polissa')
res_partner_obj = self.model('res.partner')
partner_id = 1
polissa_id = 1
mock_read_polissa.return_value={‘administradora’: None}
mock_validate_partner.return_value = True
result = pol_obj.add_contract_administrator(self.cursor, self.uid, polissa_id, partner_id, context={})
self.assertTrue(result)
partner = res_partner_obj.browse(self.cursor, self.uid, partner_id)
admin_cat = pol_obj.get_admin_cat(self.cursor, self.uid)
self.assertEqual(admin_cat.id, partner.category_id[0].id)
@patch.object(my_module.MyClass, 'my_method')
def test_my_method_shouldReturnMultipleValues_whenMyMethodReturnsSomeValue(self, mock_my_method):
list_of_return_values= [True,False,False]
def side_effect():
return list_of_return_values.pop()
mock_my_method.side_effect = side_effect
some_other_class = SomeOtherClassThatUsesMyClass()
self.assertFalse(some_other_class.method_under_test())
self.assertFalse(some_other_class.method_under_test())
self.assertTrue(some_other_class.method_under_test())
def add_contract_administrator(self, cursor, uid, polissa_id, partner_id, context=None):
...
if not self.validate_partner(cursor, uid, partner_id):
return False
administradora = self.read(cursor, uid, polissa_id, [‘administradora’])[‘administradora’]
if administradora and administradora[0] != partner_id:
self.remove_administrator_category(cursor, uid, administradora[0])
self.add_administrator_category(cursor, uid, administradora[0])
self.write(cursor, uid, polissa_id, {'administradora': partner_id})
return True
def remove_administrator_category(self, cursor, uid, partner_id):
administrated_contracts = self.search(cursor, uid, [("administradora", "=", partner_id)])
if len(administrated_contracts) == 1:
admin_cat = self.get_admin_cat(cursor, uid)
res_partner_obj = self.pool.get('res.partner')
res_partner_obj.write(cursor, uid, partner_id, {'category_id': [(3, admin_cat.id)]})
@mock.patch.object(giscedata_polissa.GiscedataPolissa, "read")
@mock.patch.object(giscedata_polissa.GiscedataPolissa, "remove_administrator_category")
@mock.patch.object(giscedata_polissa.GiscedataPolissa, "validate_partner")
def test_add_contract_administrator__polissa_with_administradora(self, mock_validate_partner,
mock_remove_administrator_category,
mock_read_polissa):
pol_obj = self.model('giscedata.polissa')
res_partner_obj = self.model('res.partner')
partner_id = 1
polissa_id = 1
mock_read_polissa.return_value = { 'administradora': [2] }
mock_validate_partner.return_value = True
def remove_administrator_category_mock(cursor, uid, partner_id):
pass
mock_remove_administrator_category.side_effect = remove_administrator_category_mock
result = pol_obj.add_contract_administrator(self.cursor, self.uid, polissa_id, partner_id, context={})
self.assertTrue(result)
pol_obj.remove_administrator_category.assert_called_once_with(self.cursor, self.uid, 2)
https://github.com/gisce/erp/pull/10927/files#diff-cc477805ade52b342f09b4f67330659a
@patch('my_module.MyClass')
def test_my_method_shouldReturnTrue_whenSomeOtherClassMethodIsCalledAndAReturnValueIsSet(self, mock_my_class):
mc = mock_my_class.return_value
mc.my_method.return_value = True
some_other_class = SomeOtherClassThatUsesMyClass()
result = some_other_class.method_under_test()
self.assertTrue(result)
@mock.patch.object(giscedata_polissa.GiscedataPolissa, “read”)
@mock.patch.object(giscedata_polissa.GiscedataPolissa, “validate_partner”)
def test_add_contract_administrator__polissa_without_administradora(self, mock_validate_partner, mock_read_polissa):
pol_obj = self.model('giscedata.polissa')
res_partner_obj = self.model('res.partner')
partner_id = 1
polissa_id = 1
mock_read_polissa.return_value={‘administradora’: None}
mock_validate_partner.return_value = True
result = pol_obj.add_contract_administrator(self.cursor, self.uid, polissa_id, partner_id, context={})
self.assertTrue(result)
….
@mock.patch(“som_polissa_administradora.giscedata_polissa.GiscedataPolissa”)
def test_add_contract_administrator__polissa_without_administradora(self, mock_polissa):
pol_obj = self.model('giscedata.polissa')
res_partner_obj = self.model('res.partner')
partner_id = 1
polissa_id = 1
mock_polissa.read.return_value={‘administradora’: None}
mock_polissa.validate_partner.return_value = True
result = pol_obj.add_contract_administrator(self.cursor, self.uid, polissa_id, partner_id, context={})
self.assertTrue(result)
….