1dbec1a7a0cfe2aba8084be24af14a3f94dd7b84
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 testGettingUserInfo(self
):
60 info
= test_connection
.getUserInfo()
61 self
.assertEqual(dict, type(info
))
64 class MessagesTests(unittest
.TestCase
):
65 def testGettingMailbox(self
):
66 mailbox
= diaspy
.messages
.Mailbox(test_connection
)
68 for i
in range(len(mailbox
)):
69 self
.assertEqual(diaspy
.models
.Conversation
, type(mailbox
[i
]))
72 class AspectsTests(unittest
.TestCase
):
73 def testAspectsGettingID(self
):
74 aspects
= diaspy
.streams
.Aspects(test_connection
)
75 id = aspects
.getAspectID(testconf
.test_aspect_name
)
76 self
.assertEqual(testconf
.test_aspect_id
, id)
78 def testAspectsRemoveById(self
):
79 aspects
= diaspy
.streams
.Aspects(test_connection
)
80 for i
in test_connection
.getUserInfo()['aspects']:
81 if i
['name'] == testconf
.test_aspect_name
:
82 print(i
['id'], end
=' ')
83 aspects
.remove(id=i
['id'])
85 names
= [i
['name'] for i
in test_connection
.getUserInfo()['aspects']]
87 self
.assertNotIn(testconf
.test_aspect_name
, names
)
89 def testAspectsRemoveByName(self
):
90 aspects
= diaspy
.streams
.Aspects(test_connection
)
91 print(testconf
.test_aspect_name_fake
, end
=' ')
92 aspects
.remove(name
=testconf
.test_aspect_name_fake
)
93 names
= [i
['name'] for i
in test_connection
.getUserInfo()['aspects']]
95 self
.assertNotIn(testconf
.test_aspect_name_fake
, names
)
98 class StreamTest(unittest
.TestCase
):
99 def testGetting(self
):
100 stream
= diaspy
.streams
.Generic(test_connection
)
102 def testGettingLength(self
):
103 stream
= diaspy
.streams
.Generic(test_connection
)
106 def testClearing(self
):
107 stream
= diaspy
.streams
.Stream(test_connection
)
109 self
.assertEqual(0, len(stream
))
111 def testPurging(self
):
112 stream
= diaspy
.streams
.Stream(test_connection
)
113 post
= stream
.post('#diaspy test')
117 self
.assertNotIn(post
.id, [p
.id for p
in stream
])
119 def testPostingText(self
):
120 stream
= diaspy
.streams
.Stream(test_connection
)
121 post
= stream
.post(post_text
)
122 self
.assertEqual(diaspy
.models
.Post
, type(post
))
124 def testPostingImage(self
):
125 stream
= diaspy
.streams
.Stream(test_connection
)
127 stream
.post(text
=post_text
, photo
='test-image.png')
128 except (diaspy
.errors
.StreamError
) as e
:
133 def testingAddingTag(self
):
134 ft
= diaspy
.streams
.FollowedTags(test_connection
)
137 def testActivity(self
):
138 activity
= diaspy
.streams
.Activity(test_connection
)
140 def testMentionsStream(self
):
141 mentions
= diaspy
.streams
.Mentions(test_connection
)
144 class UserTests(unittest
.TestCase
):
145 def testHandleSeparatorRaisingExceptions(self
):
146 handles
= ['user.pod.example.com',
147 'user@podexamplecom',
149 'use r@pod.example.com',
150 'user0@pod300 example.com',
153 self
.assertRaises(Exception, diaspy
.people
.sephandle
, h
)
155 def testGettingUserByHandleData(self
):
156 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
, fetch
='data')
157 self
.assertEqual(testconf
.guid
, user
['guid'])
158 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
159 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
160 self
.assertEqual(type(user
.stream
), list)
161 self
.assertEqual(user
.stream
, [])
162 self
.assertIn('id', user
.data
)
163 self
.assertIn('avatar', user
.data
)
165 def testGettingUserByHandlePosts(self
):
166 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
)
167 self
.assertEqual(testconf
.guid
, user
['guid'])
168 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
169 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
170 self
.assertIn('id', user
.data
)
171 self
.assertIn('avatar', user
.data
)
172 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
174 def testGettingUserByGUID(self
):
175 user
= diaspy
.people
.User(test_connection
, guid
=testconf
.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
)
183 class ContactsTest(unittest
.TestCase
):
184 def testGetOnlySharing(self
):
185 contacts
= diaspy
.people
.Contacts(test_connection
)
186 result
= contacts
.get(set='only_sharing')
188 self
.assertEqual(diaspy
.people
.User
, type(i
))
190 def testGetAll(self
):
191 contacts
= diaspy
.people
.Contacts(test_connection
)
192 result
= contacts
.get(set='all')
194 self
.assertEqual(diaspy
.people
.User
, type(i
))
197 contacts
= diaspy
.people
.Contacts(test_connection
)
198 result
= contacts
.get()
200 self
.assertEqual(diaspy
.people
.User
, type(i
))
203 class PostTests(unittest
.TestCase
):
204 def testStringConversion(self
):
205 s
= diaspy
.streams
.Stream(test_connection
)
208 s
= diaspy
.streams
.Stream(test_connection
)
211 class NotificationsTests(unittest
.TestCase
):
212 def testMarkingRead(self
):
213 notifications
= diaspy
.notifications
.Notifications(test_connection
)
215 for n
in notifications
:
219 if notif
is not None:
222 warnings
.warn('test not sufficient: no unread notifications were found')
225 class SettingsTests(unittest
.TestCase
):
226 profile
= diaspy
.settings
.Profile(test_connection
)
227 account
= diaspy
.settings
.Account(test_connection
)
229 def testGettingName(self
):
230 self
.assertEqual(testconf
.user_names_tuple
, self
.profile
.getName())
232 def testGettingLocation(self
):
233 self
.assertEqual(testconf
.user_location_string
, self
.profile
.getLocation())
235 def testGettingGender(self
):
236 self
.assertEqual(testconf
.user_gender_string
, self
.profile
.getGender())
238 def testGettingBirthDate(self
):
239 self
.assertEqual(testconf
.user_date_of_birth
, self
.profile
.getBirthDate(named_month
=False))
240 self
.assertEqual(testconf
.user_date_of_birth_named
, self
.profile
.getBirthDate(named_month
=True))
242 def testGettingInfoIfProfileIsSearchable(self
):
243 self
.assertEqual(testconf
.user_is_searchable
, self
.profile
.isSearchable())
245 def testGettingInfoIfProfileIsNSFW(self
):
246 self
.assertEqual(testconf
.user_is_nsfw
, self
.profile
.isNSFW())
248 def testGettingTags(self
):
249 self
.assertEqual(sorted(testconf
.user_tags
), sorted(self
.profile
.getTags()))
251 def testGettingLanguages(self
):
252 self
.assertIn(('English', 'en'), self
.account
.getLanguages())
255 if __name__
== '__main__':
256 print('Hello World!')
257 print('It\'s testing time!')
260 print('It was testing time!')