From 2a435155a5de1ae49626b15acd0c197dd4872f30 Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Fri, 2 Aug 2024 17:02:17 -0400 Subject: [PATCH] add tests for function and default-ctor component cloning --- lenskit/tests/test_pipeline_config.py | 43 ++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/lenskit/tests/test_pipeline_config.py b/lenskit/tests/test_pipeline_config.py index a2730cbef..a73d89835 100644 --- a/lenskit/tests/test_pipeline_config.py +++ b/lenskit/tests/test_pipeline_config.py @@ -17,10 +17,21 @@ class Prefixer(AutoConfig): def __init__(self, prefix: str = "hello"): self.prefix = prefix - def __call__(self, msg: str): + def __call__(self, msg: str) -> str: return self.prefix + msg +class Question: + "test component that is not configurable but is a class" + + def __call__(self, msg: str) -> str: + return msg + "?" + + +def exclaim(msg: str) -> str: + return msg + "!" + + def test_auto_config_roundtrip(): comp = Prefixer("FOOBIE BLETCH") @@ -65,3 +76,33 @@ def test_pipeline_clone(): assert n2.component.prefix == comp.prefix assert p2.run(msg="HACKEM MUCHE") == "scroll named HACKEM MUCHE" + + +def test_pipeline_clone_with_function(): + comp = Prefixer("scroll named ") + + pipe = Pipeline() + msg = pipe.create_input("msg", str) + pfx = pipe.add_component("prefix", comp, msg=msg) + pipe.add_component("exclaim", exclaim, msg=pfx) + + assert pipe.run(msg="FOOBIE BLETCH") == "scroll named FOOBIE BLETCH!" + + p2 = pipe.clone() + + assert p2.run(msg="HACKEM MUCHE") == "scroll named HACKEM MUCHE!" + + +def test_pipeline_clone_with_nonconfig_class(): + comp = Prefixer("scroll named ") + + pipe = Pipeline() + msg = pipe.create_input("msg", str) + pfx = pipe.add_component("prefix", comp, msg=msg) + pipe.add_component("question", Question(), msg=pfx) + + assert pipe.run(msg="FOOBIE BLETCH") == "scroll named FOOBIE BLETCH?" + + p2 = pipe.clone() + + assert p2.run(msg="HACKEM MUCHE") == "scroll named HACKEM MUCHE?"