2 Test some of the basics underpinning the graph system.
7 from testlib
import testutil
, PygrTestProgram
, SkipTest
8 from pygr
import mapping
, graphquery
, sqlgraph
10 class Query_Test(unittest
.TestCase
):
13 def dqcmp(self
, datagraph
, querygraph
, result
):
16 except AttributeError:
22 l
= [ d
.copy() for d
in graphquery
.GraphQuery(datagraph
, querygraph
) ]
23 assert len(l
) == len(result
), 'length mismatch'
26 for i
in range(len(l
)):
27 assert l
[i
] == result
[i
], 'incorrect result'
29 def test_basicquery_test(self
):
31 datagraph
= {0: {1: None, 2: None, 3: None},
32 1: {2: None}, 3: {4: None, 5: None},
33 4: {6: None}, 5: {6: None}, 2: {}, 6: {}}
34 querygraph
= {0: {1: None, 2: None, 3: None},
35 3:{4: None},1:{},2:{},4:{}}
36 result
= [{0: 0, 1: 1, 2: 2, 3: 3, 4: 4},
37 {0: 0, 1: 1, 2: 2, 3: 3, 4: 5},
38 {0: 0, 1: 2, 2: 1, 3: 3, 4: 4},
39 {0: 0, 1: 2, 2: 1, 3: 3, 4: 5}]
41 self
.dqcmp(datagraph
, querygraph
, result
)
43 def test_cyclicquery(self
):
44 "Cyclic QG against cyclic DG @CTB comment?"
45 datagraph
= { 1 :{2:None}, 2:{3:None}, 3:{4:None}, 4:{5:None},
47 querygraph
= {0:{1:None}, 1:{2:None}, 2:{4:None}, 3:{1:None},
49 result
= [ {0: 1, 1: 2, 2: 3, 3: 5, 4: 4} ]
50 self
.dqcmp(datagraph
, querygraph
, result
)
52 def test_cyclicacyclicquery(self
):
53 "Cyclic QG against acyclic DG"
54 datagraph
= {0:{1:None}, 1:{3:None}, 5:{3:None}, 4:{5:None},
55 2:{4:None,1:None}, 3:{}}
56 querygraph
= {0:{1:None}, 1:{3:None}, 3:{5:None}, 5:{4:None},
57 4:{2:None}, 2:{1:None}}
59 self
.dqcmp(datagraph
,querygraph
,result
)
61 def test_symmetricquery_test(self
):
62 "Symmetrical QG against symmetrical DG"
63 datagraph
= {1:{2:None},2:{3:None,4:None},5:{2:None},3:{},4:{}}
64 querygraph
= {0:{1:None},1:{2:None},2:{}}
65 result
= [{0: 1, 1: 2, 2: 3}, {0: 1, 1: 2, 2: 4},
66 {0: 5, 1: 2, 2: 3}, {0: 5, 1: 2, 2: 4}]
67 self
.dqcmp(datagraph
,querygraph
,result
)
69 def test_filteredquery(self
):
70 "Test a filter against a query"
71 datagraph
= {0: {1: None, 2: None, 3: None}, 1: {2: None, 3: None},
73 querygraph
= {0:{1:{'filter':lambda toNode
,**kw
:toNode
== 3}},1:{}}
74 result
= [{0: 0, 1: 3},{0: 1, 1: 3}]
75 self
.dqcmp(datagraph
,querygraph
,result
)
77 def test_headlessquery(self
):
78 "Test a query with no head nodes"
79 datagraph
= {0:{1:None},1:{2:None},2:{3:None},3:{4:None},4:{1:None}}
80 querygraph
= {0:{1:None},1:{2:None},2:{3:None},3:{0:None}}
81 result
= [{0: 1, 1: 2, 2: 3, 3: 4},
82 {0: 2, 1: 3, 2: 4, 3: 1},
83 {0: 3, 1: 4, 2: 1, 3: 2},
84 {0: 4, 1: 1, 2: 2, 3: 3}]
85 self
.dqcmp(datagraph
,querygraph
,result
)
87 class Mapping_Test(Query_Test
):
91 self
.datagraph
= mapping
.dictGraph()
93 def test_graphdict(self
):
95 datagraph
= self
.datagraph
98 results
= {1: {2: None}, 2: {}}
99 assert datagraph
== results
, 'incorrect result'
101 def test_nodedel(self
):
103 datagraph
= self
.datagraph
108 results
= {2: {3: None}, 3: {}}
109 assert datagraph
== results
, 'incorrect result'
111 def test_delraise(self
):
113 datagraph
= self
.datagraph
120 raise ValueError('failed to catch bad node deletion attempt')
122 pass # THIS IS THE CORRECT RESULT
124 def test_setitemraise(self
):
126 datagraph
= self
.datagraph
130 raise KeyError('failed to catch bad setitem attempt')
132 pass # THIS IS THE CORRECT RESULT
134 def test_graphedges(self
):
136 datagraph
= self
.datagraph
137 graphvals
= {1:{2:None},2:{3:None,4:None},5:{2:None},3:{},4:{}}
138 edge_list
= [[1, 2,None], [2, 3,None], [2, 4,None], [5, 2,None]]
141 for n
in graphvals
[i
].keys():
144 for e
in datagraph
.edges():
145 edge_results
.append(e
)
147 edge_results
= [list(t
) for t
in edge_results
]
149 #print 'edge_results:',edge_results
150 assert edge_results
== edge_list
, 'incorrect result'
152 class Graph_Test(Mapping_Test
):
153 "Run same tests on mapping.Graph class"
156 self
.datagraph
= mapping
.Graph()
158 class Graph_DB_Test(unittest
.TestCase
):
159 "test mapping.Graph with sourceDB, targetDB but no edgeDB"
163 def __init__(self
, id):
165 self
.nodes
= {1:Node(1), 2:Node(2)}
166 self
.datagraph
= mapping
.Graph(sourceDB
=self
.nodes
,
168 def test_no_edge_db(self
):
169 'test behavior with no edgeDB'
170 self
.datagraph
+= self
.nodes
[1] # add node
171 self
.datagraph
[self
.nodes
[1]][self
.nodes
[2]] = 3 # add edge
173 assert self
.datagraph
[self
.nodes
[1]][self
.nodes
[2]] == 3
176 class GraphShelve_Test(Mapping_Test
):
177 "Run same tests on mapping.Graph class"
181 tmp
= testutil
.TempDir('graphshelve-test')
182 filename
= tmp
.subfile() # needs a random name each time
183 self
.datagraph
= mapping
.Graph(filename
=filename
, intKeys
=True)
186 self
.datagraph
.close()
189 class SQLGraph_Test(Mapping_Test
):
190 "Runs the same tests on mapping.SQLGraph class"
191 dbname
= 'test.dumbo_foo_test'
194 if not testutil
.mysql_enabled():
195 raise SkipTest
, "no MySQL"
197 createOpts
= dict(source_id
='int', target_id
='int', edge_id
='int')
198 self
.datagraph
= sqlgraph
.SQLGraph(self
.dbname
, dropIfExists
=True,
199 createTable
=createOpts
)
202 self
.datagraph
.cursor
.execute('drop table if exists %s' % self
.dbname
)
204 class SQLiteGraph_Test(testutil
.SQLite_Mixin
, Mapping_Test
):
205 'run same tests on mapping.SQLGraph class using sqlite'
206 def sqlite_load(self
):
207 createOpts
= dict(source_id
='int', target_id
='int', edge_id
='int')
208 self
.datagraph
= sqlgraph
.SQLGraph('testgraph',
209 serverInfo
=self
.serverInfo
,
211 createTable
=createOpts
)
213 # test currently unused, requires access to leelab data
214 ## from pygr import worldbase
215 ## class Splicegraph_Test(unittest.TestCase):
218 ## self.sg = worldbase.Bio.Annotation.ASAP2.Isoform.HUMAN.\
219 ## hg17.splicegraph()
221 ## def exonskip_megatest(self):
222 ## 'perform exon skip query'
223 ## query = {0:{1:None,2:None},1:{2:None},2:{}}
224 ## gq = graphquery.GraphQuery(self.sg, query)
226 ## assert len(l) == 11546, 'test exact size of exonskip set'
228 if __name__
== '__main__':
229 PygrTestProgram(verbosity
=2)