# Mockgun Issues

**URL:** <https://community.shotgridsoftware.com/t/mockgun-issues/12311>\
**Category:** Flow Production Tracking\
**Created:** [May 4, 2021, 4:16pm UTC](https://community.shotgridsoftware.com/t/mockgun-issues/12311 "2021-05-04T16:16:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidma](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@davidma](https://community.shotgridsoftware.com/u/davidma)\
**Post date:** [May 4, 2021, 4:16pm UTC](https://community.shotgridsoftware.com/t/mockgun-issues/12311/1 "2021-05-04T16:16:14Z")

</div>

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:

```python
import pytest

@pytest.fixture
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']}},
                               return_fields=['tasks'])
    assert sg_asset['tasks'] == []
    sg_task = isg_conn.create('Task',
                              data={'project': {'type': 'Project', 'id': sg_project['id']}, 'entity': sg_asset},
                              return_fields=['entity'])
    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?

---

<div class="post-metadata">

**Author:** ![davidma](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@davidma](https://community.shotgridsoftware.com/u/davidma)\
**Post date:** [May 4, 2021, 5:07pm UTC](https://community.shotgridsoftware.com/t/mockgun-issues/12311/2 "2021-05-04T17:07:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rhea\_Fischer](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/rhea_fischer/32/2630_2.png) [@Rhea\_Fischer](https://community.shotgridsoftware.com/u/Rhea_Fischer)\
**Post date:** [May 5, 2021, 1:10am UTC](https://community.shotgridsoftware.com/t/mockgun-issues/12311/3 "2021-05-05T01:10:51Z")

</div>

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.
