2eade928b6028f40e887098ed10edf696933b643
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')
44 print('Adding test aspects...\t', end
='')
45 diaspy
.streams
.Aspects(test_connection
).add(testconf
.test_aspect_name_fake
)
46 testconf
.test_aspect_id
= diaspy
.streams
.Aspects(test_connection
).add(testconf
.test_aspect_name
).id
49 print([i
['name'] for i
in test_connection
.getUserInfo()['aspects']])
52 post_text
= '#diaspy test no. {0}'.format(test_count
)
55 #######################################
56 #### TEST SUITE CODE ####
57 #######################################
58 class ConnectionTest(unittest
.TestCase
):
59 def testLoginWithoutUsername(self
):
60 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
61 self
.assertRaises(diaspy
.errors
.LoginError
, connection
.login
, password
='foo')
63 def testLoginWithoutPassword(self
):
64 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
65 self
.assertRaises(diaspy
.errors
.LoginError
, connection
.login
, username
='user')
67 def testGettingUserInfo(self
):
68 info
= test_connection
.getUserInfo()
69 self
.assertEqual(dict, type(info
))
72 class MessagesTests(unittest
.TestCase
):
73 def testGettingMailbox(self
):
74 mailbox
= diaspy
.messages
.Mailbox(test_connection
)
76 for i
in range(len(mailbox
)):
77 self
.assertEqual(diaspy
.models
.Conversation
, type(mailbox
[i
]))
80 class AspectsTests(unittest
.TestCase
):
81 def testAspectsGettingID(self
):
82 aspects
= diaspy
.streams
.Aspects(test_connection
)
83 id = aspects
.getAspectID(testconf
.test_aspect_name
)
84 self
.assertEqual(testconf
.test_aspect_id
, id)
86 def testAspectsRemoveById(self
):
87 aspects
= diaspy
.streams
.Aspects(test_connection
)
88 for i
in test_connection
.getUserInfo()['aspects']:
89 if i
['name'] == testconf
.test_aspect_name
:
90 print(i
['id'], end
=' ')
91 aspects
.remove(id=i
['id'])
93 names
= [i
['name'] for i
in test_connection
.getUserInfo()['aspects']]
95 self
.assertNotIn(testconf
.test_aspect_name
, names
)
97 def testAspectsRemoveByName(self
):
98 aspects
= diaspy
.streams
.Aspects(test_connection
)
99 print(testconf
.test_aspect_name_fake
, end
=' ')
100 aspects
.remove(name
=testconf
.test_aspect_name_fake
)
101 names
= [i
['name'] for i
in test_connection
.getUserInfo()['aspects']]
103 self
.assertNotIn(testconf
.test_aspect_name_fake
, names
)
106 class StreamTest(unittest
.TestCase
):
107 def testGetting(self
):
108 stream
= diaspy
.streams
.Generic(test_connection
)
110 def testGettingLength(self
):
111 stream
= diaspy
.streams
.Generic(test_connection
)
114 def testClearing(self
):
115 stream
= diaspy
.streams
.Stream(test_connection
)
117 self
.assertEqual(0, len(stream
))
119 def testPurging(self
):
120 stream
= diaspy
.streams
.Stream(test_connection
)
121 post
= stream
.post('#diaspy test')
125 self
.assertNotIn(post
.id, [p
.id for p
in stream
])
127 def testPostingText(self
):
128 stream
= diaspy
.streams
.Stream(test_connection
)
129 post
= stream
.post(post_text
)
130 self
.assertEqual(diaspy
.models
.Post
, type(post
))
132 def testPostingImage(self
):
133 stream
= diaspy
.streams
.Stream(test_connection
)
135 stream
.post(text
=post_text
, photo
='test-image.png')
136 except (diaspy
.errors
.StreamError
) as e
:
141 def testingAddingTag(self
):
142 ft
= diaspy
.streams
.FollowedTags(test_connection
)
145 def testActivity(self
):
146 activity
= diaspy
.streams
.Activity(test_connection
)
148 def testMentionsStream(self
):
149 mentions
= diaspy
.streams
.Mentions(test_connection
)
152 class UserTests(unittest
.TestCase
):
153 def testHandleSeparatorRaisingExceptions(self
):
154 handles
= ['user.pod.example.com',
155 'user@podexamplecom',
157 'use r@pod.example.com',
158 'user0@pod300 example.com',
161 self
.assertRaises(Exception, diaspy
.people
.sephandle
, h
)
163 def testGettingUserByHandleData(self
):
164 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
, fetch
='data')
165 self
.assertEqual(testconf
.guid
, user
['guid'])
166 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
167 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
168 self
.assertEqual(type(user
.stream
), list)
169 self
.assertEqual(user
.stream
, [])
170 self
.assertIn('id', user
.data
)
171 self
.assertIn('avatar', user
.data
)
173 def testGettingUserByHandlePosts(self
):
174 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
)
175 self
.assertEqual(testconf
.guid
, user
['guid'])
176 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
177 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
178 self
.assertIn('id', user
.data
)
179 self
.assertIn('avatar', user
.data
)
180 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
182 def testGettingUserByGUID(self
):
183 user
= diaspy
.people
.User(test_connection
, guid
=testconf
.guid
)
184 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
185 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
186 self
.assertIn('id', user
.data
)
187 self
.assertIn('avatar', user
.data
)
188 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
191 class ContactsTest(unittest
.TestCase
):
192 def testGetOnlySharing(self
):
193 contacts
= diaspy
.people
.Contacts(test_connection
)
194 result
= contacts
.get(set='only_sharing')
196 self
.assertEqual(diaspy
.people
.User
, type(i
))
198 def testGetAll(self
):
199 contacts
= diaspy
.people
.Contacts(test_connection
)
200 result
= contacts
.get(set='all')
202 self
.assertEqual(diaspy
.people
.User
, type(i
))
205 contacts
= diaspy
.people
.Contacts(test_connection
)
206 result
= contacts
.get()
208 self
.assertEqual(diaspy
.people
.User
, type(i
))
211 class PostTests(unittest
.TestCase
):
212 def testStringConversion(self
):
213 s
= diaspy
.streams
.Stream(test_connection
)
216 s
= diaspy
.streams
.Stream(test_connection
)
219 class NotificationsTests(unittest
.TestCase
):
220 def testMarkingRead(self
):
221 notifications
= diaspy
.notifications
.Notifications(test_connection
)
223 for n
in notifications
:
227 if notif
is not None:
230 warnings
.warn('test not sufficient: no unread notifications were found')
233 class SettingsTests(unittest
.TestCase
):
234 def testGettingLanguages(self
):
235 settings
= diaspy
.settings
.Settings(test_connection
)
236 self
.assertIn(('English', 'en'), settings
.getLanguages())
238 if __name__
== '__main__':
239 print('Hello World!')
240 print('It\'s testing time!')
243 print('It was testing time!')