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
.errors
.LoginError
, connection
.login
, password
='foo')
54 def testLoginWithoutPassword(self
):
55 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
56 self
.assertRaises(diaspy
.errors
.LoginError
, connection
.login
, username
='user')
58 def testGettingUserInfo(self
):
59 info
= test_connection
.getUserInfo()
60 self
.assertEqual(dict, type(info
))
63 class MessagesTests(unittest
.TestCase
):
64 def testGettingMailbox(self
):
65 mailbox
= diaspy
.messages
.Mailbox(test_connection
)
67 for i
in range(len(mailbox
)):
68 self
.assertEqual(diaspy
.models
.Conversation
, type(mailbox
[i
]))
71 class StreamTest(unittest
.TestCase
):
72 def testGetting(self
):
73 stream
= diaspy
.streams
.Generic(test_connection
)
75 def testGettingLength(self
):
76 stream
= diaspy
.streams
.Generic(test_connection
)
79 def testClearing(self
):
80 stream
= diaspy
.streams
.Stream(test_connection
)
82 self
.assertEqual(0, len(stream
))
84 def testPurging(self
):
85 stream
= diaspy
.streams
.Stream(test_connection
)
86 post
= stream
.post('#diaspy test')
90 self
.assertNotIn(post
.id, [p
.id for p
in stream
])
92 def testPostingText(self
):
93 stream
= diaspy
.streams
.Stream(test_connection
)
94 post
= stream
.post(post_text
)
95 self
.assertEqual(diaspy
.models
.Post
, type(post
))
97 def testPostingImage(self
):
98 stream
= diaspy
.streams
.Stream(test_connection
)
100 stream
.post(text
=post_text
, photo
='test-image.png')
101 except (diaspy
.errors
.StreamError
) as e
:
106 def testingAddingTag(self
):
107 ft
= diaspy
.streams
.FollowedTags(test_connection
)
110 def testAspectsAdd(self
):
111 aspects
= diaspy
.streams
.Aspects(test_connection
)
112 aspects
.add(testconf
.test_aspect_name_fake
)
113 testconf
.test_aspect_id
= aspects
.add(testconf
.test_aspect_name
).id
115 def testAspectsGettingID(self
):
116 aspects
= diaspy
.streams
.Aspects(test_connection
)
117 id = aspects
.getAspectID(testconf
.test_aspect_name
)
118 self
.assertEqual(testconf
.test_aspect_id
, id)
120 def testAspectsRemoveById(self
):
121 aspects
= diaspy
.streams
.Aspects(test_connection
)
122 aspects
.remove(testconf
.test_aspect_id
)
123 self
.assertEqual(-1, aspects
.getAspectID(testconf
.test_aspect_name
))
125 def testAspectsRemoveByName(self
):
126 aspects
= diaspy
.streams
.Aspects(test_connection
)
127 aspects
.remove(name
=testconf
.test_aspect_name_fake
)
128 self
.assertEqual(-1, aspects
.getAspectID(testconf
.test_aspect_name_fake
))
130 def testActivity(self
):
131 activity
= diaspy
.streams
.Activity(test_connection
)
133 def testMentionsStream(self
):
134 mentions
= diaspy
.streams
.Mentions(test_connection
)
137 class UserTests(unittest
.TestCase
):
138 def testHandleSeparatorRaisingExceptions(self
):
139 handles
= ['user.pod.example.com',
140 'user@podexamplecom',
142 'use r@pod.example.com',
143 'user0@pod300 example.com',
146 self
.assertRaises(Exception, diaspy
.people
.sephandle
, h
)
148 def testGettingUserByHandleData(self
):
149 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
, fetch
='data')
150 self
.assertEqual(testconf
.guid
, user
['guid'])
151 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
152 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
153 self
.assertEqual(type(user
.stream
), list)
154 self
.assertEqual(user
.stream
, [])
155 self
.assertIn('id', user
.data
)
156 self
.assertIn('avatar', user
.data
)
158 def testGettingUserByHandlePosts(self
):
159 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
)
160 self
.assertEqual(testconf
.guid
, user
['guid'])
161 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
162 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
163 self
.assertIn('id', user
.data
)
164 self
.assertIn('avatar', user
.data
)
165 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
167 def testGettingUserByGUID(self
):
168 user
= diaspy
.people
.User(test_connection
, guid
=testconf
.guid
)
169 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
170 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
171 self
.assertIn('id', user
.data
)
172 self
.assertIn('avatar', user
.data
)
173 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
176 class ContactsTest(unittest
.TestCase
):
177 def testGetOnlySharing(self
):
178 contacts
= diaspy
.people
.Contacts(test_connection
)
179 result
= contacts
.get(set='only_sharing')
181 self
.assertEqual(diaspy
.people
.User
, type(i
))
183 def testGetAll(self
):
184 contacts
= diaspy
.people
.Contacts(test_connection
)
185 result
= contacts
.get(set='all')
187 self
.assertEqual(diaspy
.people
.User
, type(i
))
190 contacts
= diaspy
.people
.Contacts(test_connection
)
191 result
= contacts
.get()
193 self
.assertEqual(diaspy
.people
.User
, type(i
))
196 class PostTests(unittest
.TestCase
):
197 def testStringConversion(self
):
198 s
= diaspy
.streams
.Stream(test_connection
)
201 s
= diaspy
.streams
.Stream(test_connection
)
204 class NotificationsTests(unittest
.TestCase
):
205 def testMarkgingRead(self
):
206 notifications
= diaspy
.notifications
.Notifications(test_connection
)
208 for n
in notifications
:
212 if notif
is not None:
215 warnings
.warn('test not sufficient: no unread notifications were found')
218 class SettingsTests(unittest
.TestCase
):
219 def testGettingLanguages(self
):
220 settings
= diaspy
.settings
.Settings(test_connection
)
221 self
.assertIn(('English', 'en'), settings
.getLanguages())
223 if __name__
== '__main__':
224 print('Hello World!')
225 print('It\'s testing time!')
228 print('It was testing time!')