Code fixes, new features and refactoring in the field of user data and
[diaspy.git] / tests.py
1 #!/usr/bin/env python3
2
3 import unittest
4
5 # failure to import any of the modules below indicates failed tests
6 # =================================================================
7 # modules used by diaspy
8 import re
9 import requests
10 import warnings
11 # actual diaspy code
12 import diaspy
13
14
15 #### SETUP STUFF
16 #### test suite configuration variables: can be adjusted to your liking
17 import testconf
18 __pod__ = testconf.__pod__
19 __username__ = testconf.__username__
20 __passwd__ = testconf.__passwd__
21
22
23 # Test counter
24 try:
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):
29 test_count = 0
30 finally:
31 test_count += 1
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))
36
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')
41
42 # Setup test aspects
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
46 print('OK')
47
48 print([i['name'] for i in test_connection.getUserData()['aspects']])
49
50
51 post_text = '#diaspy test no. {0}'.format(test_count)
52
53
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))
61
62
63 class MessagesTests(unittest.TestCase):
64 def testGettingMailbox(self):
65 mailbox = diaspy.messages.Mailbox(test_connection)
66 if mailbox:
67 for i in range(len(mailbox)):
68 self.assertEqual(diaspy.models.Conversation, type(mailbox[i]))
69
70
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)
76
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'])
83 break
84 names = [i['name'] for i in test_connection.getUserData()['aspects']]
85 print(names)
86 self.assertNotIn(testconf.test_aspect_name, names)
87
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']]
93 print(names)
94 self.assertNotIn(testconf.test_aspect_name_fake, names)
95
96
97 class StreamTest(unittest.TestCase):
98 def testGetting(self):
99 stream = diaspy.streams.Generic(test_connection)
100
101 def testGettingLength(self):
102 stream = diaspy.streams.Generic(test_connection)
103 len(stream)
104
105 def testClearing(self):
106 stream = diaspy.streams.Stream(test_connection)
107 stream.clear()
108 self.assertEqual(0, len(stream))
109
110 def testPurging(self):
111 stream = diaspy.streams.Stream(test_connection)
112 post = stream.post('#diaspy test')
113 stream.update()
114 post.delete()
115 stream.purge()
116 self.assertNotIn(post.id, [p.id for p in stream])
117
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))
122
123 def testPostingImage(self):
124 stream = diaspy.streams.Stream(test_connection)
125 try:
126 stream.post(text=post_text, photo='test-image.png')
127 except (diaspy.errors.StreamError) as e:
128 warnings.warn('{0}')
129 finally:
130 pass
131
132 def testingAddingTag(self):
133 ft = diaspy.streams.FollowedTags(test_connection)
134 ft.add('test')
135
136 def testActivity(self):
137 activity = diaspy.streams.Activity(test_connection)
138
139 def testMentionsStream(self):
140 mentions = diaspy.streams.Mentions(test_connection)
141
142
143 class UserTests(unittest.TestCase):
144 def testHandleSeparatorRaisingExceptions(self):
145 handles = ['user.pod.example.com',
146 'user@podexamplecom',
147 '@pod.example.com',
148 'use r@pod.example.com',
149 'user0@pod300 example.com',
150 ]
151 for h in handles:
152 self.assertRaises(Exception, diaspy.people.sephandle, h)
153
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)
163
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)
172
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)
180
181
182 class ContactsTest(unittest.TestCase):
183 def testGetOnlySharing(self):
184 contacts = diaspy.people.Contacts(test_connection)
185 result = contacts.get(set='only_sharing')
186 for i in result:
187 self.assertEqual(diaspy.people.User, type(i))
188
189 def testGetAll(self):
190 contacts = diaspy.people.Contacts(test_connection)
191 result = contacts.get(set='all')
192 for i in result:
193 self.assertEqual(diaspy.people.User, type(i))
194
195 def testGet(self):
196 contacts = diaspy.people.Contacts(test_connection)
197 result = contacts.get()
198 for i in result:
199 self.assertEqual(diaspy.people.User, type(i))
200
201
202 class PostTests(unittest.TestCase):
203 def testStringConversion(self):
204 s = diaspy.streams.Stream(test_connection)
205
206 def testRepr(self):
207 s = diaspy.streams.Stream(test_connection)
208
209
210 class NotificationsTests(unittest.TestCase):
211 def testMarkingRead(self):
212 notifications = diaspy.notifications.Notifications(test_connection)
213 notif = None
214 for n in notifications:
215 if n.unread:
216 notif = n
217 break
218 if notif is not None:
219 n.mark(unread=False)
220 else:
221 warnings.warn('test not sufficient: no unread notifications were found')
222
223
224 class SettingsTests(unittest.TestCase):
225 profile = diaspy.settings.Profile(test_connection)
226 account = diaspy.settings.Account(test_connection)
227
228 def testGettingName(self):
229 self.assertEqual(testconf.user_names_tuple, self.profile.getName())
230
231 def testGettingLocation(self):
232 self.assertEqual(testconf.user_location_string, self.profile.getLocation())
233
234 def testGettingGender(self):
235 self.assertEqual(testconf.user_gender_string, self.profile.getGender())
236
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))
240
241 def testGettingInfoIfProfileIsSearchable(self):
242 self.assertEqual(testconf.user_is_searchable, self.profile.isSearchable())
243
244 def testGettingInfoIfProfileIsNSFW(self):
245 self.assertEqual(testconf.user_is_nsfw, self.profile.isNSFW())
246
247 def testGettingTags(self):
248 self.assertEqual(sorted(testconf.user_tags), sorted(self.profile.getTags()))
249
250 def testGettingLanguages(self):
251 self.assertIn(('en', 'English'), self.account.getLanguages())
252
253 def testGettingEmail(self):
254 self.assertEqual(testconf.user_email, self.account.getEmail())
255
256
257 if __name__ == '__main__':
258 print('Hello World!')
259 print('It\'s testing time!')
260 n = unittest.main()
261 print(n)
262 print('Good! All tests passed!')