5 # failure to import any of the modules below indicates failed tests
6 # =================================================================
7 # modules used by diaspy
16 #### test suite configuration variables: can be adjusted to your liking
18 __pod__
= testconf
.__pod
__
19 __username__
= testconf
.__username
__
20 __passwd__
= testconf
.__passwd
__
25 test_count_file
= open('TEST_COUNT', 'r')
26 test_count
= int(test_count_file
.read())
27 test_count_file
.close()
28 except (IOError, ValueError):
32 test_count_file
= open('TEST_COUNT', 'w')
33 test_count_file
.write(str(test_count
))
34 test_count_file
.close()
35 print('Running test no. {0}'.format(test_count
))
37 print('Running tests on connection: "{0}:{1}@{2}"\t'.format(testconf
.__username
__, '*'*len(testconf
.__passwd
__), __pod__
), end
='')
38 test_connection
= diaspy
.connection
.Connection(pod
=__pod__
, username
=__username__
, password
=__passwd__
)
39 test_connection
.login()
40 print('[ CONNECTED ]\n')
42 post_text
= '#diaspy test no. {0}'.format(test_count
)
45 #######################################
46 #### TEST SUITE CODE ####
47 #######################################
48 class ConnectionTest(unittest
.TestCase
):
49 def testLoginWithoutUsername(self
):
50 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
51 self
.assertRaises(diaspy
.connection
.LoginError
, connection
.login
, password
='foo')
53 def testLoginWithoutPassword(self
):
54 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
55 self
.assertRaises(diaspy
.connection
.LoginError
, connection
.login
, username
='user')
57 def testGettingUserInfo(self
):
58 info
= test_connection
.getUserInfo()
59 self
.assertEqual(dict, type(info
))
62 class ClientTests(unittest
.TestCase
):
63 def testGettingStream(self
):
64 client
= diaspy
.client
.Client(test_connection
)
65 stream
= client
.get_stream()
66 if len(stream
): self
.assertEqual(diaspy
.models
.Post
, type(stream
[0]))
68 def testGettingNotifications(self
):
69 client
= diaspy
.client
.Client(test_connection
)
70 notifications
= client
.get_notifications()
71 self
.assertEqual(diaspy
.notifications
.Notifications
, type(notifications
))
72 if notifications
: self
.assertEqual(diaspy
.models
.Notification
, type(notifications
[0]))
74 def testGettingTag(self
):
75 client
= diaspy
.client
.Client(test_connection
)
76 tag
= client
.get_tag('foo')
77 self
.assertEqual(diaspy
.streams
.Generic
, type(tag
))
78 if tag
: self
.assertEqual(diaspy
.models
.Post
, type(tag
[0]))
80 def testGettingMailbox(self
):
81 client
= diaspy
.client
.Client(test_connection
)
82 mailbox
= client
.get_mailbox()
83 self
.assertEqual(list, type(mailbox
))
84 self
.assertEqual(diaspy
.conversations
.Conversation
, type(mailbox
[0]))
87 class StreamTest(unittest
.TestCase
):
88 def testGetting(self
):
89 stream
= diaspy
.streams
.Generic(test_connection
)
91 def testGettingLength(self
):
92 stream
= diaspy
.streams
.Generic(test_connection
)
95 def testClearing(self
):
96 stream
= diaspy
.streams
.Stream(test_connection
)
98 self
.assertEqual(0, len(stream
))
100 def testPurging(self
):
101 stream
= diaspy
.streams
.Stream(test_connection
)
102 post
= stream
.post('#diaspy test')
106 self
.assertNotIn(post
.id, [p
.id for p
in stream
])
108 def testPostingText(self
):
109 stream
= diaspy
.streams
.Stream(test_connection
)
110 post
= stream
.post(post_text
)
111 self
.assertEqual(diaspy
.models
.Post
, type(post
))
113 def testPostingImage(self
):
114 stream
= diaspy
.streams
.Stream(test_connection
)
116 stream
.post(text
=post_text
, photo
='test-image.png')
117 except (StreamError
) as e
:
122 def testingAddingTag(self
):
123 ft
= diaspy
.streams
.FollowedTags(test_connection
)
126 def testAspectsAdd(self
):
127 aspects
= diaspy
.streams
.Aspects(test_connection
)
128 aspects
.add(testconf
.test_aspect_name_fake
)
129 testconf
.test_aspect_id
= aspects
.add(testconf
.test_aspect_name
).id
131 def testAspectsGettingID(self
):
132 aspects
= diaspy
.streams
.Aspects(test_connection
)
133 id = aspects
.getAspectID(testconf
.test_aspect_name
)
134 self
.assertEqual(testconf
.test_aspect_id
, id)
136 def testAspectsRemoveById(self
):
137 aspects
= diaspy
.streams
.Aspects(test_connection
)
138 aspects
.remove(testconf
.test_aspect_id
)
139 self
.assertEqual(-1, aspects
.getAspectID(testconf
.test_aspect_name
))
141 def testAspectsRemoveByName(self
):
142 aspects
= diaspy
.streams
.Aspects(test_connection
)
143 aspects
.remove(name
=testconf
.test_aspect_name_fake
)
144 self
.assertEqual(-1, aspects
.getAspectID(testconf
.test_aspect_name_fake
))
146 def testActivity(self
):
147 activity
= diaspy
.streams
.Activity(test_connection
)
149 def testMentionsStream(self
):
150 mentions
= diaspy
.streams
.Mentions(test_connection
)
153 class UserTests(unittest
.TestCase
):
154 def testHandleSeparatorRaisingExceptions(self
):
155 handles
= ['user.pod.example.com',
156 'user@podexamplecom',
158 'use r@pod.example.com',
159 'user0@pod300 example.com',
162 self
.assertRaises(Exception, diaspy
.people
.sephandle
, h
)
164 def testGettingUserByHandleData(self
):
165 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
, fetch
='data')
166 self
.assertEqual(testconf
.guid
, user
['guid'])
167 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
168 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
169 self
.assertEqual(type(user
.stream
), list)
170 self
.assertEqual(user
.stream
, [])
171 self
.assertIn('id', user
.data
)
172 self
.assertIn('avatar', user
.data
)
174 def testGettingUserByHandlePosts(self
):
175 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
)
176 self
.assertEqual(testconf
.guid
, user
['guid'])
177 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
178 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
179 self
.assertIn('id', user
.data
)
180 self
.assertIn('avatar', user
.data
)
181 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
183 def testGettingUserByGUID(self
):
184 user
= diaspy
.people
.User(test_connection
, guid
=testconf
.guid
)
185 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
186 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
187 self
.assertIn('id', user
.data
)
188 self
.assertIn('avatar', user
.data
)
189 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
192 class ContactsTest(unittest
.TestCase
):
193 def testGetOnlySharing(self
):
194 contacts
= diaspy
.people
.Contacts(test_connection
)
195 result
= contacts
.get(set='only_sharing')
197 self
.assertEqual(diaspy
.people
.User
, type(i
))
199 def testGetAll(self
):
200 contacts
= diaspy
.people
.Contacts(test_connection
)
201 result
= contacts
.get(set='all')
203 self
.assertEqual(diaspy
.people
.User
, type(i
))
206 contacts
= diaspy
.people
.Contacts(test_connection
)
207 result
= contacts
.get()
209 self
.assertEqual(diaspy
.people
.User
, type(i
))
212 class PostTests(unittest
.TestCase
):
213 def testStringConversion(self
):
214 s
= diaspy
.streams
.Stream(test_connection
)
217 s
= diaspy
.streams
.Stream(test_connection
)
220 class NotificationsTests(unittest
.TestCase
):
221 def testMarkgingRead(self
):
222 notifications
= diaspy
.notifications
.Notifications(test_connection
)
224 for n
in notifications
:
228 if notif
is not None:
231 warnings
.warn('test not sufficient: no unread notifications were found')
234 class SettingsTests(unittest
.TestCase
):
235 def testGettingLanguages(self
):
236 settings
= diaspy
.settings
.Settings(test_connection
)
237 self
.assertIn(('English', 'en'), settings
.getLanguages())
239 if __name__
== '__main__':
240 print('Hello World!')
241 print('It\'s testing time!')
244 print('It was testing time!')