class SubGraphs(unittest.TestCase): def setUp(self): os.chdir(os.path.dirname(__file__)) self.stage = Session().load_file(filepath="test_sub_graph.nxt") def test_basic_run(self): """Test that a sub graph is run. The node that is run calls a sub-graph that creates a file. If that file is there, we can assert the garph has run. """ expected_file = os.path.join(os.path.dirname(__file__), 'sub_graph_file.txt') # The file must not exist if we are going to believe the graph made it. if os.path.isfile(expected_file): os.remove(expected_file) self.assertFalse(os.path.isfile(expected_file)) self.stage.execute(start='/basic_file') self.assertTrue(os.path.isfile(expected_file)) # Clean up after the test os.remove(expected_file) def test_parameters(self): """Test that local attributes of a node that calls a sub graph are pushed to the sub graph world node, and that resulting attributes of the sub graph world node are pulled back into the calling node. """ target_path = '/specific_file' comp_layer = self.stage.build_stage() specific_node = comp_layer.lookup(target_path) expected_file = self.stage.get_node_attr_value(specific_node, 'location', comp_layer) # The file must not exist if we are going to believe the graph made it. if os.path.isfile(expected_file): os.remove(expected_file) self.assertFalse(os.path.isfile(expected_file)) self.stage.execute(start=target_path) self.assertTrue(os.path.isfile(expected_file)) # Clean up after the test os.remove(expected_file)
def spec_test(self): stage = Session().load_file(filepath=filename) r = LayerReturnTypes.Node for input_node in stage.top_layer.descendants(return_type=r): # Only concerned with test input nodes name = getattr(input_node, INTERNAL_ATTRS.NAME) if not name.startswith('test'): continue if not name.endswith('input'): continue input_path = stage.top_layer.get_node_path(input_node) rt_layer = stage.execute(input_path) # Gather expectations expected_resolve_path = input_path.replace('input', 'resolved') expected_resolve_node = stage.top_layer.lookup( expected_resolve_path) expected_cache_path = input_path.replace('input', 'cached') expected_cache_node = stage.top_layer.lookup(expected_cache_path) cached_node = rt_layer.lookup(input_path) # Assert computes are equal resolved_code = stage.get_node_code_string( input_node, layer=stage.top_layer, data_state=DATA_STATE.RESOLVED) expected_resolve_code = stage.get_node_code_string( expected_resolve_node, layer=stage.top_layer.lookup, data_state=DATA_STATE.RAW) self.assertEqual(expected_resolve_code, resolved_code) cached_code = getattr(cached_node, INTERNAL_ATTRS.CACHED_CODE) expected_cached_code = stage.get_node_code_string( expected_cache_node, layer=rt_layer, data_state=DATA_STATE.RAW) self.assertEqual(expected_cached_code, cached_code) # Assert attrs are equal for attr_name in stage.get_node_attr_names(input_node): resolved_attr_val = stage.get_node_attr_value(input_node, attr_name, stage.top_layer, resolved=True) excpected_resolve_attr_val = stage.get_node_attr_value( expected_resolve_node, attr_name, stage.top_layer, resolved=False) self.assertEqual(excpected_resolve_attr_val, resolved_attr_val) cached_attr_val = getattr(cached_node, attr_name) expected_cached_attr_val = stage.get_node_attr_value( expected_cache_node, attr_name, stage.top_layer) self.assertEqual(expected_cached_attr_val, cached_attr_val)