You can now get list of languages in settings
[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 post_text = '#diaspy test no. {0}'.format(test_count)
43
44
45 #######################################
46 #### TEST SUITE CODE ####
47 #######################################
48 class ConnectionTest(unittest.TestCase):
49 def testLoginWithoutUsername(self):
50 connection = diaspy.connection.Connection(pod=__pod__)
51 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
52
53 def testLoginWithoutPassword(self):
54 connection = diaspy.connection.Connection(pod=__pod__)
55 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
56
57 def testGettingUserInfo(self):
58 info = test_connection.getUserInfo()
59 self.assertEqual(dict, type(info))
60
61
62 class ClientTests(unittest.TestCase):
63 def testGettingStream(self):
64 client = diaspy.client.Client(test_connection)
65 stream = client.get_stream()
66 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
67
68 def testGettingNotifications(self):
69 client = diaspy.client.Client(test_connection)
70 notifications = client.get_notifications()
71 self.assertEqual(diaspy.notifications.Notifications, type(notifications))
72 if notifications: self.assertEqual(diaspy.models.Notification, type(notifications[0]))
73
74 def testGettingTag(self):
75 client = diaspy.client.Client(test_connection)
76 tag = client.get_tag('foo')
77 self.assertEqual(diaspy.streams.Generic, type(tag))
78 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
79
80 def testGettingMailbox(self):
81 client = diaspy.client.Client(test_connection)
82 mailbox = client.get_mailbox()
83 self.assertEqual(list, type(mailbox))
84 self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
85
86
87 class StreamTest(unittest.TestCase):
88 def testGetting(self):
89 stream = diaspy.streams.Generic(test_connection)
90
91 def testGettingLength(self):
92 stream = diaspy.streams.Generic(test_connection)
93 len(stream)
94
95 def testClearing(self):
96 stream = diaspy.streams.Stream(test_connection)
97 stream.clear()
98 self.assertEqual(0, len(stream))
99
100 def testPurging(self):
101 stream = diaspy.streams.Stream(test_connection)
102 post = stream.post('#diaspy test')
103 stream.update()
104 post.delete()
105 stream.purge()
106 self.assertNotIn(post.id, [p.id for p in stream])
107
108 def testPostingText(self):
109 stream = diaspy.streams.Stream(test_connection)
110 post = stream.post(post_text)
111 self.assertEqual(diaspy.models.Post, type(post))
112
113 def testPostingImage(self):
114 stream = diaspy.streams.Stream(test_connection)
115 try:
116 stream.post(text=post_text, photo='test-image.png')
117 except (StreamError) as e:
118 warnings.warn('{0}')
119 finally:
120 pass
121
122 def testingAddingTag(self):
123 ft = diaspy.streams.FollowedTags(test_connection)
124 ft.add('test')
125
126 def testAspectsAdd(self):
127 aspects = diaspy.streams.Aspects(test_connection)
128 aspects.add(testconf.test_aspect_name_fake)
129 testconf.test_aspect_id = aspects.add(testconf.test_aspect_name).id
130
131 def testAspectsGettingID(self):
132 aspects = diaspy.streams.Aspects(test_connection)
133 id = aspects.getAspectID(testconf.test_aspect_name)
134 self.assertEqual(testconf.test_aspect_id, id)
135
136 def testAspectsRemoveById(self):
137 aspects = diaspy.streams.Aspects(test_connection)
138 aspects.remove(testconf.test_aspect_id)
139 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name))
140
141 def testAspectsRemoveByName(self):
142 aspects = diaspy.streams.Aspects(test_connection)
143 aspects.remove(name=testconf.test_aspect_name_fake)
144 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name_fake))
145
146 def testActivity(self):
147 activity = diaspy.streams.Activity(test_connection)
148
149 def testMentionsStream(self):
150 mentions = diaspy.streams.Mentions(test_connection)
151
152
153 class UserTests(unittest.TestCase):
154 def testHandleSeparatorRaisingExceptions(self):
155 handles = ['user.pod.example.com',
156 'user@podexamplecom',
157 '@pod.example.com',
158 'use r@pod.example.com',
159 'user0@pod300 example.com',
160 ]
161 for h in handles:
162 self.assertRaises(Exception, diaspy.people.sephandle, h)
163
164 def testGettingUserByHandleData(self):
165 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id, fetch='data')
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.assertEqual(type(user.stream), list)
170 self.assertEqual(user.stream, [])
171 self.assertIn('id', user.data)
172 self.assertIn('avatar', user.data)
173
174 def testGettingUserByHandlePosts(self):
175 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
176 self.assertEqual(testconf.guid, user['guid'])
177 self.assertEqual(testconf.diaspora_id, user['handle'])
178 self.assertEqual(testconf.diaspora_name, user['name'])
179 self.assertIn('id', user.data)
180 self.assertIn('avatar', user.data)
181 self.assertEqual(type(user.stream), diaspy.streams.Outer)
182
183 def testGettingUserByGUID(self):
184 user = diaspy.people.User(test_connection, guid=testconf.guid)
185 self.assertEqual(testconf.diaspora_id, user['handle'])
186 self.assertEqual(testconf.diaspora_name, user['name'])
187 self.assertIn('id', user.data)
188 self.assertIn('avatar', user.data)
189 self.assertEqual(type(user.stream), diaspy.streams.Outer)
190
191
192 class ContactsTest(unittest.TestCase):
193 def testGetOnlySharing(self):
194 contacts = diaspy.people.Contacts(test_connection)
195 result = contacts.get(set='only_sharing')
196 for i in result:
197 self.assertEqual(diaspy.people.User, type(i))
198
199 def testGetAll(self):
200 contacts = diaspy.people.Contacts(test_connection)
201 result = contacts.get(set='all')
202 for i in result:
203 self.assertEqual(diaspy.people.User, type(i))
204
205 def testGet(self):
206 contacts = diaspy.people.Contacts(test_connection)
207 result = contacts.get()
208 for i in result:
209 self.assertEqual(diaspy.people.User, type(i))
210
211
212 class PostTests(unittest.TestCase):
213 def testStringConversion(self):
214 s = diaspy.streams.Stream(test_connection)
215
216 def testRepr(self):
217 s = diaspy.streams.Stream(test_connection)
218
219
220 class NotificationsTests(unittest.TestCase):
221 def testMarkgingRead(self):
222 notifications = diaspy.notifications.Notifications(test_connection)
223 notif = None
224 for n in notifications:
225 if n.unread:
226 notif = n
227 break
228 if notif is not None:
229 n.mark(unread=False)
230 else:
231 warnings.warn('test not sufficient: no unread notifications were found')
232
233
234 class SettingsTests(unittest.TestCase):
235 def testGettingLanguages(self):
236 settings = diaspy.settings.Settings(test_connection)
237 self.assertIn(('English', 'en'), settings.getLanguages())
238
239 if __name__ == '__main__':
240 print('Hello World!')
241 print('It\'s testing time!')
242 n = unittest.main()
243 print(n)
244 print('It was testing time!')