File size: 2,165 Bytes
3d3d712 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
from injector import Injector
from taskweaver.code_interpreter.code_verification import code_snippet_verification
from taskweaver.logging import LoggingModule
app_injector = Injector(
[LoggingModule],
)
def test_plugin_only():
allowed_modules = []
code_snippet = (
"anomaly_detection()\n"
"s = timext()\n"
"result, var = anomaly_detection()\n"
"result, var\n"
"result\n"
"var\n"
"s\n"
)
code_verify_errors = code_snippet_verification(
code_snippet,
["anomaly_detection"],
plugin_only=True,
allowed_modules=allowed_modules,
code_verification_on=True,
)
print("---->", code_verify_errors)
assert len(code_verify_errors) == 2
def test_import_allowed():
allowed_modules = ["pandas", "matplotlib"]
code_snippet = (
"import numpy as np\n"
"import matplotlib.pyplot as plt\n"
"random_numbers = np.random.normal(size=100)\n"
"plt.hist(random_numbers, bins=10, alpha=0.5)\n"
"plt.title('Distribution of Random Numbers')\n"
"plt.xlabel('Value')\n"
"plt.ylabel('Frequency')\n"
"# Displaying the plot\n"
"plt.show()\n"
)
code_verify_errors = code_snippet_verification(
code_snippet,
["anomaly_detection"],
plugin_only=False,
allowed_modules=allowed_modules,
code_verification_on=True,
)
print("---->", code_verify_errors)
assert len(code_verify_errors) == 1
def test_normal_code():
plugin_only = False
allowed_modules = []
code_snippet = (
"with open('file.txt', 'r') as file:\n"
" content = file.read()\n"
" print(content)\n"
"def greet(name):\n"
" return f'Hello, {name}!'\n"
"name = 'John'\n"
"print(greet(name))\n"
)
code_verify_errors = code_snippet_verification(
code_snippet,
["anomaly_detection"],
plugin_only=plugin_only,
allowed_modules=allowed_modules,
code_verification_on=True,
)
print("---->", code_verify_errors)
assert len(code_verify_errors) == 0
|