Archived
0
0
This commit is contained in:
2025-12-16 21:28:53 +08:00
parent 11d130c3fd
commit 1e534e5fe5
37 changed files with 428 additions and 207 deletions

View File

@@ -21,18 +21,20 @@ class TestAtom(unittest.TestCase):
# 创建临时目录用于持久化测试
self.temp_dir = tempfile.TemporaryDirectory()
self.temp_path = pathlib.Path(self.temp_dir.name)
# 创建默认配置
self.config = ConfigFile(pathlib.Path(__file__).parent.parent.parent.parent /
"src/heurams/default/config/config.toml")
self.config = ConfigFile(
pathlib.Path(__file__).parent.parent.parent.parent
/ "src/heurams/default/config/config.toml"
)
# 使用 ConfigContext 设置配置
self.config_ctx = ConfigContext(self.config)
self.config_ctx.__enter__()
# 清空全局注册表
atom_registry.clear()
def tearDown(self):
"""在每个测试之后运行"""
self.config_ctx.__exit__(None, None, None)
@@ -45,7 +47,7 @@ class TestAtom(unittest.TestCase):
self.assertEqual(atom.ident, "test_atom")
self.assertIn("test_atom", atom_registry)
self.assertEqual(atom_registry["test_atom"], atom)
# 检查 registry 默认值
self.assertIsNone(atom.registry["nucleon"])
self.assertIsNone(atom.registry["electron"])
@@ -58,10 +60,10 @@ class TestAtom(unittest.TestCase):
"""测试 link 方法"""
atom = Atom("test_link")
nucleon = Nucleon("test_nucleon", {"content": "test content"})
atom.link("nucleon", nucleon)
self.assertEqual(atom.registry["nucleon"], nucleon)
# 测试链接不支持的键
with self.assertRaises(ValueError):
atom.link("invalid_key", "value")
@@ -70,8 +72,8 @@ class TestAtom(unittest.TestCase):
"""测试 link 后触发 do_eval"""
atom = Atom("test_eval_trigger")
nucleon = Nucleon("test_nucleon", {"content": "eval:1+1"})
with patch.object(atom, 'do_eval') as mock_do_eval:
with patch.object(atom, "do_eval") as mock_do_eval:
atom.link("nucleon", nucleon)
mock_do_eval.assert_called_once()
@@ -80,16 +82,16 @@ class TestAtom(unittest.TestCase):
atom = Atom("test_persist_toml")
nucleon = Nucleon("test_nucleon", {"content": "test"})
atom.link("nucleon", nucleon)
# 设置路径
test_path = self.temp_path / "test.toml"
atom.link("nucleon_path", test_path)
atom.persist("nucleon")
# 验证文件存在且内容正确
self.assertTrue(test_path.exists())
with open(test_path, 'r') as f:
with open(test_path, "r") as f:
data = toml.load(f)
self.assertEqual(data["ident"], "test_nucleon")
self.assertEqual(data["payload"]["content"], "test")
@@ -99,14 +101,14 @@ class TestAtom(unittest.TestCase):
atom = Atom("test_persist_json")
electron = Electron("test_electron", {})
atom.link("electron", electron)
test_path = self.temp_path / "test.json"
atom.link("electron_path", test_path)
atom.persist("electron")
self.assertTrue(test_path.exists())
with open(test_path, 'r') as f:
with open(test_path, "r") as f:
data = json.load(f)
self.assertIn("supermemo2", data)
@@ -117,7 +119,7 @@ class TestAtom(unittest.TestCase):
atom.link("nucleon", nucleon)
atom.link("nucleon_path", self.temp_path / "test.txt")
atom.registry["nucleon_fmt"] = "invalid"
with self.assertRaises(KeyError):
atom.persist("nucleon")
@@ -127,7 +129,7 @@ class TestAtom(unittest.TestCase):
nucleon = Nucleon("test_nucleon", {})
atom.link("nucleon", nucleon)
# 不设置 nucleon_path
with self.assertRaises(TypeError):
atom.persist("nucleon")
@@ -135,26 +137,26 @@ class TestAtom(unittest.TestCase):
"""测试 __getitem__ 和 __setitem__"""
atom = Atom("test_getset")
nucleon = Nucleon("test_nucleon", {})
atom["nucleon"] = nucleon
self.assertEqual(atom["nucleon"], nucleon)
# 测试不支持的键
with self.assertRaises(KeyError):
_ = atom["invalid_key"]
with self.assertRaises(KeyError):
atom["invalid_key"] = "value"
def test_do_eval_with_eval_string(self):
"""测试 do_eval 处理 eval: 字符串"""
atom = Atom("test_do_eval")
nucleon = Nucleon("test_nucleon", {
"content": "eval:'hello' + ' world'",
"number": "eval:2 + 3"
})
nucleon = Nucleon(
"test_nucleon",
{"content": "eval:'hello' + ' world'", "number": "eval:2 + 3"},
)
atom.link("nucleon", nucleon)
# do_eval 应该在链接时自动调用
# 检查 eval 表达式是否被求值
self.assertEqual(nucleon.payload["content"], "hello world")
@@ -163,11 +165,11 @@ class TestAtom(unittest.TestCase):
def test_do_eval_with_config_access(self):
"""测试 do_eval 访问配置"""
atom = Atom("test_eval_config")
nucleon = Nucleon("test_nucleon", {
"max_riddles": "eval:default['mcq']['max_riddles_num']"
})
nucleon = Nucleon(
"test_nucleon", {"max_riddles": "eval:default['mcq']['max_riddles_num']"}
)
atom.link("nucleon", nucleon)
# 配置中 puzzles.mcq.max_riddles_num = 2
self.assertEqual(nucleon.payload["max_riddles"], 2)
@@ -185,15 +187,15 @@ class TestAtom(unittest.TestCase):
# 创建多个 Atom
atom1 = Atom("atom1")
atom2 = Atom("atom2")
self.assertEqual(len(atom_registry), 2)
self.assertEqual(atom_registry["atom1"], atom1)
self.assertEqual(atom_registry["atom2"], atom2)
# 测试 bidict 的反向查找
self.assertEqual(atom_registry.inverse[atom1], "atom1")
self.assertEqual(atom_registry.inverse[atom2], "atom2")
if __name__ == '__main__':
unittest.main()
if __name__ == "__main__":
unittest.main()