Skip to content

Commit

Permalink
added docs and renamed toy functions for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
comane committed Mar 7, 2024
1 parent eb11bee commit bff2455
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/reportengine/tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,34 @@
from reportengine.dag import DAG
from reportengine.utils import ChainMap
from reportengine import namespaces
from reportengine.resourcebuilder import (ResourceExecutor, CallSpec)
from reportengine.resourcebuilder import ResourceExecutor, CallSpec

"""
Define some simple functions that will be used as nodes in the DAG.
"""


def node_1(param):
print("Executing node_1")
time.sleep(0.1)
return "node_1_result: %s" % param


def node_2_1(node_1_result):
print("Executing node_2_1")
time.sleep(0.2)
return node_1_result*2
return node_1_result * 2


def node_2_2(node_1_result):
print("Executing node_2_2")
time.sleep(0.2)
return node_1_result*3
return node_1_result * 3


def node_3(node_2_1_result, node_2_2_result, param=None):
print("executing node_3")
return (node_2_1_result+node_2_2_result)*(param//2)
return (node_2_1_result + node_2_2_result) * (param // 2)


class TestResourceExecutor(unittest.TestCase, ResourceExecutor):
Expand All @@ -57,46 +61,48 @@ def setUp(self):
node_3
"""
self.rootns = ChainMap({'param':4, 'inner': {}})
self.rootns = ChainMap({"param": 4, "inner": {}})

def nsspec(x, beginning=()):
ns = namespaces.resolve(self.rootns, beginning)
default_label = '_default' + str(x)
default_label = "_default" + str(x)
namespaces.push_nslevel(ns, default_label)
return beginning + (default_label,)

self.graph = DAG()

node_1_call = CallSpec(node_1, ('param',), 'node_1_result',
nsspec(node_1))
node_1_call = CallSpec(node_1, ("param",), "node_1_result", nsspec(node_1))

node_2_1_call = CallSpec(node_2_1, ('node_1_result',), 'node_2_1_result',
nsspec(node_2_1))

node_2_2_call = CallSpec(node_2_2, ('node_1_result',), 'node_2_2_result',
nsspec(node_2_2))

node_3_call = CallSpec(node_3, ('node_2_1_result','node_2_2_result','param'), 'node_3_result',
nsspec(node_3))
node_2_1_call = CallSpec(
node_2_1, ("node_1_result",), "node_2_1_result", nsspec(node_2_1)
)

node_2_2_call = CallSpec(
node_2_2, ("node_1_result",), "node_2_2_result", nsspec(node_2_2)
)

node_3_call = CallSpec(
node_3,
("node_2_1_result", "node_2_2_result", "param"),
"node_3_result",
nsspec(node_3),
)

self.graph.add_node(node_1_call)
self.graph.add_node(node_2_1_call, inputs={node_1_call})
self.graph.add_node(node_2_2_call, inputs={node_1_call})
self.graph.add_node(node_3_call, inputs={node_2_1_call, node_2_2_call})


def _test_ns(self, promise=False):
"""
Asserts that the namespace contains the expected results.
"""
node_3_result = 'node_1_result: 4'*10
node_3_result = "node_1_result: 4" * 10
namespace = self.rootns
if promise:
self.assertEqual(namespace['node_3_result'].result(), node_3_result)
self.assertEqual(namespace["node_3_result"].result(), node_3_result)
else:
self.assertEqual(namespace['node_3_result'], node_3_result)

self.assertEqual(namespace["node_3_result"], node_3_result)

def test_seq_execute(self):
"""
Expand All @@ -113,5 +119,6 @@ def test_parallel_execute(self):
self.execute_parallel()
self._test_ns(promise=True)

if __name__ =='__main__':

if __name__ == "__main__":
unittest.main()

0 comments on commit bff2455

Please sign in to comment.