* `diaspy.people.User().handle()` now returns `diaspora_id` instead of `handle` wich...
[diaspy.git] / tests.py
... / ...
CommitLineData
1#!/usr/bin/env python3
2
3from __future__ import print_function
4
5import unittest
6
7# failure to import any of the modules below indicates failed tests
8# =================================================================
9# modules used by diaspy
10import re
11import requests
12import warnings
13# actual diaspy code
14import diaspy
15
16
17#### SETUP STUFF
18#### test suite configuration variables: can be adjusted to your liking
19import testconf
20__pod__ = testconf.__pod__
21__username__ = testconf.__username__
22__passwd__ = testconf.__passwd__
23
24
25# Test counter
26try:
27 test_count_file = open('TEST_COUNT', 'r')
28 test_count = int(test_count_file.read())
29 test_count_file.close()
30except (IOError, ValueError):
31 test_count = 0
32finally:
33 test_count += 1
34test_count_file = open('TEST_COUNT', 'w')
35test_count_file.write(str(test_count))
36test_count_file.close()
37print('Running test no. {0}'.format(test_count))
38
39print('Running tests on connection: "{0}:{1}@{2}"\t'.format(testconf.__username__, '*'*len(testconf.__passwd__), __pod__), end='')
40test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
41test_connection.login()
42print('[ CONNECTED ]\n')
43
44# Setup test aspects
45print('Adding test aspects...\t', end='')
46diaspy.streams.Aspects(test_connection).add(testconf.test_aspect_name_fake)
47testconf.test_aspect_id = diaspy.streams.Aspects(test_connection).add(testconf.test_aspect_name).id
48print('OK')
49
50print([i['name'] for i in test_connection.getUserData()['aspects']])
51
52
53post_text = '#diaspy test no. {0}'.format(test_count)
54
55
56#######################################
57#### TEST SUITE CODE ####
58#######################################
59class ConnectionTest(unittest.TestCase):
60 def testGettingUserInfo(self):
61 info = test_connection.getUserData()
62 self.assertEqual(dict, type(info))
63
64
65class MessagesTests(unittest.TestCase):
66 def testGettingMailbox(self):
67 mailbox = diaspy.messages.Mailbox(test_connection)
68 if mailbox:
69 for i in range(len(mailbox)):
70 self.assertEqual(diaspy.models.Conversation, type(mailbox[i]))
71
72
73class 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)
78
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'])
85 break
86 names = [i['name'] for i in test_connection.getUserData()['aspects']]
87 print(names)
88 self.assertNotIn(testconf.test_aspect_name, names)
89
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']]
95 print(names)
96 self.assertNotIn(testconf.test_aspect_name_fake, names)
97
98
99class StreamTest(unittest.TestCase):
100 def testGetting(self):
101 stream = diaspy.streams.Generic(test_connection)
102
103 def testGettingLength(self):
104 stream = diaspy.streams.Generic(test_connection)
105 len(stream)
106
107 def testClearing(self):
108 stream = diaspy.streams.Stream(test_connection)
109 stream.clear()
110 self.assertEqual(0, len(stream))
111
112 def testPurging(self):
113 stream = diaspy.streams.Stream(test_connection)
114 post = stream.post('#diaspy test')
115 stream.update()
116 post.delete()
117 stream.purge()
118 self.assertNotIn(post.id, [p.id for p in stream])
119
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))
124
125 def testPostingImage(self):
126 stream = diaspy.streams.Stream(test_connection)
127 try:
128 stream.post(text=post_text, photo='test-image.png')
129 except (diaspy.errors.StreamError) as e:
130 warnings.warn('{0}')
131 finally:
132 pass
133
134 def testingAddingTag(self):
135 ft = diaspy.streams.FollowedTags(test_connection)
136 ft.add('test')
137
138 def testActivity(self):
139 activity = diaspy.streams.Activity(test_connection)
140
141 def testMentionsStream(self):
142 mentions = diaspy.streams.Mentions(test_connection)
143
144
145class UserTests(unittest.TestCase):
146 def testHandleSeparatorRaisingExceptions(self):
147 handles = ['user.pod.example.com',
148 'user@podexamplecom',
149 '@pod.example.com',
150 'use r@pod.example.com',
151 'user0@pod300 example.com',
152 ]
153 for h in handles:
154 self.assertRaises(Exception, diaspy.people.sephandle, h)
155
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)
165
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)
174
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)
182
183 def testReprMethod(self):
184 user = diaspy.people.User(test_connection, guid=testconf.guid)
185 repr(user)
186 print(user)
187
188
189class ContactsTest(unittest.TestCase):
190 def testGetOnlySharing(self):
191 contacts = diaspy.people.Contacts(test_connection)
192 result = contacts.get(set='only_sharing')
193 for i in result:
194 self.assertEqual(diaspy.people.User, type(i))
195
196 def testGetAll(self):
197 contacts = diaspy.people.Contacts(test_connection)
198 result = contacts.get(set='all')
199 for i in result:
200 self.assertEqual(diaspy.people.User, type(i))
201
202 def testGet(self):
203 contacts = diaspy.people.Contacts(test_connection)
204 result = contacts.get()
205 for i in result:
206 self.assertEqual(diaspy.people.User, type(i))
207
208
209class PostTests(unittest.TestCase):
210 def testStringConversion(self):
211 s = diaspy.streams.Stream(test_connection)
212
213 def testRepr(self):
214 s = diaspy.streams.Stream(test_connection)
215
216
217class NotificationsTests(unittest.TestCase):
218 def testMarkingRead(self):
219 notifications = diaspy.notifications.Notifications(test_connection)
220 notif = None
221 for n in notifications:
222 if n.unread:
223 notif = n
224 break
225 if notif is not None:
226 n.mark(unread=False)
227 else:
228 warnings.warn('test not sufficient: no unread notifications were found')
229
230
231class SettingsTests(unittest.TestCase):
232 profile = diaspy.settings.Profile(test_connection)
233 account = diaspy.settings.Account(test_connection)
234
235 def testGettingName(self):
236 self.assertEqual(testconf.user_names_tuple, self.profile.getName())
237
238 def testGettingLocation(self):
239 self.assertEqual(testconf.user_location_string, self.profile.getLocation())
240
241 def testGettingGender(self):
242 self.assertEqual(testconf.user_gender_string, self.profile.getGender())
243
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))
247
248 def testGettingInfoIfProfileIsSearchable(self):
249 self.assertEqual(testconf.user_is_searchable, self.profile.isSearchable())
250
251 def testGettingInfoIfProfileIsNSFW(self):
252 self.assertEqual(testconf.user_is_nsfw, self.profile.isNSFW())
253
254 def testGettingTags(self):
255 self.assertEqual(sorted(testconf.user_tags), sorted(self.profile.getTags()))
256
257 def testGettingLanguages(self):
258 self.assertIn(('en', 'English'), self.account.getLanguages())
259
260 def testGettingEmail(self):
261 self.assertEqual(testconf.user_email, self.account.getEmail())
262
263
264if __name__ == '__main__':
265 print('Hello World!')
266 print('It\'s testing time!')
267 n = unittest.main()
268 print(n)
269 print('Good! All tests passed!')