Mocking Save Scene
Since create_maya_scene
deals with creating new work files, these files need to be saved to the file system at some point.
When create_maya_scene
is executed in a test, this is an issue. Saving the file would cause the file system to be modified by creating some folders and at least one file.
We don’t want to test if Maya can save files correctly: It’s enough to verify that the save function is called.
This is the section dealing with saving the file:
[...]
t1 = time.time()
tmp_path = save_scene_as_temp()
log.debug('moving tmp file from "{}" to "{}"'.format(tmp_path, scene_path))
shutil.move(tmp_path, scene_path)
pm.renameFile(scene_path)
pm.workspace(os.path.dirname(os.path.dirname(scene_path)), openWorkspace=True)
t2 = time.time()
log.debug('Done saving, took {} seconds'.format(int(t2 - t1)))
pm.select(cl=True)
return db_file
[...]
This section is extracted into a method:
[...]
save_scene_using_tmp(scene_path)
return db_file
def save_scene_using_tmp(scene_path):
t1 = time.time()
tmp_path = save_scene_as_temp()
log.debug('moving tmp file from "{}" to "{}"'.format(tmp_path, scene_path))
shutil.move(tmp_path, scene_path)
pm.renameFile(scene_path)
pm.workspace(os.path.dirname(os.path.dirname(scene_path)), openWorkspace=True)
t2 = time.time()
log.debug('Done saving, took {} seconds'.format(int(t2 - t1)))
pm.select(cl=True)
save_scene_using_tmp
can now be mocked in the test function:
@mock.patch("...]maya.api.filemanager.save_scene_using_tmp")
@mock.patch("...]maya.api.filemanager.set_current_file_info")
@mock.patch("...]maya.api.filemanager.query_previous_files")
def test_create_maya_scene(mock_query_previous_files, mock_set_current_file_info, mock_save_scene):
shot = Shot('myAwesomeShot')
user = User('Jan Honsbrok', 'janhon')
current = File(shot=shot)
mock_query_previous_files.return_value = [File(shot=shot, name='test', filetype='filetype', version=0)]
mock_session = mock.MagicMock()
create_maya_scene(shot, user=user, current=current, session=mock_session)
mock_session.commit.assert_called_once()
mock_set_current_file_info.assert_called_once()
mock_save_scene.assert_called_once()
Last modified August 19, 2020