def test_attr_simple_action(self): """ActionProxy object reads the right input attribute.""" node = Node.Node(None, None) node.backend = None proxy = Proxy.ActionProxy(node) func = proxy.attr self.assertEqual(proxy._cur_attr, "attr") self.assertTrue(callable(func))
def test_type_return_action(self): """ ActionProxy object is of type `DistRDF.ActionProxy` and wraps a node object. """ node = Node.Node(None, None) node.backend = None proxy = Proxy.ActionProxy(node) self.assertIsInstance(proxy, Proxy.ActionProxy) self.assertIsInstance(proxy.proxied_node, Node.Node)
def test_undefined_attr_transformation(self): """ When a non-defined Node class attribute is called on a TransformationProxy object, it raises an AttributeError. """ node = Node.Node(None, None) node.backend = None proxy = Proxy.TransformationProxy(node) with self.assertRaises(AttributeError): proxy.attribute
def test_get_value_with_existing_value(self): """ Test case to check the working of 'GetValue' method in Proxy when the current action node already houses a value. """ node = Node.Node(None, None) node.backend = None proxy = Proxy.ActionProxy(node) node.value = 5 self.assertEqual(proxy.GetValue(), 5)
def test_return_value(self): """ Proxy object computes and returns the right output based on the function call. """ t = AttrReadTest.Temp() node = Node.Node(None, None) node.backend = None node.value = t proxy = Proxy.ActionProxy(node) self.assertEqual(proxy.val(21), 144)
def test_other_dunder_methods(self): """ Test cases to check the working of other dunder methods on Node class. """ node = Node.Node(None, None) # Regular dunder method must not throw an error node.__format__('') with self.assertRaises(AttributeError): node.__random__() # Unknown dunder method
def test_proxied_node_has_user_references(self): """ Check that the user reference holds until the proxy lives. When the Python garbage collector attempts to remove the proxy object, its `__del__` method switches the node attribute `has_user_references` from `True` to `False`. """ node = Node.Node(None, None) node.backend = None proxy = Proxy.TransformationProxy(node) self.assertTrue(node.has_user_references) proxy = None # noqa: avoid PEP8 F841 self.assertFalse(node.has_user_references)
def test_supported_transformation(self): """ TransformationProxy object reads the right input attributes, returning the methods of the proxied node. """ node = Node.Node(None, None) node.backend = AttrReadTest.TestBackend() proxy = Proxy.TransformationProxy(node) transformations = { "Define": ["x", "tdfentry_"], "Filter": ["tdfentry_ > 0"], } for transformation, args in transformations.items(): newProxy = getattr(proxy, transformation)(*args) self.assertEqual(proxy.proxied_node._new_op_name, transformation) self.assertIsInstance(newProxy, Proxy.TransformationProxy) self.assertEqual(newProxy.proxied_node.operation.name, transformation) self.assertEqual(newProxy.proxied_node.operation.args, args)
def test_set_state(self): """ Test cases to check the working of __setstate__ method on Node class. """ # Head node hn = create_dummy_headnode(1) hn.backend = TestBackend() node = Proxy.TransformationProxy(hn) nn1 = Node.Node(None, None) nn1.backend = TestBackend() n1 = Proxy.TransformationProxy(nn1) # State dictionaries node_dict = {"children": [n1]} n1_dict = { "operation_name": "Define", "operation_args": ["a"], "operation_kwargs": { "b": "c" }, "children": [] } # Set node objects with state dicts node.proxied_node.__setstate__(node_dict) n1.proxied_node.__setstate__(n1_dict) self.assertListEqual([node.operation, node.children], [None, node_dict["children"]]) self.assertListEqual([ n1.operation.name, n1.operation.args, n1.operation.kwargs, n1.children ], [ n1_dict["operation_name"], n1_dict["operation_args"], n1_dict["operation_kwargs"], n1_dict["children"] ])
def test_node_attr_transformation(self): """ When a node attribute is called on a TransformationProxy object, it correctly returns the attribute of the proxied node. """ node = Node.Node(None, None) node.backend = AttrReadTest.TestBackend() proxy = Proxy.TransformationProxy(node) node_attributes = [ "get_head", "operation", "children", "_new_op_name", "value", "pyroot_node", "has_user_references" ] for attr in node_attributes: self.assertEqual(getattr(proxy, attr), getattr(proxy.proxied_node, attr))