Example #1
0
	def testListAttributes(self):
		"""
		Tests :meth:`foundations.nodes.AbstractNode.listAttributes` method.
		"""

		nodeA = AbstractNode("MyNodeA")
		self.assertListEqual(nodeA.listAttributes(), [])
		nodeB = AbstractNode("MyNodeB", attributeA=Attribute(), attributeB=Attribute())
		self.assertListEqual(sorted(nodeB.listAttributes()), ["attributeA", "attributeB"])
Example #2
0
	def testGetAttributes(self):
		"""
		Tests :meth:`foundations.nodes.AbstractNode.getAttributes` method.
		"""

		attributes = {"attributeA" : Attribute(), "attributeB" : Attribute()}

		nodeA = AbstractNode("MyNodeA", **attributes)
		for attribute in attributes.itervalues():
			self.assertIn(attribute, nodeA.getAttributes())
Example #3
0
	def testAddAttribute(self):
		"""
		Tests :meth:`foundations.nodes.AbstractNode.addAttribute` method.
		"""

		attributes = {"attributeA" : Attribute(), "attributeB" : Attribute()}

		nodeA = AbstractNode("MyNodeA")
		for attribute, value in attributes.iteritems():
			self.assertTrue(nodeA.addAttribute(attribute, value))
			self.assertTrue(nodeA.attributeExists(attribute))
Example #4
0
	def testHasAttribute(self):
		"""
		Tests :meth:`foundations.nodes.AbstractNode.attributeExists` method.
		"""

		attributes = {"attributeA" : Attribute(), "attributeB" : Attribute()}

		nodeA = AbstractNode("MyNodeA", **attributes)
		for attribute in attributes:
			self.assertTrue(nodeA.attributeExists(attribute))
		nodeB = AbstractNode("MyNodeB", nonAttribute="Non Attribute")
		self.assertFalse(nodeB.attributeExists("nonAttribute"))
Example #5
0
	def testAbstractNodePickle(self):
		"""
		Tests :class:`foundations.nodes.AbstractNode` class pickling.
		"""

		nodeA = AbstractNode("MyNodeA", attributeA=Attribute(), attributeB=Attribute())

		data = pickle.dumps(nodeA)
		data = pickle.loads(data)
		self.assertEqual(nodeA, data)

		data = pickle.dumps(nodeA, pickle.HIGHEST_PROTOCOL)
		data = pickle.loads(data)
		self.assertEqual(nodeA, data)
Example #6
0
    def testAbstractCompositeNodePickle(self):
        """
		This method tests :class:`foundations.nodes.AbstractCompositeNode` class pickling.
		
		:note: :data:`pickle.HIGHEST_PROTOCOL` must be used to pickle :class:`foundations.nodes.AbstractCompositeNode` class.
		"""

        nodeA = AbstractCompositeNode("MyNodeA",
                                      attributeA=Attribute(value="A"),
                                      attributeB=Attribute(value="1"))
        nodeB = AbstractCompositeNode("MyNodeB",
                                      nodeA,
                                      attributeA=Attribute(value="B"),
                                      attributeB=Attribute(value="2"))
        nodeC = AbstractCompositeNode("MyNodeC",
                                      nodeA,
                                      attributeA=Attribute(value="C"),
                                      attributeB=Attribute(value="3"))
        nodeD = AbstractCompositeNode("MyNodeD",
                                      nodeB,
                                      attributeA=Attribute(value="D"),
                                      attributeB=Attribute(value="4"))

        data = pickle.dumps(nodeA, pickle.HIGHEST_PROTOCOL)
        data = pickle.loads(data)
        self.assertEqual(nodeA, data)
Example #7
0
	def testSortChildrenNode(self):
		"""
		Tests :meth:`foundations.nodes.AbstractCompositeNode.sortChildren` method.
		"""

		nodeA = AbstractCompositeNode("MyNodeA", attributeA=Attribute(value="A"), attributeB=Attribute(value="1"))
		nodeC = AbstractCompositeNode("MyNodeC", nodeA, attributeA=Attribute(value="C"), attributeB=Attribute(value="3"))
		nodeB = AbstractCompositeNode("MyNodeB", nodeA, attributeA=Attribute(value="B"), attributeB=Attribute(value="2"))
		nodeG = AbstractCompositeNode("MyNodeG", nodeB, attributeA=Attribute(value="G"))
		nodeE = AbstractCompositeNode("MyNodeE", nodeB, attributeA=Attribute(value="E"), attributeB=Attribute(value="5"))
		nodeF = AbstractCompositeNode("MyNodeF", nodeB, attributeA=Attribute(value="F"), attributeB=Attribute(value="6"))
		nodeD = AbstractCompositeNode("MyNodeD", nodeB, attributeA=Attribute(value="D"), attributeB=Attribute(value="4"))

		self.assertTrue(nodeA.sortChildren())
		self.assertEqual(nodeA.children[0], nodeB)
		self.assertEqual(nodeA.children[1], nodeC)
		self.assertEqual(nodeA.children[0].children[0], nodeD)
		self.assertEqual(nodeA.children[0].children[1], nodeE)
		self.assertEqual(nodeA.children[0].children[2], nodeF)

		self.assertTrue(nodeA.sortChildren(reverseOrder=True))
		self.assertEqual(nodeA.children[0], nodeC)
		self.assertEqual(nodeA.children[1], nodeB)
		self.assertEqual(nodeA.children[1].children[0], nodeG)
		self.assertEqual(nodeA.children[1].children[1], nodeF)
		self.assertEqual(nodeA.children[1].children[2], nodeE)
		self.assertEqual(nodeA.children[1].children[3], nodeD)

		self.assertTrue(nodeA.sortChildren(attribute="attributeA"))
		self.assertEqual(nodeA.children[0], nodeB)
		self.assertEqual(nodeA.children[1], nodeC)
		self.assertEqual(nodeA.children[0].children[0], nodeD)
		self.assertEqual(nodeA.children[0].children[1], nodeE)
		self.assertEqual(nodeA.children[0].children[2], nodeF)

		self.assertTrue(nodeA.sortChildren(attribute="attributeA", reverseOrder=True))
		self.assertEqual(nodeA.children[0], nodeC)
		self.assertEqual(nodeA.children[1], nodeB)
		self.assertEqual(nodeA.children[1].children[0], nodeG)
		self.assertEqual(nodeA.children[1].children[1], nodeF)
		self.assertEqual(nodeA.children[1].children[2], nodeE)
		self.assertEqual(nodeA.children[1].children[3], nodeD)

		self.assertTrue(nodeA.sortChildren(attribute="attributeB"))
		self.assertEqual(nodeA.children[0], nodeB)
		self.assertEqual(nodeA.children[1], nodeC)
		self.assertEqual(nodeA.children[0].children[0], nodeD)
		self.assertEqual(nodeA.children[0].children[1], nodeE)
		self.assertEqual(nodeA.children[0].children[2], nodeF)
		self.assertEqual(nodeA.children[0].children[3], nodeG)

		self.assertTrue(nodeA.sortChildren(attribute="attributeB", reverseOrder=True))
		self.assertEqual(nodeA.children[0], nodeC)
		self.assertEqual(nodeA.children[1], nodeB)
		self.assertEqual(nodeA.children[1].children[0], nodeF)
		self.assertEqual(nodeA.children[1].children[1], nodeE)
		self.assertEqual(nodeA.children[1].children[2], nodeD)
		self.assertEqual(nodeA.children[1].children[3], nodeG)
Example #8
0
	def __setattr__(self, attribute, value):
		"""
		Reimplements the :meth:`foundations.nodes.Attribute.__setattr__` method.

		:param attribute: Attribute.
		:type attribute: object
		:param value: Value.
		:type value: object
		"""

		currentValue = getattr(self, attribute, None)

		Attribute.__setattr__(self, attribute, value)

		if not attribute in ("_Attribute__name",
							"_Attribute__value",
							"_Attribute__roles",
							"_Attribute__flags"):
			return

		triggerModel = getattr(self, "_Mixin_GraphModelObject__triggerModel", False)
		if triggerModel and value is not currentValue:
			self.attributeChanged()
Example #9
0
	def __init__(self, name=None, value=None, roles=None, flags=None, **kwargs):
		"""
		Initializes the class.

		:param name: Attribute name.
		:type name: unicode
		:param value: Attribute value.
		:type value: object
		:param roles: Roles.
		:type roles: dict
		:param flags: Flags.
		:type flags: int
		:param \*\*kwargs: Keywords arguments.
		:type \*\*kwargs: \*\*
		"""

		LOGGER.debug("> Initializing '{0}()' class.".format(self.__class__.__name__))

		Attribute.__init__(self, name, value, **kwargs)
		Mixin_GraphModelObject.__init__(self)

		# --- Setting class attributes. ---
		self.roles = roles or {Qt.DisplayRole : value, Qt.EditRole : value}
		self.flags = flags or int(Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsEnabled)