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')
43 print('Adding test aspects...\t', end
='')
44 diaspy
.streams
.Aspects(test_connection
).add(testconf
.test_aspect_name_fake
)
45 testconf
.test_aspect_id
= diaspy
.streams
.Aspects(test_connection
).add(testconf
.test_aspect_name
).id
48 print([i
['name'] for i
in test_connection
.getUserData()['aspects']])
51 post_text
= '#diaspy test no. {0}'.format(test_count
)
54 #######################################
55 #### TEST SUITE CODE ####
56 #######################################
57 class ConnectionTest(unittest
.TestCase
):
58 def testGettingUserInfo(self
):
59 info
= test_connection
.getUserData()
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 AspectsTests(unittest
.TestCase
):
72 def testAspectsGettingID(self
):
73 aspects
= diaspy
.streams
.Aspects(test_connection
)
74 id = aspects
.getAspectID(testconf
.test_aspect_name
)
75 self
.assertEqual(testconf
.test_aspect_id
, id)
77 def testAspectsRemoveById(self
):
78 aspects
= diaspy
.streams
.Aspects(test_connection
)
79 for i
in test_connection
.getUserData()['aspects']:
80 if i
['name'] == testconf
.test_aspect_name
:
81 print(i
['id'], end
=' ')
82 aspects
.remove(id=i
['id'])
84 names
= [i
['name'] for i
in test_connection
.getUserData()['aspects']]
86 self
.assertNotIn(testconf
.test_aspect_name
, names
)
88 def testAspectsRemoveByName(self
):
89 aspects
= diaspy
.streams
.Aspects(test_connection
)
90 print(testconf
.test_aspect_name_fake
, end
=' ')
91 aspects
.remove(name
=testconf
.test_aspect_name_fake
)
92 names
= [i
['name'] for i
in test_connection
.getUserData()['aspects']]
94 self
.assertNotIn(testconf
.test_aspect_name_fake
, names
)
97 class StreamTest(unittest
.TestCase
):
98 def testGetting(self
):
99 stream
= diaspy
.streams
.Generic(test_connection
)
101 def testGettingLength(self
):
102 stream
= diaspy
.streams
.Generic(test_connection
)
105 def testClearing(self
):
106 stream
= diaspy
.streams
.Stream(test_connection
)
108 self
.assertEqual(0, len(stream
))
110 def testPurging(self
):
111 stream
= diaspy
.streams
.Stream(test_connection
)
112 post
= stream
.post('#diaspy test')
116 self
.assertNotIn(post
.id, [p
.id for p
in stream
])
118 def testPostingText(self
):
119 stream
= diaspy
.streams
.Stream(test_connection
)
120 post
= stream
.post(post_text
)
121 self
.assertEqual(diaspy
.models
.Post
, type(post
))
123 def testPostingImage(self
):
124 stream
= diaspy
.streams
.Stream(test_connection
)
126 stream
.post(text
=post_text
, photo
='test-image.png')
127 except (diaspy
.errors
.StreamError
) as e
:
132 def testingAddingTag(self
):
133 ft
= diaspy
.streams
.FollowedTags(test_connection
)
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 testMarkingRead(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 profile
= diaspy
.settings
.Profile(test_connection
)
226 account
= diaspy
.settings
.Account(test_connection
)
228 def testGettingName(self
):
229 self
.assertEqual(testconf
.user_names_tuple
, self
.profile
.getName())
231 def testGettingLocation(self
):
232 self
.assertEqual(testconf
.user_location_string
, self
.profile
.getLocation())
234 def testGettingGender(self
):
235 self
.assertEqual(testconf
.user_gender_string
, self
.profile
.getGender())
237 def testGettingBirthDate(self
):
238 self
.assertEqual(testconf
.user_date_of_birth
, self
.profile
.getBirthDate(named_month
=False))
239 self
.assertEqual(testconf
.user_date_of_birth_named
, self
.profile
.getBirthDate(named_month
=True))
241 def testGettingInfoIfProfileIsSearchable(self
):
242 self
.assertEqual(testconf
.user_is_searchable
, self
.profile
.isSearchable())
244 def testGettingInfoIfProfileIsNSFW(self
):
245 self
.assertEqual(testconf
.user_is_nsfw
, self
.profile
.isNSFW())
247 def testGettingTags(self
):
248 self
.assertEqual(sorted(testconf
.user_tags
), sorted(self
.profile
.getTags()))
250 def testGettingLanguages(self
):
251 self
.assertIn(('en', 'English'), self
.account
.getLanguages())
253 def testGettingEmail(self
):
254 self
.assertEqual(testconf
.user_email
, self
.account
.getEmail())
257 if __name__
== '__main__':
258 print('Hello World!')
259 print('It\'s testing time!')
262 print('Good! All tests passed!')