Skip to content

Commit ab3201b

Browse files
authored
fix(uuid): record containing uuid doesn't crash at create anymore (#324)
1 parent 1439018 commit ab3201b

File tree

2 files changed

+95
-1
lines changed
  • src/agent_toolkit

2 files changed

+95
-1
lines changed

src/agent_toolkit/forestadmin/agent_toolkit/resources/collections/crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ async def extract_data(
402402
field = collection.get_field(field_name)
403403
if is_column(field):
404404
if field["column_type"] == PrimitiveType.UUID:
405-
record[field_name] = UUID(value)
405+
record[field_name] = UUID(value) if isinstance(value, str) else value
406406
else:
407407
record[field_name] = value
408408
elif (

src/agent_toolkit/tests/resources/collections/test_crud.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
from unittest import TestCase
77
from unittest.mock import ANY, AsyncMock, Mock, patch
8+
from uuid import UUID
89

910
if sys.version_info >= (3, 9):
1011
import zoneinfo
@@ -222,6 +223,53 @@ def _create_collections(cls):
222223
},
223224
)
224225

226+
# to test with uuid
227+
cls.collection_book = cls._create_collection(
228+
"book",
229+
{
230+
"id": {
231+
"column_type": PrimitiveType.UUID,
232+
"is_primary_key": True,
233+
"type": FieldType.COLUMN,
234+
"filter_operators": {Operator.IN, Operator.EQUAL},
235+
},
236+
"name": {
237+
"column_type": PrimitiveType.STRING,
238+
"is_primary_key": False,
239+
"type": FieldType.COLUMN,
240+
"filter_operators": {Operator.IN, Operator.EQUAL},
241+
},
242+
"author_id": {
243+
"column_type": PrimitiveType.UUID,
244+
"type": FieldType.COLUMN,
245+
"filter_operators": {Operator.IN, Operator.EQUAL},
246+
},
247+
"author": {
248+
"type": FieldType.MANY_TO_ONE,
249+
"foreign_collection": "author",
250+
"foreign_key_target": "id",
251+
"foreign_key": "author_id",
252+
},
253+
},
254+
)
255+
cls.collection_author = cls._create_collection(
256+
"Author",
257+
{
258+
"id": {
259+
"column_type": PrimitiveType.UUID,
260+
"is_primary_key": True,
261+
"type": FieldType.COLUMN,
262+
"filter_operators": {Operator.IN, Operator.EQUAL},
263+
},
264+
"name": {
265+
"column_type": PrimitiveType.STRING,
266+
"is_primary_key": False,
267+
"type": FieldType.COLUMN,
268+
"filter_operators": {Operator.IN, Operator.EQUAL},
269+
},
270+
},
271+
)
272+
225273
@classmethod
226274
def setUpClass(cls) -> None:
227275
cls.loop = asyncio.new_event_loop()
@@ -243,6 +291,8 @@ def setUpClass(cls) -> None:
243291
"cart": cls.collection_cart,
244292
"product": cls.collection_product,
245293
"tag": cls.collection_tag,
294+
# for uuid
295+
"author": cls.collection_author,
246296
}
247297
cls.datasource_composite.add_datasource(cls.datasource)
248298

@@ -957,6 +1007,50 @@ def test_add_should_return_to_many_relations_as_link(self):
9571007
},
9581008
)
9591009

1010+
def test_add_with_uuid_should_work_with_uuid_as_obj_or_str(self):
1011+
for uuid in ["123e4567-e89b-12d3-a456-426614174000", UUID("123e4567-e89b-12d3-a456-426614174000")]:
1012+
request = RequestCollection(
1013+
RequestMethod.POST,
1014+
self.collection_book,
1015+
body={
1016+
"data": {
1017+
"attributes": {"name": "Foundation", "id": uuid, "author_id": uuid},
1018+
"relationships": {},
1019+
},
1020+
"type": "book",
1021+
},
1022+
query={
1023+
"collection_name": "book",
1024+
"timezone": "Europe/Paris",
1025+
},
1026+
headers={},
1027+
client_ip="127.0.0.1",
1028+
)
1029+
crud_resource = CrudResource(
1030+
self.datasource_composite,
1031+
self.datasource,
1032+
self.permission_service,
1033+
self.ip_white_list_service,
1034+
self.options,
1035+
)
1036+
with patch.object(
1037+
self.collection_book,
1038+
"create",
1039+
new_callable=AsyncMock,
1040+
return_value=[{"name": "Foundation", "id": str(uuid), "author_id": str(uuid)}],
1041+
) as mock_create:
1042+
self.loop.run_until_complete(crud_resource.add(request))
1043+
mock_create.assert_any_await(
1044+
FAKE_USER,
1045+
[
1046+
{
1047+
"name": "Foundation",
1048+
"id": UUID("123e4567-e89b-12d3-a456-426614174000"),
1049+
"author_id": UUID("123e4567-e89b-12d3-a456-426614174000"),
1050+
}
1051+
],
1052+
)
1053+
9601054
# list
9611055
def test_list(self):
9621056
mock_orders = [{"id": 10, "cost": 200}, {"id": 11, "cost": 201}]

0 commit comments

Comments
 (0)