You've already forked HeurAMS-legacy
fix
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -11,10 +11,12 @@ class TestElectron(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
# 模拟 timer.get_timestamp 返回固定值
|
||||
self.timestamp_patcher = patch('heurams.kernel.particles.electron.timer.get_timestamp')
|
||||
self.timestamp_patcher = patch(
|
||||
"heurams.kernel.particles.electron.timer.get_timestamp"
|
||||
)
|
||||
self.mock_get_timestamp = self.timestamp_patcher.start()
|
||||
self.mock_get_timestamp.return_value = 1234567890.0
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
self.timestamp_patcher.stop()
|
||||
|
||||
@@ -67,9 +69,9 @@ class TestElectron(unittest.TestCase):
|
||||
electron.modify("interval", 5)
|
||||
self.assertEqual(electron.algodata[electron.algo]["interval"], 5)
|
||||
self.assertEqual(electron.algodata[electron.algo]["last_modify"], 1234567890.0)
|
||||
|
||||
|
||||
# 修改不存在的字段应记录警告但不引发异常
|
||||
with patch('heurams.kernel.particles.electron.logger.warning') as mock_warning:
|
||||
with patch("heurams.kernel.particles.electron.logger.warning") as mock_warning:
|
||||
electron.modify("unknown_field", 99)
|
||||
mock_warning.assert_called_once()
|
||||
|
||||
@@ -85,7 +87,7 @@ class TestElectron(unittest.TestCase):
|
||||
def test_is_due(self):
|
||||
"""测试 is_due 方法"""
|
||||
electron = Electron("test_electron")
|
||||
with patch.object(electron.algo, 'is_due') as mock_is_due:
|
||||
with patch.object(electron.algo, "is_due") as mock_is_due:
|
||||
mock_is_due.return_value = 1
|
||||
result = electron.is_due()
|
||||
mock_is_due.assert_called_once_with(electron.algodata)
|
||||
@@ -94,7 +96,7 @@ class TestElectron(unittest.TestCase):
|
||||
def test_rate(self):
|
||||
"""测试 rate 方法"""
|
||||
electron = Electron("test_electron")
|
||||
with patch.object(electron.algo, 'rate') as mock_rate:
|
||||
with patch.object(electron.algo, "rate") as mock_rate:
|
||||
mock_rate.return_value = "good"
|
||||
result = electron.rate()
|
||||
mock_rate.assert_called_once_with(electron.algodata)
|
||||
@@ -103,7 +105,7 @@ class TestElectron(unittest.TestCase):
|
||||
def test_nextdate(self):
|
||||
"""测试 nextdate 方法"""
|
||||
electron = Electron("test_electron")
|
||||
with patch.object(electron.algo, 'nextdate') as mock_nextdate:
|
||||
with patch.object(electron.algo, "nextdate") as mock_nextdate:
|
||||
mock_nextdate.return_value = 1234568000
|
||||
result = electron.nextdate()
|
||||
mock_nextdate.assert_called_once_with(electron.algodata)
|
||||
@@ -112,7 +114,7 @@ class TestElectron(unittest.TestCase):
|
||||
def test_revisor(self):
|
||||
"""测试 revisor 方法"""
|
||||
electron = Electron("test_electron")
|
||||
with patch.object(electron.algo, 'revisor') as mock_revisor:
|
||||
with patch.object(electron.algo, "revisor") as mock_revisor:
|
||||
electron.revisor(quality=3, is_new_activation=True)
|
||||
mock_revisor.assert_called_once_with(electron.algodata, 3, True)
|
||||
|
||||
@@ -144,7 +146,7 @@ class TestElectron(unittest.TestCase):
|
||||
electron.activate()
|
||||
self.assertEqual(electron["ident"], "test_electron")
|
||||
self.assertEqual(electron["is_activated"], 1)
|
||||
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
_ = electron["nonexistent_key"]
|
||||
|
||||
@@ -154,7 +156,7 @@ class TestElectron(unittest.TestCase):
|
||||
electron["interval"] = 10
|
||||
self.assertEqual(electron.algodata[electron.algo]["interval"], 10)
|
||||
self.assertEqual(electron.algodata[electron.algo]["last_modify"], 1234567890.0)
|
||||
|
||||
|
||||
with self.assertRaises(AttributeError):
|
||||
electron["ident"] = "new_ident"
|
||||
|
||||
@@ -173,5 +175,5 @@ class TestElectron(unittest.TestCase):
|
||||
self.assertEqual(placeholder.algo, algorithms["supermemo2"])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -9,7 +9,9 @@ class TestNucleon(unittest.TestCase):
|
||||
|
||||
def test_init(self):
|
||||
"""测试初始化"""
|
||||
nucleon = Nucleon("test_id", {"content": "hello", "note": "world"}, {"author": "test"})
|
||||
nucleon = Nucleon(
|
||||
"test_id", {"content": "hello", "note": "world"}, {"author": "test"}
|
||||
)
|
||||
self.assertEqual(nucleon.ident, "test_id")
|
||||
self.assertEqual(nucleon.payload, {"content": "hello", "note": "world"})
|
||||
self.assertEqual(nucleon.metadata, {"author": "test"})
|
||||
@@ -27,7 +29,7 @@ class TestNucleon(unittest.TestCase):
|
||||
self.assertEqual(nucleon["ident"], "test_id")
|
||||
self.assertEqual(nucleon["content"], "hello")
|
||||
self.assertEqual(nucleon["note"], "world")
|
||||
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
_ = nucleon["nonexistent"]
|
||||
|
||||
@@ -58,16 +60,23 @@ class TestNucleon(unittest.TestCase):
|
||||
|
||||
def test_do_eval_with_metadata_access(self):
|
||||
"""测试 do_eval 访问元数据"""
|
||||
nucleon = Nucleon("test_id", {"result": "eval:nucleon.metadata.get('value', 0)"}, {"value": 42})
|
||||
nucleon = Nucleon(
|
||||
"test_id",
|
||||
{"result": "eval:nucleon.metadata.get('value', 0)"},
|
||||
{"value": 42},
|
||||
)
|
||||
nucleon.do_eval()
|
||||
self.assertEqual(nucleon.payload["result"], "42")
|
||||
|
||||
def test_do_eval_nested(self):
|
||||
"""测试 do_eval 处理嵌套结构"""
|
||||
nucleon = Nucleon("test_id", {
|
||||
"list": ["eval:2*3", "normal"],
|
||||
"dict": {"key": "eval:'hello' + ' world'"}
|
||||
})
|
||||
nucleon = Nucleon(
|
||||
"test_id",
|
||||
{
|
||||
"list": ["eval:2*3", "normal"],
|
||||
"dict": {"key": "eval:'hello' + ' world'"},
|
||||
},
|
||||
)
|
||||
nucleon.do_eval()
|
||||
self.assertEqual(nucleon.payload["list"][0], "6")
|
||||
self.assertEqual(nucleon.payload["list"][1], "normal")
|
||||
@@ -95,5 +104,5 @@ class TestNucleon(unittest.TestCase):
|
||||
self.assertEqual(placeholder.metadata, {})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user