test_fstring.py
1 # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> 2 # 3 # SPDX-License-Identifier: Apache-2.0 4 5 import pytest 6 7 from haystack.testing.sample_components import FString 8 9 10 def test_fstring_with_one_var(): 11 fstring = FString(template="Hello, {name}!", variables=["name"]) 12 output = fstring.run(name="Alice") 13 assert output == {"string": "Hello, Alice!"} 14 15 16 def test_fstring_with_no_vars(): 17 fstring = FString(template="No variables in this template.", variables=[]) 18 output = fstring.run() 19 assert output == {"string": "No variables in this template."} 20 21 22 def test_fstring_with_template_at_runtime(): 23 fstring = FString(template="Hello {name}", variables=["name"]) 24 output = fstring.run(template="Goodbye {name}!", name="Alice") 25 assert output == {"string": "Goodbye Alice!"} 26 27 28 def test_fstring_with_vars_mismatch(): 29 fstring = FString(template="Hello {name}", variables=["name"]) 30 with pytest.raises(KeyError): 31 fstring.run(template="Goodbye {person}!", name="Alice") 32 33 34 def test_fstring_with_vars_in_excess(): 35 fstring = FString(template="Hello {name}", variables=["name"]) 36 output = fstring.run(template="Goodbye!", name="Alice") 37 assert output == {"string": "Goodbye!"} 38 39 40 def test_fstring_with_vars_missing(): 41 fstring = FString(template="{greeting}, {name}!", variables=["name"]) 42 with pytest.raises(KeyError): 43 fstring.run(greeting="Hello")