Mockgun Issues

I’ve begun the process of creating a PyTest unit testing suite for an in-house Python library which interacts with Shotgun via the shotgun_api3 (currently using version: 3.2.4)

In order to keep the test data isolated from our live database I discovered the shotgun_api3.lib.mockgun.Shotgun object and have been using this.

I have encountered a few alarming deficiencies in mockgun.Shotgun.

  1. mockgun.Shotgun().update() is completely missing the argument for multi_entity_update_modes - rendering multi-entity add / remove updates ineffective
  2. On mockgun.Shotgun().create() when setting entity / multi-entity field values to the short-form value dict of another entity eg: Task - {"entity": [{"type": "Asset", "id": 1}]}

For an example on #2 - consider the following:

import pytest

def isg_conn():
    import shotgun_api3
    sg_conn = shotgun_api3.Shotgun(SG_URL, *API_CRED)
    tmp_dir = 'D:/Temp/testing/shotgun'
    schema_file = tmp_dir + '/shotgun_schema'
    schema_entity_file = tmp_dir + '/shotgun_schema_entity'

    from shotgun_api3.lib import mockgun
    mockgun.generate_schema(sg_conn, schema_file, schema_entity_file)
    mockgun.Shotgun.set_schema_paths(schema_file, schema_entity_file)
    yield mockgun.Shotgun(SG_URL, *API_CRED)

def test_create_entity_links(isg_conn):
    sg_project = isg_conn.create('Project', data={})
    sg_asset = isg_conn.create('Asset',
                               data={'project': {'type': 'Project', 'id': sg_project['id']}},
    assert sg_asset['tasks'] == []
    sg_task = isg_conn.create('Task',
                              data={'project': {'type': 'Project', 'id': sg_project['id']}, 'entity': sg_asset},
    expected_sg_task_asset = {'type': 'Asset', 'id': sg_asset['id']}
    assert sg_task['entity'] == expected_sg_task_asset
    updated_sg_asset = isg_conn.find_one('Asset', [['id', 'is', sg_asset['id']]], ['tasks'])
    expected_sg_asset_tasks = [{'type': 'Task', 'id': sg_task['id']}]
    assert updated_sg_asset['tasks'] == expected_sg_asset_tasks  # Failed Test: AssertionError: assert [] == [{'id': 1, 'type': 'Task'}]

There seems to be no account of some necessary logic when it comes to working with entity / multi-entity fields and the related EntityType_field_Connection objects.

Is this something others have encountered?
Is there any plan to further develop shotgun_api3.lib.mockgun to be fully-featured?

1 Like

Is there a better alternative for isolated testing aside from using the mockgun module?

I’m just another user of SgTk like yourself. I find mockgun very useful for basic unit tests as well. It is indeed incomplete. I made a branch and patched in some simple things I needed. I also built a complimentary “mock Perforce” to test my framework for that.

Since it’s not a documented part of tk-core I think the best we can do is encourage others to use it and make our own patches then PR those back to the Shotgun people.