Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions shotgun_api3/lib/mockgun/mockgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_mockgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading