langchain/tests/unit_tests/test_python.py

41 lines
1.1 KiB
Python
Raw Normal View History

2022-10-31 01:09:04 +00:00
"""Test functionality of Python REPL."""
from langchain.python import PythonREPL
def test_python_repl() -> None:
"""Test functionality when globals/locals are not provided."""
repl = PythonREPL()
# Run a simple initial command.
repl.run("foo = 1")
assert repl._locals["foo"] == 1
# Now run a command that accesses `foo` to make sure it still has it.
repl.run("bar = foo * 2")
assert repl._locals["bar"] == 2
def test_python_repl_no_previous_variables() -> None:
"""Test that it does not have access to variables created outside the scope."""
foo = 3 # noqa: F841
repl = PythonREPL()
2022-12-19 02:51:23 +00:00
output = repl.run("print(foo)")
assert output == "name 'foo' is not defined"
2022-10-31 01:09:04 +00:00
def test_python_repl_pass_in_locals() -> None:
"""Test functionality when passing in locals."""
_locals = {"foo": 4}
repl = PythonREPL(_locals=_locals)
repl.run("bar = foo * 2")
assert repl._locals["bar"] == 8
2022-12-04 23:57:36 +00:00
def test_functionality() -> None:
"""Test correct functionality."""
chain = PythonREPL()
code = "print(1 + 1)"
output = chain.run(code)
assert output == "2\n"