Changes in diaspy.streams.Generic() and diaspy.models.Post()
[diaspy.git] / tests.py
CommitLineData
94c2d637
MM
1#!/usr/bin/env python3
2
3import unittest
4
313c9233 5# failure to import any of the modules below indicates failed tests
2ec93347 6# =================================================================
94c2d637 7# modules used by diaspy
88ec6cda 8import re
2ec93347
MM
9import requests
10import warnings
94c2d637
MM
11# actual diaspy code
12import diaspy
3311aa0c 13from diaspy import client as dclient
94c2d637
MM
14
15
62f1912f 16#### SETUP STUFF
94c2d637 17#### test suite configuration variables: can be adjusted to your liking
fab8b736
MM
18import testconf
19__pod__ = testconf.__pod__
20__username__ = testconf.__username__
21__passwd__ = testconf.__passwd__
94c2d637 22
505fc964 23
62f1912f
MM
24# Test counter
25try:
26 test_count_file = open('TEST_COUNT', 'r')
27 test_count = int(test_count_file.read())
28 test_count_file.close()
29except (IOError, ValueError):
30 test_count = 0
31finally:
32 test_count += 1
33test_count_file = open('TEST_COUNT', 'w')
34test_count_file.write(str(test_count))
35test_count_file.close()
36print('Running test no. {0}'.format(test_count))
94c2d637 37
6d8d47ce 38print('Running tests on connection: "{0}:{1}@{2}"\t'.format(testconf.__username__, '*'*len(testconf.__passwd__), __pod__), end='')
62f1912f
MM
39test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
40test_connection.login()
c1490af7 41print('[ CONNECTED ]\n')
62f1912f 42
73a9e0d3
MM
43# Setup test aspects
44print('Adding test aspects...\t', end='')
45diaspy.streams.Aspects(test_connection).add(testconf.test_aspect_name_fake)
46testconf.test_aspect_id = diaspy.streams.Aspects(test_connection).add(testconf.test_aspect_name).id
47print('OK')
48
49print([i['name'] for i in test_connection.getUserInfo()['aspects']])
50
51
66c3bb76
MM
52post_text = '#diaspy test no. {0}'.format(test_count)
53
62f1912f 54
a8fdc14a
MM
55#######################################
56#### TEST SUITE CODE ####
57#######################################
385e7ebe 58class ConnectionTest(unittest.TestCase):
264336e2 59 def testGettingUserInfo(self):
c1490af7 60 info = test_connection.getUserInfo()
264336e2
MM
61 self.assertEqual(dict, type(info))
62
7a31b4aa 63
4882952f 64class MessagesTests(unittest.TestCase):
fab8b736 65 def testGettingMailbox(self):
4882952f
MM
66 mailbox = diaspy.messages.Mailbox(test_connection)
67 if mailbox:
68 for i in range(len(mailbox)):
69 self.assertEqual(diaspy.models.Conversation, type(mailbox[i]))
fab8b736 70
f2eaa3c7 71
73a9e0d3
MM
72class AspectsTests(unittest.TestCase):
73 def testAspectsGettingID(self):
74 aspects = diaspy.streams.Aspects(test_connection)
75 id = aspects.getAspectID(testconf.test_aspect_name)
76 self.assertEqual(testconf.test_aspect_id, id)
77
78 def testAspectsRemoveById(self):
79 aspects = diaspy.streams.Aspects(test_connection)
80 for i in test_connection.getUserInfo()['aspects']:
81 if i['name'] == testconf.test_aspect_name:
82 print(i['id'], end=' ')
83 aspects.remove(id=i['id'])
84 break
85 names = [i['name'] for i in test_connection.getUserInfo()['aspects']]
86 print(names)
87 self.assertNotIn(testconf.test_aspect_name, names)
88
89 def testAspectsRemoveByName(self):
90 aspects = diaspy.streams.Aspects(test_connection)
91 print(testconf.test_aspect_name_fake, end=' ')
92 aspects.remove(name=testconf.test_aspect_name_fake)
93 names = [i['name'] for i in test_connection.getUserInfo()['aspects']]
94 print(names)
95 self.assertNotIn(testconf.test_aspect_name_fake, names)
96
97
f3eaa601
MM
98class StreamTest(unittest.TestCase):
99 def testGetting(self):
100 stream = diaspy.streams.Generic(test_connection)
101
102 def testGettingLength(self):
103 stream = diaspy.streams.Generic(test_connection)
104 len(stream)
105
106 def testClearing(self):
107 stream = diaspy.streams.Stream(test_connection)
108 stream.clear()
109 self.assertEqual(0, len(stream))
110
111 def testPurging(self):
112 stream = diaspy.streams.Stream(test_connection)
113 post = stream.post('#diaspy test')
114 stream.update()
115 post.delete()
116 stream.purge()
9aa1c960 117 self.assertNotIn(post.id, [p.id for p in stream])
f3eaa601
MM
118
119 def testPostingText(self):
120 stream = diaspy.streams.Stream(test_connection)
a98c6792 121 post = stream.post(post_text)
f3eaa601 122 self.assertEqual(diaspy.models.Post, type(post))
27f09973 123
f3eaa601
MM
124 def testPostingImage(self):
125 stream = diaspy.streams.Stream(test_connection)
2d4f6eeb
MM
126 try:
127 stream.post(text=post_text, photo='test-image.png')
1cff2093 128 except (diaspy.errors.StreamError) as e:
2d4f6eeb
MM
129 warnings.warn('{0}')
130 finally:
131 pass
f3eaa601
MM
132
133 def testingAddingTag(self):
134 ft = diaspy.streams.FollowedTags(test_connection)
135 ft.add('test')
136
27a28aaf
MM
137 def testActivity(self):
138 activity = diaspy.streams.Activity(test_connection)
139
140 def testMentionsStream(self):
141 mentions = diaspy.streams.Mentions(test_connection)
142
f3eaa601
MM
143
144class UserTests(unittest.TestCase):
145 def testHandleSeparatorRaisingExceptions(self):
f3eaa601
MM
146 handles = ['user.pod.example.com',
147 'user@podexamplecom',
148 '@pod.example.com',
149 'use r@pod.example.com',
150 'user0@pod300 example.com',
151 ]
152 for h in handles:
6d8d47ce 153 self.assertRaises(Exception, diaspy.people.sephandle, h)
141216df 154
6d8d47ce
MM
155 def testGettingUserByHandleData(self):
156 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id, fetch='data')
157 self.assertEqual(testconf.guid, user['guid'])
158 self.assertEqual(testconf.diaspora_id, user['handle'])
159 self.assertEqual(testconf.diaspora_name, user['name'])
160 self.assertEqual(type(user.stream), list)
161 self.assertEqual(user.stream, [])
162 self.assertIn('id', user.data)
163 self.assertIn('avatar', user.data)
164
165 def testGettingUserByHandlePosts(self):
9aa1c960 166 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
141216df 167 self.assertEqual(testconf.guid, user['guid'])
6d8d47ce
MM
168 self.assertEqual(testconf.diaspora_id, user['handle'])
169 self.assertEqual(testconf.diaspora_name, user['name'])
141216df 170 self.assertIn('id', user.data)
6d8d47ce 171 self.assertIn('avatar', user.data)
141216df 172 self.assertEqual(type(user.stream), diaspy.streams.Outer)
6d8d47ce 173
141216df 174 def testGettingUserByGUID(self):
9aa1c960 175 user = diaspy.people.User(test_connection, guid=testconf.guid)
6d8d47ce
MM
176 self.assertEqual(testconf.diaspora_id, user['handle'])
177 self.assertEqual(testconf.diaspora_name, user['name'])
141216df 178 self.assertIn('id', user.data)
6d8d47ce 179 self.assertIn('avatar', user.data)
141216df 180 self.assertEqual(type(user.stream), diaspy.streams.Outer)
f3eaa601
MM
181
182
dd0a4d9f
MM
183class ContactsTest(unittest.TestCase):
184 def testGetOnlySharing(self):
185 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
186 result = contacts.get(set='only_sharing')
187 for i in result:
dd0a4d9f
MM
188 self.assertEqual(diaspy.people.User, type(i))
189
27f09973
MM
190 def testGetAll(self):
191 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
192 result = contacts.get(set='all')
193 for i in result:
27f09973
MM
194 self.assertEqual(diaspy.people.User, type(i))
195
196 def testGet(self):
197 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
198 result = contacts.get()
199 for i in result:
27f09973
MM
200 self.assertEqual(diaspy.people.User, type(i))
201
dd0a4d9f 202
1467ec15
MM
203class PostTests(unittest.TestCase):
204 def testStringConversion(self):
205 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
206
207 def testRepr(self):
208 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
209
210
b0b4c46d 211class NotificationsTests(unittest.TestCase):
5de52803 212 def testMarkingRead(self):
b0b4c46d
MM
213 notifications = diaspy.notifications.Notifications(test_connection)
214 notif = None
215 for n in notifications:
216 if n.unread:
217 notif = n
218 break
219 if notif is not None:
220 n.mark(unread=False)
221 else:
222 warnings.warn('test not sufficient: no unread notifications were found')
223
dea56a86
MM
224
225class SettingsTests(unittest.TestCase):
b74513c5 226 profile = diaspy.settings.Profile(test_connection)
79a0fc19 227 account = diaspy.settings.Account(test_connection)
b74513c5
MM
228
229 def testGettingName(self):
230 self.assertEqual(testconf.user_names_tuple, self.profile.getName())
231
232 def testGettingLocation(self):
233 self.assertEqual(testconf.user_location_string, self.profile.getLocation())
234
235 def testGettingGender(self):
236 self.assertEqual(testconf.user_gender_string, self.profile.getGender())
237
238 def testGettingBirthDate(self):
239 self.assertEqual(testconf.user_date_of_birth, self.profile.getBirthDate(named_month=False))
240 self.assertEqual(testconf.user_date_of_birth_named, self.profile.getBirthDate(named_month=True))
241
242 def testGettingInfoIfProfileIsSearchable(self):
243 self.assertEqual(testconf.user_is_searchable, self.profile.isSearchable())
244
245 def testGettingInfoIfProfileIsNSFW(self):
246 self.assertEqual(testconf.user_is_nsfw, self.profile.isNSFW())
247
79a0fc19 248 def testGettingTags(self):
f91374ce 249 self.assertEqual(sorted(testconf.user_tags), sorted(self.profile.getTags()))
79a0fc19 250
dea56a86 251 def testGettingLanguages(self):
64de697f 252 self.assertIn(('en', 'English'), self.account.getLanguages())
b74513c5 253
f50cbea3
MM
254 def testGettingEmail(self):
255 self.assertEqual(testconf.user_email, self.account.getEmail())
256
dea56a86 257
6d8d47ce
MM
258if __name__ == '__main__':
259 print('Hello World!')
260 print('It\'s testing time!')
261 n = unittest.main()
262 print(n)
263 print('It was testing time!')