Beispiel #1
0
 def test_file_token(self):
     base_path = __file__.replace(os.path.sep, '/')
     if base_path.endswith('.pyc'):
         base_path = base_path[:-1]
     my_dir = os.path.dirname(base_path)
     os.chdir(my_dir)
     test_stage = Session().load_file(filepath='FileTokenSub.nxt')
     comp_layer = test_stage.build_stage()
     fake_path = os.path.join(my_dir, 'notafile.txt')
     fake_path = fake_path.replace(os.path.sep, '/')
     real_path = os.path.join(my_dir, 'real_file.txt')
     real_path = real_path.replace(os.path.sep, '/')
     test_node = test_stage.top_layer.lookup('/tokens')
     file_fake = test_stage.get_node_attr_value(test_node,
                                                'file_fake',
                                                comp_layer,
                                                resolved=True)
     self.assertEqual('', file_fake)
     file_real = test_stage.get_node_attr_value(test_node,
                                                'file_real',
                                                comp_layer,
                                                resolved=True)
     self.assertEqual(real_path, file_real)
     path_fake = test_stage.get_node_attr_value(test_node,
                                                'path_fake',
                                                comp_layer,
                                                resolved=True)
     self.assertEqual(fake_path, path_fake)
     path_real = test_stage.get_node_attr_value(test_node,
                                                'path_real',
                                                test_stage.top_layer,
                                                resolved=True)
     self.assertEqual(real_path, path_real)
Beispiel #2
0
 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)
Beispiel #3
0
    def test_sibling_path(self):
        """Test that resolution of a filename only returns a file next to it.

        NOTE: this test depends on the test folder structure. It is using a
        test graph `TokenSiblingTest.nxt` that must be a direct sibling
        to this test file. The contents of that file are also depended on.
        If you find one of these files without the other, something is wrong.
        """
        base_path = __file__.replace(os.path.sep, '/')
        if base_path.endswith('.pyc'):
            base_path = base_path[:-1]
        base_name = os.path.basename(base_path)
        in_val = '${file::' + base_name + '}'
        expected = base_path
        test_attr = 'my_file'
        my_dir = os.path.dirname(base_path)
        os.chdir(my_dir)
        test_stage = Session().load_file(filepath='TokenSiblingTest.nxt')
        comp_layer = test_stage.build_stage()
        test_node = test_stage.top_layer.lookup('/tester')
        test_stage.add_node_attr(test_node, test_attr, {'value': in_val},
                                 test_stage.top_layer)
        result = test_stage.get_node_attr_value(test_node, test_attr,
                                                comp_layer)
        self.assertEqual(expected, result)
Beispiel #4
0
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)