def test_010_connect(self): sg0 = SG() sg1 = SG() n1 = SN(sg0) n2 = SN(sg0) n3 = SN(sg1) n1.connect(n2) self.assertRaises(ValueError, n1.connect, n3) self.assertRaises(ValueError, n1.connect, sg0) self.assertRaises(ValueError, n1.connect, None)
def test_000_create(self): sg0 = SG() sg1 = SG() n1 = SN(sg0) n2 = SN(sg0) n3 = SN(sg1) n4 = SN(sg1) self.assertIs(n1.sg,n2.sg) self.assertIsNot(n1.sg,n3.sg) self.assertIs(n3.sg,n4.sg)
def test_nodes_factory(self): sg = SG(SN) self.assertIsInstance(sg.add_node(), SN) class nSN(SN): pass sg = SG(nSN) self.assertIsInstance(sg.add_node(), nSN) self.assertIs(nSN, sg.nodes_factory) class nnSN(object): def __init__(self,sg): pass self.assertRaises(ValueError, SG, nnSN)
def test_020_nodes(self): sg = SG() nodes = [sg.add_node() for _ in xrange(10)] nn = sg.nodes self.assertEqual(len(nodes), len(nn)) for n in nodes: self.assertIn(n, nn)
def test_030_phy(self): sg0 = SG() n1 = SN(sg0) self.assertEqual(0.0, n1.phy) n1.phy = 1.2 self.assertEqual(1.2, n1.phy) self.assertRaises(ValueError, n1.__setattr__, "phy", "pippo")
def test_clean_contex(self): g = SG() for x in xrange(20): g.add_node()._context = x g.clean_context() for n in g.nodes: self.assertIsNone(n._context)
def test_025_disconnect(self): g = SG() n = SN(g) m = SN(g) n.connect(m) self.assertEqual(1,len(n.connected)) self.assertEqual(1,len(m.connected)) n.disconnect(m) self.assertEqual(0,len(n.connected)) self.assertEqual(0,len(m.connected)) """Already disconnected""" n.disconnect(m) """Assert ValueError""" g1 = SG() m = SN(g1) self.assertRaises(ValueError, n.disconnect, m) self.assertRaises(ValueError, n.disconnect, None)
def test_050__contex(self): """Try to set and get node _contex""" g = SG() n = g.add_node() self.assertIsNone(n._context) n._context = "paperino" self.assertEqual("paperino",n._context) n._context = None self.assertIsNone(n._context)
def test_030_remove_node(self): sg = SG() n = sg.add_node() self.assertEqual(1,len(sg.nodes)) sg.remove_node(n) self.assertEqual(0,len(sg.nodes)) n = sg.add_node() m = sg.add_node() r = sg.add_node() n.connect(m) n.connect(r) m.connect(r) sg.remove_node(n) self.assertEqual(2,len(sg.nodes)) self.assertEqual(1,len(m.connected)) self.assertEqual(1,len(r.connected)) self.assertIn(r,m.connected) self.assertRaises(ValueError, sg.remove_node, SG().add_node()) self.assertRaises(ValueError, sg.remove_node, None)
def test_xxx_dump_read_sg_dual(self): g = SG() N = [g.add_node(.1), g.add_node(.2), g.add_node(.3), g.add_node(.4) ] N[0].connect(N[1]) N[0].connect(N[2]) N[1].connect(N[3]) f = sio() g.dump(f) f.seek(0) self.assertSameGraph(g, SizeGraph.readsg(f)) print f.getvalue()
def test_get_connections(self): sg = SG() self.assertFalse(sg.get_connections()) nn = [sg.add_node() for _i in xrange(10)] self.assertFalse(sg.get_connections()) for n in nn: for m in nn: n.connect(m) connections = sg.get_connections() self.assertEqual(55, len(connections)) for n in nn: for m in nn: self.assertTrue((n,m) in connections or (m,n) in connections)
def test_read_sg_x00_from_file_real_file(self): p = tempfile.mktemp() f = file(p, "w") try: f.write("""1 MS 1.1 """) f.close() g = SG() g.add_node(1.1) self.assertSameGraph(g, SizeGraph.readsg(p)) finally: os.unlink(p)
def test_add_node(self): sg = SG() n = sg.add_node(1.3) self.assertEqual(1.3, n.phy) n2 = sg.add_node(n) self.assertIn(n, n2.connected) self.assertIn(n2, n.connected) n3 = sg.add_node(1.5,n2,n) self.assertEqual(1.5, n3.phy) self.assertIn(n3, n2.connected) self.assertIn(n3, n.connected) self.assertIn(n2, n3.connected) self.assertIn(n, n3.connected) self.assertRaises(ValueError, sg.add_node, None) self.assertRaises(ValueError, sg.add_node, "pippo") self.assertRaises(ValueError, sg.add_node, 1, "paperino") self.assertRaises(ValueError, sg.add_node, 1, n, "paperino")
def test_copy(self): """Copy graph""" g = SG() self.assertSameGraph(g, g.copy()) n = g.add_node(1.0) self.assertSameGraph(g, g.copy()) nn = [n] + [g.add_node(x) for x in xrange(3)] self.assertSameGraph(g, g.copy()) mm = [g.add_node(x) for x in xrange(20,24)] self.assertSameGraph(g, g.copy()) for n in nn: for m in mm: n.connect(m) self.assertSameGraph(g, g.copy()) for n in nn+mm: for m in nn+mm: n.connect(m) self.assertSameGraph(g, g.copy())
def test_xxx_dump_options(self): g = SG() N = [g.add_node(.1), g.add_node(.2), g.add_node(.3), g.add_node(.4) ] N[0].connect(N[1]) N[0].connect(N[2]) N[1].connect(N[3]) f = sio() g.dump(f,legacy=True) f.seek(0) print """LEGACY ######################## """ + f.getvalue() self.assertSameGraph(g, SizeGraph.readsg(f,ms=[.1,.2,.3,.4])) self.assertNotIn("#", f.getvalue()) self.assertNotIn("MS", f.getvalue()) f = sio() g.dump(f,comments=False) f.seek(0) print """COMMENTS ######################## """ + f.getvalue() self.assertSameGraph(g, SizeGraph.readsg(f)) self.assertNotIn("#", f.getvalue()) self.assertIn("MS", f.getvalue())
def test_020_connected(self): sg0 = SG() n1 = SN(sg0) n2 = SN(sg0) n3 = SN(sg0) n1.connect(n2) nn = n1.connected nnn = n2.connected self.assertIsNot(nn, n1._connected) self.assertEqual(1, len(nn)) self.assertEqual(1, len(nnn)) self.assertIn(n2, nn) self.assertIn(n1, nnn) n1.connect(n3) nn = n1.connected nnn = n2.connected nnnn = n3.connected self.assertEqual(2, len(nn)) self.assertEqual(1, len(nnn)) self.assertEqual(1, len(nnnn)) self.assertIn(n2, nn) self.assertIn(n3, nn) self.assertIn(n1, nnn) self.assertIn(n1, nnnn) """Duplicare NON cambia nulla""" n1.connect(n2) n1.connect(n3) nn = n1.connected nnn = n2.connected nnnn = n3.connected self.assertEqual(2, len(nn)) self.assertEqual(1, len(nnn)) self.assertEqual(1, len(nnnn)) self.assertIn(n2, nn) self.assertIn(n3, nn) self.assertIn(n1, nnn) self.assertIn(n1, nnnn)
def test_000_create(self): self.assertIsNotNone(SG())
def test_010_add_node(self): sg = SG() n = sg.add_node() self.assertIsNotNone(n) self.assertEqual(sg, n.sg)