def force_db_error(self): def execute_that_fails(*args, **kwargs): raise sqlite3.DatabaseError("Test Error") mock_execute = mock.Mock(side_effect=execute_that_fails) return mock.patch('miro.data.connectionpool.Connection.execute', mock_execute)
def patch_for_test(self, object_name): """Use Mock to replace a function/class/object with a mock object. We will unpatch the object during teardown :param object_name: name of the object to patch :returns: Mock object used to patch """ patcher = mock.patch(object_name, autospec=True) mock_object = patcher.start() self.mock_patchers.append(patcher) return mock_object
def patch_for_test(self, object_name, autospec=True): """Use Mock to replace a function/class/object with a mock object. We will unpatch the object during teardown :param object_name: name of the object to patch :param autospec: autospec parameter to pass to mock.patch. Generally this should be True, except for class methods. :returns: Mock object used to patch """ patcher = mock.patch(object_name, autospec=autospec) mock_object = patcher.start() self.mock_patchers.append(patcher) return mock_object
def patch_function(self, function_name, new_function): """Use Mock to replace an existing function for a single test. function_name should be in the form "full.module.name.object". For example "miro.startup.startup" This can also be used on a class object in order to return a different object, if we only use class objects as factory functions. :param function_name: name of the function to patch :param new_function: function object to replace it with """ patcher = mock.patch(function_name, mock.Mock(side_effect=new_function)) patcher.start() self.mock_patchers.append(patcher)
def patch_for_test(self, object_name, mock_object=None): """Use Mock to replace a function/class/object with a mock object. We will unpatch the object during teardown :param object_name: name of the object to patch :param mock_object: object to patch with, if None we will create a new Mock :returns: object used to patch """ if mock_object is None: mock_object = mock.Mock() patcher = mock.patch(object_name, mock_object) patcher.start() self.mock_patchers.append(patcher) return mock_object
def test_upgrade_error(self): self.open_database() self.make_device_items('foo.mp3', 'bar.mp3') devices.write_database(self.device.database, self.device.mount) # Force our upgrade code to run and throw an exception. os.remove(os.path.join(self.device.mount, '.miro', 'sqlite')) mock_do_import = mock.Mock() method = ('miro.devicedatabaseupgrade.' '_do_import_old_items.convert_old_item') patcher = mock.patch(method, mock_do_import) mock_do_import.side_effect = sqlite3.DatabaseError("Error") self.device.database.created_new = False with patcher: self.open_database() # check that we created a new sqlite database and added new metadata # status rows for the device items cursor = self.device.sqlite_database.cursor cursor.execute("SELECT path FROM metadata_status") paths = [r[0] for r in cursor.fetchall()] self.assertSameSet(paths, ['foo.mp3', 'bar.mp3'])