mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-01 19:12:42 +00:00
factor out mock python repl (#43)
This commit is contained in:
34
tests/unit_tests/test_python.py
Normal file
34
tests/unit_tests/test_python.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""Test functionality of Python REPL."""
|
||||
|
||||
import pytest
|
||||
|
||||
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()
|
||||
with pytest.raises(NameError):
|
||||
repl.run("print(foo)")
|
||||
|
||||
|
||||
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
|
Reference in New Issue
Block a user