Set version in setup.py to current version.
[diaspy.git] / tests.py
CommitLineData
94c2d637
MM
1#!/usr/bin/env python3
2
e2c48b2f
MM
3from __future__ import print_function
4
94c2d637
MM
5import unittest
6
313c9233 7# failure to import any of the modules below indicates failed tests
2ec93347 8# =================================================================
94c2d637 9# modules used by diaspy
88ec6cda 10import re
2ec93347
MM
11import requests
12import warnings
94c2d637
MM
13# actual diaspy code
14import diaspy
15
16
62f1912f 17#### SETUP STUFF
94c2d637 18#### test suite configuration variables: can be adjusted to your liking
fab8b736
MM
19import testconf
20__pod__ = testconf.__pod__
21__username__ = testconf.__username__
22__passwd__ = testconf.__passwd__
94c2d637 23
505fc964 24
62f1912f
MM
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))
94c2d637 38
6d8d47ce 39print('Running tests on connection: "{0}:{1}@{2}"\t'.format(testconf.__username__, '*'*len(testconf.__passwd__), __pod__), end='')
62f1912f
MM
40test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
41test_connection.login()
c1490af7 42print('[ CONNECTED ]\n')
62f1912f 43
73a9e0d3
MM
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
39af9756 50print([i['name'] for i in test_connection.getUserData()['aspects']])
73a9e0d3
MM
51
52
66c3bb76
MM
53post_text = '#diaspy test no. {0}'.format(test_count)
54
62f1912f 55
a8fdc14a
MM
56#######################################
57#### TEST SUITE CODE ####
58#######################################
385e7ebe 59class ConnectionTest(unittest.TestCase):
264336e2 60 def testGettingUserInfo(self):
39af9756 61 info = test_connection.getUserData()
264336e2
MM
62 self.assertEqual(dict, type(info))
63
7a31b4aa 64
4882952f 65class MessagesTests(unittest.TestCase):
fab8b736 66 def testGettingMailbox(self):
4882952f
MM
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]))
fab8b736 71
f2eaa3c7 72
73a9e0d3
MM
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)
39af9756 81 for i in test_connection.getUserData()['aspects']:
73a9e0d3
MM
82 if i['name'] == testconf.test_aspect_name:
83 print(i['id'], end=' ')
84 aspects.remove(id=i['id'])
85 break
39af9756 86 names = [i['name'] for i in test_connection.getUserData()['aspects']]
73a9e0d3
MM
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)
39af9756 94 names = [i['name'] for i in test_connection.getUserData()['aspects']]
73a9e0d3
MM
95 print(names)
96 self.assertNotIn(testconf.test_aspect_name_fake, names)
97
98
f3eaa601
MM
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()
9aa1c960 118 self.assertNotIn(post.id, [p.id for p in stream])
f3eaa601
MM
119
120 def testPostingText(self):
121 stream = diaspy.streams.Stream(test_connection)
a98c6792 122 post = stream.post(post_text)
f3eaa601 123 self.assertEqual(diaspy.models.Post, type(post))
27f09973 124
f3eaa601
MM
125 def testPostingImage(self):
126 stream = diaspy.streams.Stream(test_connection)
2d4f6eeb
MM
127 try:
128 stream.post(text=post_text, photo='test-image.png')
1cff2093 129 except (diaspy.errors.StreamError) as e:
2d4f6eeb
MM
130 warnings.warn('{0}')
131 finally:
132 pass
f3eaa601
MM
133
134 def testingAddingTag(self):
135 ft = diaspy.streams.FollowedTags(test_connection)
136 ft.add('test')
137
27a28aaf
MM
138 def testActivity(self):
139 activity = diaspy.streams.Activity(test_connection)
140
141 def testMentionsStream(self):
142 mentions = diaspy.streams.Mentions(test_connection)
143
f3eaa601
MM
144
145class UserTests(unittest.TestCase):
146 def testHandleSeparatorRaisingExceptions(self):
f3eaa601
MM
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:
6d8d47ce 154 self.assertRaises(Exception, diaspy.people.sephandle, h)
141216df 155
6d8d47ce
MM
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):
9aa1c960 167 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
141216df 168 self.assertEqual(testconf.guid, user['guid'])
6d8d47ce
MM
169 self.assertEqual(testconf.diaspora_id, user['handle'])
170 self.assertEqual(testconf.diaspora_name, user['name'])
141216df 171 self.assertIn('id', user.data)
6d8d47ce 172 self.assertIn('avatar', user.data)
141216df 173 self.assertEqual(type(user.stream), diaspy.streams.Outer)
6d8d47ce 174
141216df 175 def testGettingUserByGUID(self):
9aa1c960 176 user = diaspy.people.User(test_connection, guid=testconf.guid)
6d8d47ce
MM
177 self.assertEqual(testconf.diaspora_id, user['handle'])
178 self.assertEqual(testconf.diaspora_name, user['name'])
141216df 179 self.assertIn('id', user.data)
6d8d47ce 180 self.assertIn('avatar', user.data)
141216df 181 self.assertEqual(type(user.stream), diaspy.streams.Outer)
f3eaa601
MM
182
183
dd0a4d9f
MM
184class ContactsTest(unittest.TestCase):
185 def testGetOnlySharing(self):
186 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
187 result = contacts.get(set='only_sharing')
188 for i in result:
dd0a4d9f
MM
189 self.assertEqual(diaspy.people.User, type(i))
190
27f09973
MM
191 def testGetAll(self):
192 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
193 result = contacts.get(set='all')
194 for i in result:
27f09973
MM
195 self.assertEqual(diaspy.people.User, type(i))
196
197 def testGet(self):
198 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
199 result = contacts.get()
200 for i in result:
27f09973
MM
201 self.assertEqual(diaspy.people.User, type(i))
202
dd0a4d9f 203
1467ec15
MM
204class PostTests(unittest.TestCase):
205 def testStringConversion(self):
206 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
207
208 def testRepr(self):
209 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
210
211
b0b4c46d 212class NotificationsTests(unittest.TestCase):
5de52803 213 def testMarkingRead(self):
b0b4c46d
MM
214 notifications = diaspy.notifications.Notifications(test_connection)
215 notif = None
216 for n in notifications:
217 if n.unread:
218 notif = n
219 break
220 if notif is not None:
221 n.mark(unread=False)
222 else:
223 warnings.warn('test not sufficient: no unread notifications were found')
224
dea56a86
MM
225
226class SettingsTests(unittest.TestCase):
b74513c5 227 profile = diaspy.settings.Profile(test_connection)
79a0fc19 228 account = diaspy.settings.Account(test_connection)
b74513c5
MM
229
230 def testGettingName(self):
231 self.assertEqual(testconf.user_names_tuple, self.profile.getName())
232
233 def testGettingLocation(self):
234 self.assertEqual(testconf.user_location_string, self.profile.getLocation())
235
236 def testGettingGender(self):
237 self.assertEqual(testconf.user_gender_string, self.profile.getGender())
238
239 def testGettingBirthDate(self):
240 self.assertEqual(testconf.user_date_of_birth, self.profile.getBirthDate(named_month=False))
241 self.assertEqual(testconf.user_date_of_birth_named, self.profile.getBirthDate(named_month=True))
242
243 def testGettingInfoIfProfileIsSearchable(self):
244 self.assertEqual(testconf.user_is_searchable, self.profile.isSearchable())
245
246 def testGettingInfoIfProfileIsNSFW(self):
247 self.assertEqual(testconf.user_is_nsfw, self.profile.isNSFW())
248
79a0fc19 249 def testGettingTags(self):
f91374ce 250 self.assertEqual(sorted(testconf.user_tags), sorted(self.profile.getTags()))
79a0fc19 251
dea56a86 252 def testGettingLanguages(self):
64de697f 253 self.assertIn(('en', 'English'), self.account.getLanguages())
b74513c5 254
f50cbea3
MM
255 def testGettingEmail(self):
256 self.assertEqual(testconf.user_email, self.account.getEmail())
257
dea56a86 258
6d8d47ce
MM
259if __name__ == '__main__':
260 print('Hello World!')
261 print('It\'s testing time!')
262 n = unittest.main()
263 print(n)
39af9756 264 print('Good! All tests passed!')