Skip to content

test_inherited_attribs_sync

Test AYON inherited attributes are synced to SG.

EntityHub only holds own attribute values, inherited ones are None. SG has no inheritance, so the values resolved by AYON must be sent, whatever the sync: manual (update and creation) or event based.

Hierarchy used by all tests, new being set on the sequence:

seq           own value: new
├── sh010     inherits             -> new
│   └── task  inherits from sh010  -> new
└── sh020     own value: own
    └── task  inherits from sh020  -> own

Scenario

Build the same hierarchy in AYON and optionally in SG.

Source code in services/tests/update_from_ayon/test_inherited_attribs_sync.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
class Scenario:
    """Build the same hierarchy in AYON and optionally in SG."""

    def __init__(self, mg, sg_project, ay_attrib, sg_attrib, values):
        self.mg = mg
        self.sg_project = sg_project
        self.ay_attrib = ay_attrib
        self.custom_attribs_map = {ay_attrib: sg_attrib}
        self.values = values
        self.sg_field = self._get_sg_field(sg_attrib)

        mg.create("Step", {"code": "edit", "entity_type": "Shot"})
        self.entity_hub = InheritEntityHub("test_project", ay_attrib)

        # name: (AYON entity, own state or None, resolved state)
        self.ay_entities = {}
        seq = self._add_folder("seq", "Sequence", "test_project", "new", "new")
        sh010 = self._add_folder("sh010", "Shot", seq.id, None, "new")
        self._add_task("task010", sh010.id, None, "new")
        sh020 = self._add_folder("sh020", "Shot", seq.id, "own", "own")
        self._add_task("task020", sh020.id, None, "own")

        self.sg_entities = {}

    def _get_sg_field(self, sg_attrib):
        schema = self.mg.schema_field_read("Task")
        return sg_attrib if sg_attrib in schema else f"sg_{sg_attrib}"

    def _ay_value(self, state):
        return self.values[state][0] if state else None

    def _add_folder(self, name, folder_type, parent_id, own, resolved):
        folder = FolderEntity(
            name,
            folder_type,
            parent_id=parent_id,
            entity_hub=self.entity_hub,
            attribs={self.ay_attrib: self._ay_value(own)},
        )
        self.ay_entities[name] = (folder, own, resolved)
        return self.entity_hub._add(folder, parent_id)

    def _add_task(self, name, folder_id, own, resolved):
        task = TaskEntity(
            name,
            "edit",
            folder_id=folder_id,
            entity_hub=self.entity_hub,
            attribs={self.ay_attrib: self._ay_value(own)},
        )
        self.ay_entities[name] = (task, own, resolved)
        return self.entity_hub._add(task, folder_id)

    def has_sg_field(self, sg_type):
        return self.sg_field in self.mg.schema_field_read(sg_type)

    def create_sg_entities(self, initial_states):
        """Create SG entities linked to AYON ones, with initial values."""
        parents = {"sh010": "seq", "sh020": "seq",
                   "task010": "sh010", "task020": "sh020"}
        step = self.mg.find_one("Step", [["code", "is", "edit"]])
        for name in ("seq", "sh010", "sh020", "task010", "task020"):
            ay_entity = self.ay_entities[name][0]
            sg_type = (
                "Task" if ay_entity.entity_type == "task"
                else ay_entity.folder_type
            )
            data = {
                "project": self.sg_project,
                constants.CUST_FIELD_CODE_ID: ay_entity.id,
            }
            if sg_type == "Task":
                data.update({
                    "content": name,
                    "entity": self.sg_entities[parents[name]],
                    "step": step,
                })
            else:
                data["code"] = name
                if sg_type == "Shot":
                    data["sg_sequence"] = self.sg_entities[parents[name]]
            if self.has_sg_field(sg_type):
                data[self.sg_field] = self.values[initial_states[name]][1]

            sg_entity = self.mg.create(sg_type, data)
            self.sg_entities[name] = sg_entity
            ay_entity.attribs.set(
                constants.SHOTGRID_ID_ATTRIB, str(sg_entity["id"]))
            ay_entity.attribs.set(constants.SHOTGRID_TYPE_ATTRIB, sg_type)

    def server_entity(self, name):
        """AYON server data, `ownAttrib` has None for inherited values."""
        ay_entity, own, resolved = self.ay_entities[name]
        attrib = {
            self.ay_attrib: self._ay_value(resolved),
            constants.SHOTGRID_ID_ATTRIB: ay_entity.attribs.get(
                constants.SHOTGRID_ID_ATTRIB),
            constants.SHOTGRID_TYPE_ATTRIB: ay_entity.attribs.get(
                constants.SHOTGRID_TYPE_ATTRIB),
        }
        data = {
            "id": ay_entity.id,
            "attrib": attrib,
            "ownAttrib": {self.ay_attrib: self._ay_value(own)},
        }
        if ay_entity.entity_type == "task":
            data["folderId"] = ay_entity.parent_id
        else:
            data["parentId"] = ay_entity.parent_id
        return data

    def server_entities(self, entity_type):
        return [
            self.server_entity(name)
            for name, (ay_entity, _, _) in self.ay_entities.items()
            if ay_entity.entity_type == entity_type
        ]

    def assert_sg_values(self):
        """SG must hold the values resolved by AYON."""
        for name, (ay_entity, _, resolved) in self.ay_entities.items():
            sg_type = (
                "Task" if ay_entity.entity_type == "task"
                else ay_entity.folder_type
            )
            if not self.has_sg_field(sg_type):
                continue
            filters = [[constants.CUST_FIELD_CODE_ID, "is", ay_entity.id]]
            sg_entity = self.mg.find_one(sg_type, filters, [self.sg_field])
            assert sg_entity, f"{name} not found in SG"
            assert sg_entity[self.sg_field] == self.values[resolved][1], name

assert_sg_values()

SG must hold the values resolved by AYON.

Source code in services/tests/update_from_ayon/test_inherited_attribs_sync.py
211
212
213
214
215
216
217
218
219
220
221
222
223
def assert_sg_values(self):
    """SG must hold the values resolved by AYON."""
    for name, (ay_entity, _, resolved) in self.ay_entities.items():
        sg_type = (
            "Task" if ay_entity.entity_type == "task"
            else ay_entity.folder_type
        )
        if not self.has_sg_field(sg_type):
            continue
        filters = [[constants.CUST_FIELD_CODE_ID, "is", ay_entity.id]]
        sg_entity = self.mg.find_one(sg_type, filters, [self.sg_field])
        assert sg_entity, f"{name} not found in SG"
        assert sg_entity[self.sg_field] == self.values[resolved][1], name

create_sg_entities(initial_states)

Create SG entities linked to AYON ones, with initial values.

Source code in services/tests/update_from_ayon/test_inherited_attribs_sync.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def create_sg_entities(self, initial_states):
    """Create SG entities linked to AYON ones, with initial values."""
    parents = {"sh010": "seq", "sh020": "seq",
               "task010": "sh010", "task020": "sh020"}
    step = self.mg.find_one("Step", [["code", "is", "edit"]])
    for name in ("seq", "sh010", "sh020", "task010", "task020"):
        ay_entity = self.ay_entities[name][0]
        sg_type = (
            "Task" if ay_entity.entity_type == "task"
            else ay_entity.folder_type
        )
        data = {
            "project": self.sg_project,
            constants.CUST_FIELD_CODE_ID: ay_entity.id,
        }
        if sg_type == "Task":
            data.update({
                "content": name,
                "entity": self.sg_entities[parents[name]],
                "step": step,
            })
        else:
            data["code"] = name
            if sg_type == "Shot":
                data["sg_sequence"] = self.sg_entities[parents[name]]
        if self.has_sg_field(sg_type):
            data[self.sg_field] = self.values[initial_states[name]][1]

        sg_entity = self.mg.create(sg_type, data)
        self.sg_entities[name] = sg_entity
        ay_entity.attribs.set(
            constants.SHOTGRID_ID_ATTRIB, str(sg_entity["id"]))
        ay_entity.attribs.set(constants.SHOTGRID_TYPE_ATTRIB, sg_type)

server_entity(name)

AYON server data, ownAttrib has None for inherited values.

Source code in services/tests/update_from_ayon/test_inherited_attribs_sync.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
def server_entity(self, name):
    """AYON server data, `ownAttrib` has None for inherited values."""
    ay_entity, own, resolved = self.ay_entities[name]
    attrib = {
        self.ay_attrib: self._ay_value(resolved),
        constants.SHOTGRID_ID_ATTRIB: ay_entity.attribs.get(
            constants.SHOTGRID_ID_ATTRIB),
        constants.SHOTGRID_TYPE_ATTRIB: ay_entity.attribs.get(
            constants.SHOTGRID_TYPE_ATTRIB),
    }
    data = {
        "id": ay_entity.id,
        "attrib": attrib,
        "ownAttrib": {self.ay_attrib: self._ay_value(own)},
    }
    if ay_entity.entity_type == "task":
        data["folderId"] = ay_entity.parent_id
    else:
        data["parentId"] = ay_entity.parent_id
    return data

test_event_sync(hub_and_project, ay_attrib, sg_attrib, values)

Changing an attribute on a folder updates the inheriting children.

Source code in services/tests/update_from_ayon/test_inherited_attribs_sync.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
@pytest.mark.parametrize("ay_attrib, sg_attrib, values", ATTRIB_CASES)
def test_event_sync(hub_and_project, ay_attrib, sg_attrib, values):
    """Changing an attribute on a folder updates the inheriting children."""
    mg = hub_and_project["mg"]
    hub = hub_and_project["hub"]
    scenario = Scenario(
        mg, hub_and_project["project"], ay_attrib, sg_attrib, values)
    scenario.create_sg_entities({
        "seq": "old", "sh010": "old", "task010": "old",
        "sh020": "own", "task020": "own",
    })
    seq_entity = scenario.ay_entities["seq"][0]

    ay_event = {
        "topic": "entity.folder.attrib_changed",
        "project": "test_project",
        "payload": {
            "oldValue": {ay_attrib: values["old"][0]},
            "newValue": {ay_attrib: values["new"][0]},
        },
        "summary": {"entityId": seq_entity.id, "parentId": "test_project"},
        "user": "admin",
    }

    def get_folders(_project_name, parent_ids=None, **_kwargs):
        return [
            folder for folder in scenario.server_entities("folder")
            if folder["parentId"] in parent_ids
        ]

    def get_tasks(_project_name, folder_ids=None, **_kwargs):
        return [
            task for task in scenario.server_entities("task")
            if task["folderId"] in folder_ids
        ]

    hub.custom_attribs_map = scenario.custom_attribs_map

    with mock.patch.object(EntityHub, "get_or_query_entity_by_id", return_value=seq_entity), \
         mock.patch.object(ayon_api, "get_folders", side_effect=get_folders), \
         mock.patch.object(ayon_api, "get_tasks", side_effect=get_tasks), \
         mock.patch.object(mg, "batch", wraps=mg.batch) as batch_mock:
        hub.react_to_ayon_event(ay_event)

    scenario.assert_sg_values()

    # Only the children whose value changed are written.
    expected = {"task010"}
    if scenario.has_sg_field("Shot"):
        expected.add("sh010")
    batch_mock.assert_called_once()
    updated = {
        (request["entity_type"], request["entity_id"])
        for request in batch_mock.call_args.args[0]
    }
    assert updated == {
        (scenario.sg_entities[name]["type"], scenario.sg_entities[name]["id"])
        for name in expected
    }

test_manual_sync_create(mockgun_project, ay_attrib, sg_attrib, values)

New SG entities are created with the inherited values.

Source code in services/tests/update_from_ayon/test_inherited_attribs_sync.py
272
273
274
275
276
277
278
279
280
@pytest.mark.parametrize("ay_attrib, sg_attrib, values", ATTRIB_CASES)
def test_manual_sync_create(mockgun_project, ay_attrib, sg_attrib, values):
    """New SG entities are created with the inherited values."""
    mg, sg_project = mockgun_project
    scenario = Scenario(mg, sg_project, ay_attrib, sg_attrib, values)

    _run_full_sync(scenario)

    scenario.assert_sg_values()

test_manual_sync_update(mockgun_project, ay_attrib, sg_attrib, values)

Existing SG entities get the inherited values.

Source code in services/tests/update_from_ayon/test_inherited_attribs_sync.py
257
258
259
260
261
262
263
264
265
266
267
268
269
@pytest.mark.parametrize("ay_attrib, sg_attrib, values", ATTRIB_CASES)
def test_manual_sync_update(mockgun_project, ay_attrib, sg_attrib, values):
    """Existing SG entities get the inherited values."""
    mg, sg_project = mockgun_project
    scenario = Scenario(mg, sg_project, ay_attrib, sg_attrib, values)
    scenario.create_sg_entities({
        "seq": "old", "sh010": "old", "task010": "old",
        "sh020": "own", "task020": "own",
    })

    _run_full_sync(scenario)

    scenario.assert_sg_values()