class NodeTests(unittest.TestCase): """ .. class:: NodeTests """ def setUp(self): """ test setup """ self.roAttrs = {"ro1": True, "ro2": "I'm read only"} self.rwAttrs = {"rw1": 0, "rw2": (1, 2, 3)} self.name = "BrightStart" self.node = Node(self.name, self.rwAttrs, self.roAttrs) def tearDown(self): """ clean up """ del self.roAttrs del self.rwAttrs del self.name del self.node def testNode(self): """ node rwAttrs roAttrs connect """ # # node name - th eon,y one prop you can't overwrite self.assertEqual(self.node.name, self.name) try: self.node.name = "can't do this" except AttributeError as error: self.assertEqual(str(error), "can't set attribute") try: self.node.makeProperty("name", "impossible") except AttributeError as error: self.assertEqual(str(error), "_name or name is already defined as a member") # # visited attr for walking self.assertEqual(hasattr(self.node, "visited"), True) self.assertEqual(self.node.visited, False) #pylint: disable=no-member # # ro attrs for k, v in self.roAttrs.items(): self.assertEqual(hasattr(self.node, k), True) self.assertEqual(getattr(self.node, k), v) try: setattr(self.node, k, "new value") except AttributeError as error: self.assertEqual(str(error), "can't set attribute") # # rw attrs for k, v in self.rwAttrs.items(): self.assertEqual(hasattr(self.node, k), True) self.assertEqual(getattr(self.node, k), v) setattr(self.node, k, "new value") self.assertEqual(getattr(self.node, k), "new value") # # connect toNode = Node("DeadEnd") edge = self.node.connect(toNode, {"foo": "boo"}, {"ro3": True}) self.assertEqual(isinstance(edge, Edge), True) self.assertEqual(edge.name, self.name + "-DeadEnd") self.assertEqual(self.node, edge.fromNode) #pylint: disable=no-member self.assertEqual(toNode, edge.toNode) #pylint: disable=no-member
def setUp(self): """ setup test case """ self.nodes = [Node("1"), Node("2"), Node("3")] self.edges = [ self.nodes[0].connect(self.nodes[1]), self.nodes[0].connect(self.nodes[2]) ] self.aloneNode = Node("4")
def testGraph(self): """ ctor nodes edges connect walk """ # # create graph gr = Graph("testGraph", self.nodes, self.edges) # # nodes and edges for node in self.nodes: self.assertEqual(node in gr, True) for edge in self.edges: self.assertEqual(edge in gr, True) self.assertEqual(sorted(self.nodes, key=lambda x: x.name), sorted(gr.nodes(), key=lambda x: x.name)) self.assertEqual(sorted(self.edges, key=lambda x: x.name), sorted(gr.edges(), key=lambda x: x.name)) # # getNode for node in self.nodes: self.assertEqual(gr.getNode(node.name), node) # # connect aloneEdge = gr.connect(self.nodes[0], self.aloneNode) self.assertEqual(self.aloneNode in gr, True) self.assertEqual(aloneEdge in gr, True) # # addNode anotherNode = Node("5") anotherEdge = anotherNode.connect(self.aloneNode) gr.addNode(anotherNode) self.assertEqual(anotherNode in gr, True) self.assertEqual(anotherEdge in gr, True) # # walk no nodeFcn ret = gr.walkAll() self.assertEqual(ret, {}) for node in gr.nodes(): self.assertEqual(node.visited, True) gr.reset() for node in gr.nodes(): self.assertEqual(node.visited, False) # # walk with nodeFcn def nbEdges(node): """ dummy node fcn """ return len(node.edges()) ret = gr.walkAll(nodeFcn=nbEdges) self.assertEqual(ret, {'1': 3, '2': 0, '3': 0, '4': 0, '5': 1})
def testGraph( self ): """ ctor nodes edges connect walk """ # # create graph gr = Graph( "testGraph", self.nodes, self.edges ) # # nodes and edges for node in self.nodes: self.assertEqual( node in gr, True ) for edge in self.edges: self.assertEqual( edge in gr, True ) self.assertEqual( sorted( self.nodes ), sorted( gr.nodes() ) ) self.assertEqual( sorted( self.edges ), sorted( gr.edges() ) ) # # getNode for node in self.nodes: self.assertEqual( gr.getNode( node.name ), node ) # # connect aloneEdge = gr.connect( self.nodes[0], self.aloneNode ) self.assertEqual( self.aloneNode in gr, True ) self.assertEqual( aloneEdge in gr, True ) # # addNode anotherNode = Node( "5" ) anotherEdge = anotherNode.connect( self.aloneNode ) gr.addNode( anotherNode ) self.assertEqual( anotherNode in gr, True ) self.assertEqual( anotherEdge in gr, True ) # # walk no nodeFcn ret = gr.walkAll() self.assertEqual( ret, {} ) for node in gr.nodes(): self.assertEqual( node.visited, True ) gr.reset() for node in gr.nodes(): self.assertEqual( node.visited, False ) # # walk with nodeFcn def nbEdges( node ): """ dummy node fcn """ return len( node.edges() ) ret = gr.walkAll( nodeFcn = nbEdges ) self.assertEqual( ret, { '1': 3, '2' : 0, '3': 0, '4' : 0, '5': 1 } )
def testNode(self): """ node rwAttrs roAttrs connect """ # # node name - th eon,y one prop you can't overwrite self.assertEqual(self.node.name, self.name) try: self.node.name = "can't do this" except AttributeError as error: self.assertEqual(str(error), "can't set attribute") try: self.node.makeProperty("name", "impossible") except AttributeError as error: self.assertEqual(str(error), "_name or name is already defined as a member") # # visited attr for walking self.assertEqual(hasattr(self.node, "visited"), True) self.assertEqual(self.node.visited, False) #pylint: disable=no-member # # ro attrs for k, v in self.roAttrs.items(): self.assertEqual(hasattr(self.node, k), True) self.assertEqual(getattr(self.node, k), v) try: setattr(self.node, k, "new value") except AttributeError as error: self.assertEqual(str(error), "can't set attribute") # # rw attrs for k, v in self.rwAttrs.items(): self.assertEqual(hasattr(self.node, k), True) self.assertEqual(getattr(self.node, k), v) setattr(self.node, k, "new value") self.assertEqual(getattr(self.node, k), "new value") # # connect toNode = Node("DeadEnd") edge = self.node.connect(toNode, {"foo": "boo"}, {"ro3": True}) self.assertEqual(isinstance(edge, Edge), True) self.assertEqual(edge.name, self.name + "-DeadEnd") self.assertEqual(self.node, edge.fromNode) #pylint: disable=no-member self.assertEqual(toNode, edge.toNode) #pylint: disable=no-member
def __init__( self, name, rwAttrs = None, roAttrs = None ): """ c'tor """ Node.__init__( self, name, rwAttrs, roAttrs )
def setUp(self): """ test setup """ self.roAttrs = {"ro1": True, "ro2": "I'm read only"} self.rwAttrs = {"rw1": 0, "rw2": (1, 2, 3)} self.name = "BrightStart" self.node = Node(self.name, self.rwAttrs, self.roAttrs)
def setUp(self): """ test setup """ self.fromNode = Node("Start") self.toNode = Node("End") self.roAttrs = {"ro1": True, "ro2": "I'm read only"} self.rwAttrs = {"rw1": 0, "rw2": (1, 2, 3)}
self.assertEqual( hasattr( self.node, k ), True ) self.assertEqual( getattr( self.node, k ), v ) try: setattr( self.node, k, "new value" ) except AttributeError, error: self.assertEqual( str(error), "can't set attribute" ) ## rw attrs for k, v in self.rwAttrs.items(): self.assertEqual( hasattr( self.node, k ), True ) self.assertEqual( getattr( self.node, k ), v ) setattr( self.node, k, "new value" ) self.assertEqual( getattr( self.node, k ), "new value" ) ## connect toNode = Node( "DeadEnd" ) edge = self.node.connect( toNode, { "foo" : "boo" }, { "ro3" : True } ) self.assertEqual( isinstance(edge, Edge), True ) self.assertEqual( edge.name, self.name + "-DeadEnd" ) self.assertEqual( self.node, edge.fromNode ) self.assertEqual( toNode, edge.toNode ) class EdgeTests( unittest.TestCase ): """ .. class:: EdgeTests """ def setUp( self ): """ test setup """ self.fromNode = Node( "Start" ) self.toNode = Node( "End" ) self.roAttrs = { "ro1" : True, "ro2" : "I'm read only" }