From 3d389ba865f6fcfdd1720e193962da37008275be Mon Sep 17 00:00:00 2001 From: Patrick Wolf <758811+patrickwolf@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:00:00 +0000 Subject: [PATCH] Mockgun: return default values in the types the server returns New records took their defaults from the schema as stored: an unset checkbox could be None, empty text "", and a number the string "0". The server returns False, None and 0. Co-Authored-By: Claude Opus 5.5 --- shotgun_api3/lib/mockgun/mockgun.py | 22 +++++++++------- tests/test_mockgun.py | 40 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/shotgun_api3/lib/mockgun/mockgun.py b/shotgun_api3/lib/mockgun/mockgun.py index 522e162d9..f8a916524 100644 --- a/shotgun_api3/lib/mockgun/mockgun.py +++ b/shotgun_api3/lib/mockgun/mockgun.py @@ -564,21 +564,25 @@ def _validate_entity_fields(self, entity_type, fields): def _get_default_value(self, entity_type, field): field_info = self._schema[entity_type][field] - if field_info["data_type"]["value"] == "multi_entity": + field_type = field_info["data_type"]["value"] + default_value = field_info["properties"]["default_value"]["value"] + # Return the default as Flow Production Tracking does, whatever form the schema stores it in. + if field_type == "multi_entity": default_value = [] - else: - default_value = field_info["properties"]["default_value"]["value"] + elif field_type == "checkbox": + default_value = bool(default_value) + elif field_type == "text" and default_value == "": + default_value = None + elif field_type in ("number", "percent", "duration", "timecode") and isinstance(default_value, str): + default_value = int(default_value) if default_value else None + elif field_type in ("float", "currency") and isinstance(default_value, str): + default_value = float(default_value) if default_value else None return default_value def _get_new_row(self, entity_type): row = {"type": entity_type, "__retired": False} for field in self._schema[entity_type]: - field_info = self._schema[entity_type][field] - if field_info["data_type"]["value"] == "multi_entity": - default_value = [] - else: - default_value = field_info["properties"]["default_value"]["value"] - row[field] = default_value + row[field] = self._get_default_value(entity_type, field) return row def _compare(self, field_type: str, lval: Any, operator: str, rval: Any) -> bool: diff --git a/tests/test_mockgun.py b/tests/test_mockgun.py index b12c318f7..2b01221a5 100644 --- a/tests/test_mockgun.py +++ b/tests/test_mockgun.py @@ -755,5 +755,45 @@ def test_set_server_params_with_url_with_path(self): self.assertEqual(mockgun.config.api_path, "/something/api3/json") +class TestDefaultValues(unittest.TestCase): + """ + A new record holds its defaults in the types Flow Production Tracking returns. + """ + + def setUp(self): + self._mockgun = Mockgun( + "https://test.shotgunstudio.com", login="user", password="1234" + ) + + def test_unset_checkbox_is_false(self): + # The schema stores None as this checkbox's default. + project = self._mockgun.create("Project", {"name": "prj"}) + found = self._mockgun.find_one( + "Project", [["id", "is", project["id"]]], ["current_user_favorite"] + ) + self.assertIs(found["current_user_favorite"], False) + + def test_empty_text_is_none(self): + # The schema stores "" as this text field's default. + user = self._mockgun.create("HumanUser", {"name": "someone"}) + found = self._mockgun.find_one( + "HumanUser", [["id", "is", user["id"]]], ["email"] + ) + self.assertIsNone(found["email"]) + + def test_number_stored_as_string(self): + # Mockgun caches the schema for every instance, so the change is undone after the test. + default = self._mockgun._schema["Shot"]["sg_cut_order"]["properties"][ + "default_value" + ] + self.addCleanup(default.__setitem__, "value", default["value"]) + default["value"] = "0" + shot = self._mockgun.create("Shot", {"code": "shot"}) + found = self._mockgun.find_one( + "Shot", [["id", "is", shot["id"]]], ["sg_cut_order"] + ) + self.assertEqual(found["sg_cut_order"], 0) + + if __name__ == "__main__": unittest.main()