Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graphs integration tests for pythonboard #193

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions seeds/pythonboard/src/breadboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def run(
if result.skip:
if probe:
probe.dispatchEvent(
ProbeEvent("skip", descriptor.__dict__ | inputs | missingInputs)
ProbeEvent("skip", descriptor.__dict__ | inputs | {"missingInputs": missingInputs})
)
continue

Expand Down Expand Up @@ -166,19 +166,31 @@ async def await_js(func, args):
except TypeError as e:
# This can occur when javascript object is empty.
res = {}

if probe:
probe.dispatchEvent(
ProbeEvent("node", descriptor.__dict__ | inputs | res | {
"validatorMetadata": [validator.getValidatorMetadata(descriptor) for validator in self._validators]
})
)
return res
outputsPromise = await_js(handler, inputs)
else:
async def await_awaitable(func, args):
if func is not None:
res = await func(args)
else:
res = args
if probe:
probe.dispatchEvent(
ProbeEvent("node", descriptor.__dict__ | inputs | res | {
"validatorMetadata": [validator.getValidatorMetadata(descriptor) for validator in self._validators]
})
)
return res
# TODO(kevxiao): Handle shouldInvokeHandler with probe
outputsPromise = (
handler(inputs) if shouldInvokeHandler else wrap_future(beforehandlerDetail.outputs)
)

# TODO(kevxiao): should append probe.dispatchEvent to part of promise/awaitable instead of running now
if probe:
probe.dispatchEvent(
ProbeEvent("node", descriptor | inputs | outputsPromise | {
"validatorMetadata": [validator.getValidatorMetadata(descriptor) for validator in self._validators]
})
handler(inputs) # if shouldInvokeHandler else wrap_future(beforehandlerDetail["outputs"])
)

result.outputsPromise = outputsPromise
Expand Down
15 changes: 11 additions & 4 deletions seeds/pythonboard/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
from javascript import require, AsyncTask
import json
import sys
import os
project_path = os.path.dirname(__file__)
sys.path.append(project_path)

from breadboard import Board

class LogProbe():
def dispatchEvent(self, probe_event):
print(f"Probe Event: {probe_event.__dict__}")

async def main():
graph_path = sys.argv[1]
async def main(graph_path: str):
breadboard = await Board.load(graph_path)

print("Let's traverse a graph!")
Expand All @@ -27,7 +32,8 @@ async def main():
if len(props) > 0:
first_prop = next(iter(props.values()))
message = first_prop.get("description", message)
res.inputs = {"text": input(message+ "\n")}
user_input = input(message+ "\n")
res.inputs = {"text": user_input} if user_input else {"exit": True}
elif res.type == "output":
if res.outputs and res.outputs['text']:
for key in res.outputs:
Expand All @@ -47,4 +53,5 @@ async def main():
raise e

if __name__ == "__main__":
asyncio.run(main())
graph_path = sys.argv[1]
asyncio.run(main(graph_path))
4 changes: 3 additions & 1 deletion seeds/pythonboard/tests/test_breadboard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
import sys
sys.path.append("..")
import os
project_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(project_path)
from src.traversal.state import MachineEdgeState
from src.traversal.traversal_types import Edge

Expand Down
83 changes: 83 additions & 0 deletions seeds/pythonboard/tests/test_graphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import unittest
import sys
import os
project_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(project_path)
from src.traversal.state import MachineEdgeState
from src.traversal.traversal_types import Edge
from src.main import main
from src.breadboard import Board
from src import breadboard
from unittest import mock

class TestKit():
output = "hello!"
index = 0
@staticmethod
async def generateText(_inputs):
if type(TestKit.output) == list:
output = TestKit.output[index]
index = max(index + 1, len(TestKit.output) - 1)
else:
output = TestKit.output
return {"completion": output}

@staticmethod
async def secrets(_inputs):
return {k: f"test_k" for k in _inputs["keys"]}

def __init__(self, _arg):
self.handlers = {}
self.handlers["generateText"] = TestKit.generateText
self.handlers["secrets"] = TestKit.secrets

class TestGraphs(unittest.IsolatedAsyncioTestCase):

@mock.patch('src.main.print', create=True)
@mock.patch('src.main.input', create=True)
@mock.patch('src.main.Board.fromGraphDescriptor')
async def test_accumulating_context(self, mock_ctr, mock_input, mock_print):
mock_input.side_effect = ["First input", "Second input", ""]
mock_print.side_effect = print
original_fun = Board.fromGraphDescriptor

async def fromGraphDescriptor(graph):
res = await original_fun(graph)
res.addKit(TestKit)
return res
mock_ctr.side_effect = fromGraphDescriptor
await main("seeds/graph-playground/graphs/accumulating-context.json")
# verify that multiple rounds were called.
# Verify that output is correct.
expected_calls = [
mock.call("Let's traverse a graph!"),
mock.call("hello!"),
mock.call("hello!"),
mock.call("Awesome work! Let's do this again sometime."),
]
mock_print.assert_has_calls(expected_calls, any_order=False)

@mock.patch('src.main.print', create=True)
@mock.patch('src.main.input', create=True)
@mock.patch('src.main.Board.fromGraphDescriptor')
async def test_math(self, mock_ctr, mock_input, mock_print):
mock_input.side_effect = ["What's one plus seven?"]
mock_print.side_effect = print
original_fun = Board.fromGraphDescriptor
async def fromGraphDescriptor(graph):
res = await original_fun(graph)
res.addKit(TestKit)
return res
TestKit.output = "```js\nfunction compute() {\nreturn 1 + 7;\n}\n```"
mock_ctr.side_effect = fromGraphDescriptor
await main("seeds/graph-playground/graphs/math.json")
expected_calls = [
mock.call("Let's traverse a graph!"),
mock.call("8"),
mock.call("Awesome work! Let's do this again sometime."),
]
mock_print.assert_has_calls(expected_calls, any_order=False)


if __name__ == '__main__':
unittest.main()
9 changes: 6 additions & 3 deletions seeds/pythonboard/tests/test_machine.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest
import asyncio
import os
import sys
sys.path.append("..")
project_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(project_path)
from src import util
from src.traversal.state import MachineEdgeState
from src.traversal.traversal_types import (
Expand Down Expand Up @@ -37,15 +39,16 @@ class TestGraphDescriptor(GraphDescriptor):

class TestMachine(unittest.IsolatedAsyncioTestCase):
async def _run_file(self, filename):
print(f"Testing {filename}")
async with aiofiles.open(filename, 'r') as handle:
# read the contents of the file
data = await handle.read()
data = data.replace('"from":', '"previous":')
data = data.replace('"to":', '"next":')
data = data.replace('"in":', '"input":')
graph = TestGraphDescriptor.from_json(data)
if "skip" in graph.__dict__.get("title", []):
self.log("Skipped")
if "skip" in (graph.__dict__.get("title") or []):
print("Skipped")
return
machine = TraversalMachine(graph)
outputs = []
Expand Down