-
Notifications
You must be signed in to change notification settings - Fork 2
/
socialgraph_test.py
122 lines (98 loc) · 4.09 KB
/
socialgraph_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Unit tests for the socialgraph.py library'''
__author__ = 'colgur@gmail.com'
import unittest
import simplejson as json
import socialgraph as socg
class DlsIterTest(unittest.TestCase):
def testPopulate(self):
'''Test socialgraph creation'''
TESTFILE = "./testPopulate.json"
def lookup(graphurl, svids):
'''Test Lookup Generator'''
try:
sviditer = iter(svids)
except TypeError:
sviditer = iter([svids])
pass
with open(TESTFILE) as result:
for each_svid in sviditer:
vertex = socg.SocialVertex(each_svid)
vertex.relations = [socg.SocialVertex(each_relation_svid)
for each_relation_svid in json.load(result)]
yield vertex
sg = socg.SocialSubGraph(lookup)
sg.populate_friends(12345)
self.assertEqual(len(sg.vs), 6,
"Check that '%s' contains 5 entries" % TESTFILE)
sg.populate_friends([12345])
self.assertEqual(len(sg.vs), 6,
"No new Vertex additions")
self.assertEqual(len(sg.es), 5)
sg.populate_followers(12345)
self.assertEqual(len(sg.vs), 6,
"set(friends).difference(set(followers)) should be None")
sg.populate_followers([12345])
self.assertEqual(len(sg.vs), 6,
"set(friends).difference(set(followers)) should be None")
self.assertEqual(len(sg.es), 10)
def testDlsiter(self):
'''Test Depth-limited Search functionality'''
me = 12345
bob = 2911221
mary = 749863
friends_of_mine = [bob, mary]
friends_of_bob = [20536157,5676102]
friends_of_mary = [5676102,7190742]
postorder = []
postorder.extend(friends_of_bob)
postorder.extend([bob])
postorder.extend(friends_of_mary)
postorder.extend([mary])
postorder.extend([me])
def lookup(url, svids):
try:
sviditer = iter(svids)
except TypeError:
sviditer = iter([svids])
pass
for each_svid in sviditer:
vertex = socg.SocialVertex(each_svid)
relationship = friends_of_mine
if each_svid == bob:
relationship = friends_of_bob
elif each_svid == mary:
relationship = friends_of_mary
vertex.relations = [socg.SocialVertex(each_relation_svid)
for each_relation_svid in relationship]
yield vertex
sg = socg.SocialSubGraph(lookup)
sg.add_vertex(socg.SocialVertex(me))
my_svid_match = sg.vs.select(svid=me)
self.assertEqual(len(my_svid_match), 1,
"Social Vertex ID '%d' is not unique" % me)
my_ivertex = my_svid_match[0]
my_dlsiter = sg.dlsiter(my_ivertex, 2)
my_dlsiter_svids = [each_ivertex['svid'] for each_ivertex in my_dlsiter]
self.assertEqual(len(postorder), len(my_dlsiter_svids))
my_dlsiter_svids_iter = iter(my_dlsiter_svids)
for each_svid in postorder:
self.assertEqual(each_svid, my_dlsiter_svids_iter.next())
def suite():
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(DlsIterTest))
return suite
suite = suite()
unittest.TextTestRunner().run(suite)