5 # failure to import any of the modules below indicates failed tests
6 # =================================================================
7 # modules used by diaspy
13 from diaspy
import client
as dclient
17 #### test suite configuration variables: can be adjusted to your liking
19 __pod__
= testconf
.__pod
__
20 __username__
= testconf
.__username
__
21 __passwd__
= testconf
.__passwd
__
26 test_count_file
= open('TEST_COUNT', 'r')
27 test_count
= int(test_count_file
.read())
28 test_count_file
.close()
29 except (IOError, ValueError):
33 test_count_file
= open('TEST_COUNT', 'w')
34 test_count_file
.write(str(test_count
))
35 test_count_file
.close()
36 print('Running test no. {0}'.format(test_count
))
38 print('Running tests on connection: "{0}:{1}@{2}"\t'.format(testconf
.__username
__, '*'*len(testconf
.__passwd
__), __pod__
), end
='')
39 test_connection
= diaspy
.connection
.Connection(pod
=__pod__
, username
=__username__
, password
=__passwd__
)
40 test_connection
.login()
41 print('[ CONNECTED ]\n')
43 post_text
= '#diaspy test no. {0}'.format(test_count
)
46 #######################################
47 #### TEST SUITE CODE ####
48 #######################################
49 class ConnectionTest(unittest
.TestCase
):
50 def testLoginWithoutUsername(self
):
51 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
52 self
.assertRaises(diaspy
.connection
.LoginError
, connection
.login
, password
='foo')
54 def testLoginWithoutPassword(self
):
55 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
56 self
.assertRaises(diaspy
.connection
.LoginError
, connection
.login
, username
='user')
58 def testGettingUserInfo(self
):
59 info
= test_connection
.getUserInfo()
60 self
.assertEqual(dict, type(info
))
63 class ClientTests(unittest
.TestCase
):
64 def testGettingTag(self
):
65 client
= dclient
.Client(test_connection
)
66 tag
= client
.get_tag('foo')
67 self
.assertEqual(diaspy
.streams
.Generic
, type(tag
))
68 if tag
: self
.assertEqual(diaspy
.models
.Post
, type(tag
[0]))
70 def testGettingMailbox(self
):
71 client
= dclient
.Client(test_connection
)
72 mailbox
= client
.get_mailbox()
73 self
.assertEqual(list, type(mailbox
))
74 self
.assertEqual(diaspy
.conversations
.Conversation
, type(mailbox
[0]))
77 class StreamTest(unittest
.TestCase
):
78 def testGetting(self
):
79 stream
= diaspy
.streams
.Generic(test_connection
)
81 def testGettingLength(self
):
82 stream
= diaspy
.streams
.Generic(test_connection
)
85 def testClearing(self
):
86 stream
= diaspy
.streams
.Stream(test_connection
)
88 self
.assertEqual(0, len(stream
))
90 def testPurging(self
):
91 stream
= diaspy
.streams
.Stream(test_connection
)
92 post
= stream
.post('#diaspy test')
96 self
.assertNotIn(post
.id, [p
.id for p
in stream
])
98 def testPostingText(self
):
99 stream
= diaspy
.streams
.Stream(test_connection
)
100 post
= stream
.post(post_text
)
101 self
.assertEqual(diaspy
.models
.Post
, type(post
))
103 def testPostingImage(self
):
104 stream
= diaspy
.streams
.Stream(test_connection
)
106 stream
.post(text
=post_text
, photo
='test-image.png')
107 except (diaspy
.errors
.StreamError
) as e
:
112 def testingAddingTag(self
):
113 ft
= diaspy
.streams
.FollowedTags(test_connection
)
116 def testAspectsAdd(self
):
117 aspects
= diaspy
.streams
.Aspects(test_connection
)
118 aspects
.add(testconf
.test_aspect_name_fake
)
119 testconf
.test_aspect_id
= aspects
.add(testconf
.test_aspect_name
).id
121 def testAspectsGettingID(self
):
122 aspects
= diaspy
.streams
.Aspects(test_connection
)
123 id = aspects
.getAspectID(testconf
.test_aspect_name
)
124 self
.assertEqual(testconf
.test_aspect_id
, id)
126 def testAspectsRemoveById(self
):
127 aspects
= diaspy
.streams
.Aspects(test_connection
)
128 aspects
.remove(testconf
.test_aspect_id
)
129 self
.assertEqual(-1, aspects
.getAspectID(testconf
.test_aspect_name
))
131 def testAspectsRemoveByName(self
):
132 aspects
= diaspy
.streams
.Aspects(test_connection
)
133 aspects
.remove(name
=testconf
.test_aspect_name_fake
)
134 self
.assertEqual(-1, aspects
.getAspectID(testconf
.test_aspect_name_fake
))
136 def testActivity(self
):
137 activity
= diaspy
.streams
.Activity(test_connection
)
139 def testMentionsStream(self
):
140 mentions
= diaspy
.streams
.Mentions(test_connection
)
143 class UserTests(unittest
.TestCase
):
144 def testHandleSeparatorRaisingExceptions(self
):
145 handles
= ['user.pod.example.com',
146 'user@podexamplecom',
148 'use r@pod.example.com',
149 'user0@pod300 example.com',
152 self
.assertRaises(Exception, diaspy
.people
.sephandle
, h
)
154 def testGettingUserByHandleData(self
):
155 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
, fetch
='data')
156 self
.assertEqual(testconf
.guid
, user
['guid'])
157 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
158 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
159 self
.assertEqual(type(user
.stream
), list)
160 self
.assertEqual(user
.stream
, [])
161 self
.assertIn('id', user
.data
)
162 self
.assertIn('avatar', user
.data
)
164 def testGettingUserByHandlePosts(self
):
165 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
)
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
.assertIn('id', user
.data
)
170 self
.assertIn('avatar', user
.data
)
171 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
173 def testGettingUserByGUID(self
):
174 user
= diaspy
.people
.User(test_connection
, guid
=testconf
.guid
)
175 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
176 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
177 self
.assertIn('id', user
.data
)
178 self
.assertIn('avatar', user
.data
)
179 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
182 class ContactsTest(unittest
.TestCase
):
183 def testGetOnlySharing(self
):
184 contacts
= diaspy
.people
.Contacts(test_connection
)
185 result
= contacts
.get(set='only_sharing')
187 self
.assertEqual(diaspy
.people
.User
, type(i
))
189 def testGetAll(self
):
190 contacts
= diaspy
.people
.Contacts(test_connection
)
191 result
= contacts
.get(set='all')
193 self
.assertEqual(diaspy
.people
.User
, type(i
))
196 contacts
= diaspy
.people
.Contacts(test_connection
)
197 result
= contacts
.get()
199 self
.assertEqual(diaspy
.people
.User
, type(i
))
202 class PostTests(unittest
.TestCase
):
203 def testStringConversion(self
):
204 s
= diaspy
.streams
.Stream(test_connection
)
207 s
= diaspy
.streams
.Stream(test_connection
)
210 class NotificationsTests(unittest
.TestCase
):
211 def testMarkgingRead(self
):
212 notifications
= diaspy
.notifications
.Notifications(test_connection
)
214 for n
in notifications
:
218 if notif
is not None:
221 warnings
.warn('test not sufficient: no unread notifications were found')
224 class SettingsTests(unittest
.TestCase
):
225 def testGettingLanguages(self
):
226 settings
= diaspy
.settings
.Settings(test_connection
)
227 self
.assertIn(('English', 'en'), settings
.getLanguages())
229 if __name__
== '__main__':
230 print('Hello World!')
231 print('It\'s testing time!')
234 print('It was testing time!')