def test_mapper_with_pruning(self):
        """
        A test case to check that the mapper works even in the case of
        pruning.

        """
        # A mock RDF object
        t = CallableGeneratorTest.Temp()

        # Head node
        node = TransformationProxy(Node(None, None))

        # Set of operations to build the graph
        n1 = node.Define()
        n2 = node.Filter().Filter()
        n4 = n2.Count()
        n5 = n1.Count()
        n6 = node.Filter()  # noqa: avoid PEP8 F841

        # Reason for pruning (change of reference)
        n5 = n1.Filter()  # noqa: avoid PEP8 F841

        # Generate and execute the mapper
        generator = CallableGenerator(node.proxied_node)
        mapper_func = generator.get_callable()
        values = mapper_func(t)
        nodes = generator.get_action_nodes()

        reqd_order = [1, 2, 2, 2, 3, 2]

        self.assertEqual(t.ord_list, reqd_order)
        self.assertListEqual(nodes, [n4.proxied_node])
        self.assertListEqual(values, [t])
Beispiel #2
0
    def test_dfs_graph_with_pruning_actions(self):
        """
        Test case to check that action nodes with no user references get
        pruned.

        """
        # Head node
        node = TransformationProxy(Node(None, None))

        # Graph nodes
        n1 = node.Define()
        n2 = node.Filter()
        n3 = n2.Filter()
        n4 = n3.Count()  # noqa: avoid PEP8 F841
        n5 = n1.Count()
        n6 = node.Filter()  # noqa: avoid PEP8 F841

        # Action pruning, n5 was an action node earlier
        n5 = n1.Filter()  # noqa: avoid PEP8 F841

        obtained_order = DfsTest.traverse(node=node.get_head())

        reqd_order = [1, 2, 2, 2, 3, 2]

        self.assertEqual(obtained_order, reqd_order)
    def test_mapper_from_graph(self):
        """A simple test case to check the working of mapper."""
        # A mock RDF object
        t = CallableGeneratorTest.Temp()

        # Head node
        node = TransformationProxy(Node(None, None))
        # Set of operations to build the graph
        n1 = node.Define()
        n2 = node.Filter().Filter()
        n4 = n2.Count()
        n5 = n1.Count()
        n6 = node.Filter()  # noqa: avoid PEP8 F841

        # Generate and execute the mapper
        generator = CallableGenerator(node.proxied_node)
        mapper_func = generator.get_callable()
        values = mapper_func(t)
        nodes = generator.get_action_nodes()

        reqd_order = [1, 3, 2, 2, 3, 2]

        self.assertEqual(t.ord_list, reqd_order)
        self.assertListEqual(nodes, [n5.proxied_node, n4.proxied_node])
        self.assertListEqual(values, [t, t])
Beispiel #4
0
    def test_node_pickle(self):
        """
        Test cases to check that nodes can be accurately
        pickled and un-pickled.
        """
        import pickle

        # Node definitions
        node = TransformationProxy(Node(None, None))  # Head node
        n1 = node.Define("a", b="c")  # First child node
        n2 = n1.Count()  # noqa: avoid PEP8 F841
        n3 = node.Filter("b")
        n4 = n3.Count()  # noqa: avoid PEP8 F841

        # Pickled representation of nodes
        pickled_node = pickle.dumps(node.proxied_node)
        # n3 is of class TransformationProxy, so the proxied node must be
        # accessed before pickling.
        pickled_n3_node = pickle.dumps(n3.proxied_node)

        # Un-pickled node objects
        unpickled_node = pickle.loads(pickled_node)
        unpickled_n3_node = pickle.loads(pickled_n3_node)

        self.assertIsInstance(unpickled_node, type(node.proxied_node))
        self.assertIsInstance(unpickled_n3_node, type(n3.proxied_node))
        self.assertGraphs(node, unpickled_node)
        self.assertGraphs(n3.proxied_node, unpickled_n3_node)
Beispiel #5
0
    def test_dfs_graph_with_computed_values_pruning(self):
        """
        Test case to check that computed values in action nodes get
        pruned.

        """
        # Head node
        node = TransformationProxy(Node(None, None))

        # Graph nodes
        n1 = node.Define()
        n2 = node.Filter()
        n3 = n2.Filter()
        n4 = n3.Count()  # noqa: avoid PEP8 F841
        n5 = n1.Filter()
        n6 = n5.Count()
        n7 = node.Filter()

        # This is to make sure action nodes with
        # already computed values are pruned.
        n6.proxied_node.value = 1
        # This is to make sure that transformation
        # leaf nodes with value (possibly set intentionally)
        # don't get pruned.
        n7.value = 1  # noqa: avoid PEP8 F841

        obtained_order = DfsTest.traverse(node=node.get_head())

        # The node 'n6' will be pruned. Hence,
        # there's only one '3' in this list.
        reqd_order = [1, 2, 2, 2, 3, 2]

        self.assertEqual(obtained_order, reqd_order)
Beispiel #6
0
    def test_dfs_graph_with_parent_pruning(self):
        """
        Test case to check that parent nodes with no user references don't
        get pruned.

        """
        # Head node
        node = TransformationProxy(Node(None, None))

        # Graph nodes
        n1 = node.Define()
        n2 = node.Filter()
        n3 = n2.Filter()
        n4 = n3.Count()  # noqa: avoid PEP8 F841
        n5 = n1.Filter()  # noqa: avoid PEP8 F841
        n6 = node.Filter()  # noqa: avoid PEP8 F841

        # Remove references from n2 (which shouldn't affect the graph)
        n2 = None

        obtained_order = DfsTest.traverse(node=node.get_head())

        reqd_order = [1, 2, 2, 2, 3, 2]
        # Removing references from n2 will not prune any node
        # because n2 still has children

        self.assertEqual(obtained_order, reqd_order)
Beispiel #7
0
    def test_dfs_graph_without_pruning(self):
        """
        Test case to check that node pruning does not occur if every node either
        has children or some user references.

        """
        # Head node
        node = TransformationProxy(Node(None, None))

        # Graph nodes
        n1 = node.Define()
        n2 = node.Filter()
        n3 = n2.Filter()
        n4 = n3.Count()  # noqa: avoid PEP8 F841
        n5 = n1.Count()  # noqa: avoid PEP8 F841
        n6 = node.Filter()  # noqa: avoid PEP8 F841

        obtained_order = DfsTest.traverse(node=node.get_head())

        reqd_order = [1, 3, 2, 2, 3, 2]

        self.assertEqual(obtained_order, reqd_order)
Beispiel #8
0
    def test_get_state(self):
        """
        Test cases to check the working of __getstate__ method on
        Node class.

        """
        node = TransformationProxy(Node(None, None))  # Head node
        n1 = node.Define("a", b="c")  # First child node

        # Required dictionaries
        node_dict = {"children": [n1.proxied_node]}
        n1_dict = {
            'operation_name': "Define",
            'operation_args': ["a"],
            'operation_kwargs': {
                "b": "c"
            },
            'children': []
        }
        # nodes are of class TransformationProxy, so the proxied nodes must be
        # accessed in order to extract their dictionaries.
        self.assertDictEqual(node.proxied_node.__getstate__(), node_dict)
        self.assertDictEqual(n1.proxied_node.__getstate__(), n1_dict)
Beispiel #9
0
    def test_dfs_graph_with_recursive_pruning(self):
        """
        Test case to check that nodes in a PyRDF graph with no user references
        and no children get pruned recursively.
        """
        # Head node
        node = TransformationProxy(Node(None, None))

        # Graph nodes
        n1 = node.Define()
        n2 = node.Filter()
        n3 = n2.Filter()
        n4 = n3.Count()  # noqa: avoid PEP8 F841
        n5 = n1.Filter()  # noqa: avoid PEP8 F841
        n6 = node.Filter()  # noqa: avoid PEP8 F841

        # Remove references from n4 and it's parent nodes
        n4 = n3 = n2 = None  # noqa: avoid PEP8 F841

        obtained_order = DfsTest.traverse(node=node.get_head())

        reqd_order = [1, 2, 2]

        self.assertEqual(obtained_order, reqd_order)
Beispiel #10
0
 def test_transformation_proxy_return(self):
     """Node objects are returned for transformation nodes."""
     node = TransformationProxy(Node(None, None))
     newNode = node.Define(1)
     self.assertIsInstance(newNode, TransformationProxy)
     self.assertIsInstance(newNode.proxied_node, Node)
Beispiel #11
0
 def test_kwargs_read(self):
     """Named arguments are read accurately."""
     node = TransformationProxy(Node(None, None))
     newNode = node.Define(1, "b", a="1", b=2)
     self.assertEqual(newNode.operation.kwargs, {"a": "1", "b": 2})
Beispiel #12
0
 def test_args_read(self):
     """Arguments (unnamed) are read accurately."""
     node = TransformationProxy(Node(None, None))
     newNode = node.Define(1, "b", a="1", b=2)
     self.assertEqual(newNode.operation.args, [1, "b"])