def test_get_user_skip_dao_constructor(self, mock_user_dao_class): mock_class = mock_user_dao_class.return_value # no need to create a Person, just use Mock's sentinel sentinel.person.last_name = 'Lincoln' mock_class.query_user.return_value = sentinel.person bus_obj = BusinessObject('mock_demo') user_id = 123 actual_result = bus_obj.get_user(user_id) mock_class.query_user.assert_called_with(user_id) self.assertEquals(sentinel.person, actual_result)
def test_get_user(self, mock_query_user_method): expected_result = Person('Isaac', None, 'Newton') # set the return value for the mock method mock_query_user_method.return_value = expected_result bus_obj = BusinessObject('mock_demo') user_id = 123 actual_result = bus_obj.get_user(user_id) mock_query_user_method.assert_called_with(user_id) self.assertEquals(expected_result, actual_result)
def test_get_user_dao_error(self): mock_dao = Mock(spec=UserDao) # Configure the mock query_user method to raise a DB error mock_dao.query_user = Mock(side_effect=sqlite3.Error('SQL error')) bus_obj = BusinessObject('mock_demo') bus_obj.user_dao = mock_dao user_id = 123 with self.assertRaisesRegex(BusinessError, 'SQL error'): bus_obj.get_user(user_id) mock_dao.query_user.assert_called_with(user_id)
def test_get_user_not_found(self): mock_dao = Mock(spec=UserDao) mock_dao.query_user = Mock() mock_dao.query_user.return_value = None bus_obj = BusinessObject('mock_demo') bus_obj.user_dao = mock_dao user_id = 123 with self.assertRaisesRegex(ValueError, r'[Vv]alid.*[Ii][Dd]'): bus_obj.get_user(user_id) mock_dao.query_user.assert_called_with(user_id)
def test_get_user_skip_dao_constructor(self, mock_user_dao_class): mock_dao = mock_user_dao_class.return_value expected_result = Person('Isaac', None, 'Newton') mock_dao.query_user.return_value = expected_result # BusinessObject constructor's call to UserDao() now creates a mock bus_obj = BusinessObject('mock_demo') # no need to set BusinessObject's user_dao user_id = 123 actual_result = bus_obj.get_user(user_id) mock_dao.query_user.assert_called_with(user_id) self.assertEquals(expected_result, actual_result)
def test_get_user(self, mock_query_user_method): # no need to create a Person, just use Mock's sentinel # as a generic object # BusinessObject.get_user references Person.last_name, so add a # person attribute, which has a last_name attribute, to the sentinel sentinel.person.last_name = 'Jackson' # Tell the mock query_user method to return the sentinel mock_query_user_method.return_value = sentinel.person bus_obj = BusinessObject('mock_demo') user_id = 123 actual_result = bus_obj.get_user(user_id) mock_query_user_method.assert_called_with(user_id) self.assertEquals(sentinel.person, actual_result)
def test_get_user(self): expected_result = Person('Isaac', None, 'Newton') # create a mock for the DAO itself mock_dao = Mock(spec=UserDao) # create a mock for the DAO's query_user method and # set the return value of the mock method mock_dao.query_user.return_value = expected_result bus_obj = BusinessObject('mock_demo') # replace the real DAO with the mock DAO bus_obj.user_dao = mock_dao user_id = 123 # when the business method is called, it will use the mock DAO # instead of the real DAO actual_result = bus_obj.get_user(user_id) # verify that the mock method was called correctly mock_dao.query_user.assert_called_with(user_id) # verify that the actual result equals the expected result self.assertEquals(expected_result, actual_result)
""" monkey_patch_business_object.py - Example of monkey patching, from Chapter 3 """ import types from business_object import BusinessObject, UserDao from person import Person # Here's our monkey patch def patch_get_user(obj, user_id): print("patch_get_user({}, {})".format(obj, user_id)) return Person('Curious', '', 'George') bus_obj = BusinessObject('Business As Usual') person = bus_obj.get_user(123) print('\nBefore monkey patch, person = {}'.format(person)) # Save a reference to the old method old_get_user = BusinessObject.get_user # Replace the class's get_user method with our monkey patch. # Now all instances of BusinessObject have the new method. BusinessObject.get_user = patch_get_user person = bus_obj.get_user(123) print('After monkey patch, person = {}'.format(person)) # Restore the old method definition BusinessObject.get_user = old_get_user
monkey_patch_business_object.py - Example of monkey patching, from Chapter 3 """ import types from business_object import BusinessObject, UserDao from person import Person # Here's our monkey patch def patch_get_user(obj, user_id): print("patch_get_user({}, {})".format(obj, user_id)) return Person('Curious', '', 'George') bus_obj = BusinessObject('Business As Usual') person = bus_obj.get_user(123) print('\nBefore monkey patch, person = {}'.format(person)) # Save a reference to the old method old_get_user = BusinessObject.get_user # Replace the class's get_user method with our monkey patch. # Now all instances of BusinessObject have the new method. BusinessObject.get_user = patch_get_user person = bus_obj.get_user(123) print('After monkey patch, person = {}'.format(person)) # Restore the old method definition BusinessObject.get_user = old_get_user