Spaces:
Paused
Paused
File size: 1,677 Bytes
4b03eed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | # -*- coding: utf-8 -*-
"""Tests for the configurable ID factory."""
import re
from unittest.async_case import IsolatedAsyncioTestCase
from agentscope import set_id_factory
from agentscope.message import Msg, TextBlock
_HEX32_RE = re.compile(r"^[0-9a-f]{32}$")
class IdFactoryTest(IsolatedAsyncioTestCase):
"""Tests for set_id_factory."""
async def asyncSetUp(self) -> None:
"""Save the current factory before each test."""
import agentscope._utils._common as common
# pylint: disable=protected-access
self._saved_factory = common._id_factory
async def test_default_id_factory_returns_hex32(self) -> None:
"""The default ID factory returns uuid.uuid4().hex."""
msg = Msg(
name="test",
content=[TextBlock(text="hello")],
role="user",
)
self.assertRegex(msg.id, _HEX32_RE)
self.assertRegex(msg.content[0].id, _HEX32_RE)
async def test_custom_factory_affects_entities(self) -> None:
"""After ``set_id_factory``, entities use the custom factory."""
set_id_factory(lambda: "custom-entity-id")
msg = Msg(
name="test",
content=[TextBlock(text="hello")],
role="user",
)
self.assertEqual(msg.id, "custom-entity-id")
self.assertEqual(msg.content[0].id, "custom-entity-id")
async def asyncTearDown(self) -> None:
"""Restore the original factory after each test."""
import agentscope._utils._common as common
# pylint: disable=protected-access
common._id_factory = self._saved_factory
|