Mocking File Info

During the file creation process, some information is set as file info:

    [...]
    
    # write db_file settings into maya file
    set_current_file_info(db_file)
    
    [...]

In Maya, the file info lets you store related information as key-value pairs in the Maya scene file. In this case, some information about the project is stored in the file. We need to make sure that this information is still stored correctly in the file, even if we change something. The code responsible for setting this information is already a single function called set_current_file_info. set_current_file_info itself is pretty simple, so it’s not worth testing it. Since we know set_current_file_info is working correctly right now, we can just mock it and assert a correct call to it:

@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):
    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()

Mocking this call makes the test more independent from the Maya API, which is always a good idea.

Last modified August 24, 2020