d35e262d42d05ec3e1aa012ee82feb8286dc8d55
3 from __future__
import print_function
7 # failure to import any of the modules below indicates failed tests
8 # =================================================================
9 # modules used by diaspy
18 #### test suite configuration variables: can be adjusted to your liking
20 __pod__
= testconf
.__pod
__
21 __username__
= testconf
.__username
__
22 __passwd__
= testconf
.__passwd
__
27 test_count_file
= open('TEST_COUNT', 'r')
28 test_count
= int(test_count_file
.read())
29 test_count_file
.close()
30 except (IOError, ValueError):
34 test_count_file
= open('TEST_COUNT', 'w')
35 test_count_file
.write(str(test_count
))
36 test_count_file
.close()
37 print('Running test no. {0}'.format(test_count
))
39 print('Running tests on connection: "{0}:{1}@{2}"\t'.format(testconf
.__username
__, '*'*len(testconf
.__passwd
__), __pod__
), end
='')
40 test_connection
= diaspy
.connection
.Connection(pod
=__pod__
, username
=__username__
, password
=__passwd__
)
41 test_connection
.login()
42 print('[ CONNECTED ]\n')
45 print('Adding test aspects...\t', end
='')
46 diaspy
.streams
.Aspects(test_connection
).add(testconf
.test_aspect_name_fake
)
47 testconf
.test_aspect_id
= diaspy
.streams
.Aspects(test_connection
).add(testconf
.test_aspect_name
).id
50 print([i
['name'] for i
in test_connection
.getUserData()['aspects']])
53 post_text
= '#diaspy test no. {0}'.format(test_count
)
56 #######################################
57 #### TEST SUITE CODE ####
58 #######################################
59 class ConnectionTest(unittest
.TestCase
):
60 def testGettingUserInfo(self
):
61 info
= test_connection
.getUserData()
62 self
.assertEqual(dict, type(info
))
65 class MessagesTests(unittest
.TestCase
):
66 def testGettingMailbox(self
):
67 mailbox
= diaspy
.messages
.Mailbox(test_connection
)
69 for i
in range(len(mailbox
)):
70 self
.assertEqual(diaspy
.models
.Conversation
, type(mailbox
[i
]))
73 class AspectsTests(unittest
.TestCase
):
74 def testAspectsGettingID(self
):
75 aspects
= diaspy
.streams
.Aspects(test_connection
)
76 id = aspects
.getAspectID(testconf
.test_aspect_name
)
77 self
.assertEqual(testconf
.test_aspect_id
, id)
79 def testAspectsRemoveById(self
):
80 aspects
= diaspy
.streams
.Aspects(test_connection
)
81 for i
in test_connection
.getUserData()['aspects']:
82 if i
['name'] == testconf
.test_aspect_name
:
83 print(i
['id'], end
=' ')
84 aspects
.remove(id=i
['id'])
86 names
= [i
['name'] for i
in test_connection
.getUserData()['aspects']]
88 self
.assertNotIn(testconf
.test_aspect_name
, names
)
90 def testAspectsRemoveByName(self
):
91 aspects
= diaspy
.streams
.Aspects(test_connection
)
92 print(testconf
.test_aspect_name_fake
, end
=' ')
93 aspects
.remove(name
=testconf
.test_aspect_name_fake
)
94 names
= [i
['name'] for i
in test_connection
.getUserData()['aspects']]
96 self
.assertNotIn(testconf
.test_aspect_name_fake
, names
)
99 class StreamTest(unittest
.TestCase
):
100 def testGetting(self
):
101 stream
= diaspy
.streams
.Generic(test_connection
)
103 def testGettingLength(self
):
104 stream
= diaspy
.streams
.Generic(test_connection
)
107 def testClearing(self
):
108 stream
= diaspy
.streams
.Stream(test_connection
)
110 self
.assertEqual(0, len(stream
))
112 def testPurging(self
):
113 stream
= diaspy
.streams
.Stream(test_connection
)
114 post
= stream
.post('#diaspy test')
118 self
.assertNotIn(post
.id, [p
.id for p
in stream
])
120 def testPostingText(self
):
121 stream
= diaspy
.streams
.Stream(test_connection
)
122 post
= stream
.post(post_text
)
123 self
.assertEqual(diaspy
.models
.Post
, type(post
))
125 def testPostingImage(self
):
126 stream
= diaspy
.streams
.Stream(test_connection
)
128 stream
.post(text
=post_text
, photo
='test-image.png')
129 except (diaspy
.errors
.StreamError
) as e
:
134 def testingAddingTag(self
):
135 ft
= diaspy
.streams
.FollowedTags(test_connection
)
138 def testActivity(self
):
139 activity
= diaspy
.streams
.Activity(test_connection
)
141 def testMentionsStream(self
):
142 mentions
= diaspy
.streams
.Mentions(test_connection
)
145 class UserTests(unittest
.TestCase
):
146 def testHandleSeparatorRaisingExceptions(self
):
147 handles
= ['user.pod.example.com',
148 'user@podexamplecom',
150 'use r@pod.example.com',
151 'user0@pod300 example.com',
154 self
.assertRaises(Exception, diaspy
.people
.sephandle
, h
)
156 def testGettingUserByHandleData(self
):
157 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
, fetch
='data')
158 self
.assertEqual(testconf
.guid
, user
['guid'])
159 self
.assertEqual(testconf
.diaspora_id
, user
['handle'])
160 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
161 self
.assertEqual(type(user
.stream
), list)
162 self
.assertEqual(user
.stream
, [])
163 self
.assertIn('id', user
.data
)
164 self
.assertIn('avatar', user
.data
)
166 def testGettingUserByHandlePosts(self
):
167 user
= diaspy
.people
.User(test_connection
, handle
=testconf
.diaspora_id
)
168 self
.assertEqual(testconf
.guid
, user
['guid'])
169 self
.assertEqual(testconf
.diaspora_id
, user
['diaspora_id'])
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
)
175 def testGettingUserByGUID(self
):
176 user
= diaspy
.people
.User(test_connection
, guid
=testconf
.guid
)
177 self
.assertEqual(testconf
.diaspora_id
, user
['diaspora_id'])
178 self
.assertEqual(testconf
.diaspora_name
, user
['name'])
179 self
.assertIn('id', user
.data
)
180 self
.assertIn('avatar', user
.data
['profile'])
181 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
183 def testReprMethod(self
):
184 user
= diaspy
.people
.User(test_connection
, guid
=testconf
.guid
)
189 class ContactsTest(unittest
.TestCase
):
190 def testGetOnlySharing(self
):
191 contacts
= diaspy
.people
.Contacts(test_connection
)
192 result
= contacts
.get(set='only_sharing')
194 self
.assertEqual(diaspy
.people
.User
, type(i
))
196 def testGetAll(self
):
197 contacts
= diaspy
.people
.Contacts(test_connection
)
198 result
= contacts
.get(set='all')
200 self
.assertEqual(diaspy
.people
.User
, type(i
))
203 contacts
= diaspy
.people
.Contacts(test_connection
)
204 result
= contacts
.get()
206 self
.assertEqual(diaspy
.people
.User
, type(i
))
209 class PostTests(unittest
.TestCase
):
210 def testStringConversion(self
):
211 s
= diaspy
.streams
.Stream(test_connection
)
214 s
= diaspy
.streams
.Stream(test_connection
)
217 class NotificationsTests(unittest
.TestCase
):
218 def testMarkingRead(self
):
219 notifications
= diaspy
.notifications
.Notifications(test_connection
)
221 for n
in notifications
:
225 if notif
is not None:
228 warnings
.warn('test not sufficient: no unread notifications were found')
231 class SettingsTests(unittest
.TestCase
):
232 profile
= diaspy
.settings
.Profile(test_connection
)
233 account
= diaspy
.settings
.Account(test_connection
)
235 def testGettingName(self
):
236 self
.assertEqual(testconf
.user_names_tuple
, self
.profile
.getName())
238 def testGettingLocation(self
):
239 self
.assertEqual(testconf
.user_location_string
, self
.profile
.getLocation())
241 def testGettingGender(self
):
242 self
.assertEqual(testconf
.user_gender_string
, self
.profile
.getGender())
244 def testGettingBirthDate(self
):
245 self
.assertEqual(testconf
.user_date_of_birth
, self
.profile
.getBirthDate(named_month
=False))
246 self
.assertEqual(testconf
.user_date_of_birth_named
, self
.profile
.getBirthDate(named_month
=True))
248 def testGettingInfoIfProfileIsSearchable(self
):
249 self
.assertEqual(testconf
.user_is_searchable
, self
.profile
.isSearchable())
251 def testGettingInfoIfProfileIsNSFW(self
):
252 self
.assertEqual(testconf
.user_is_nsfw
, self
.profile
.isNSFW())
254 def testGettingTags(self
):
255 self
.assertEqual(sorted(testconf
.user_tags
), sorted(self
.profile
.getTags()))
257 def testGettingLanguages(self
):
258 self
.assertIn(('en', 'English'), self
.account
.getLanguages())
260 def testGettingEmail(self
):
261 self
.assertEqual(testconf
.user_email
, self
.account
.getEmail())
264 if __name__
== '__main__':
265 print('Hello World!')
266 print('It\'s testing time!')
269 print('Good! All tests passed!')