Merge branch 'streams' of https://github.com/Javafant/diaspora-api into streams
[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 # modules used by diaspy
7 import requests
8 import re
9 # actual diaspy code
10 import diaspy
11
12
13 #### SETUP STUFF
14 #### test suite configuration variables: can be adjusted to your liking
15 import testconf
16 __pod__ = testconf.__pod__
17 __username__ = testconf.__username__
18 __passwd__ = testconf.__passwd__
19
20
21 # Test counter
22 try:
23 test_count_file = open('TEST_COUNT', 'r')
24 test_count = int(test_count_file.read())
25 test_count_file.close()
26 except (IOError, ValueError):
27 test_count = 0
28 finally:
29 test_count += 1
30 test_count_file = open('TEST_COUNT', 'w')
31 test_count_file.write(str(test_count))
32 test_count_file.close()
33 print('Running test no. {0}'.format(test_count))
34
35 print('Running tests on connection to pod: "{0}"\t'.format(__pod__), end='')
36 test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
37 test_connection.login()
38 print('[ CONNECTED ]\n')
39
40
41 #### Test suite code
42 class StreamTest(unittest.TestCase):
43 def testGetting(self):
44 stream = diaspy.streams.Generic(test_connection)
45
46 def testGettingLength(self):
47 stream = diaspy.streams.Generic(test_connection)
48 len(stream)
49
50 def testClearing(self):
51 stream = diaspy.streams.Stream(test_connection)
52 stream.clear()
53 self.assertEqual(0, len(stream))
54
55 def testPurging(self):
56 stream = diaspy.streams.Stream(test_connection)
57 post = stream.post('#diaspy test')
58 stream.update()
59 post.delete()
60 stream.purge()
61 self.assertNotIn(post.post_id, [p.post_id for p in stream])
62
63 def testPostingText(self):
64 stream = diaspy.streams.Stream(test_connection)
65 post = stream.post('#diaspy test no. {0}'.format(test_count))
66 self.assertEqual(diaspy.models.Post, type(post))
67
68 def testPostingImage(self):
69 stream = diaspy.streams.Stream(test_connection)
70 stream.post_picture('./test-image.png')
71
72 def testCreatingTag(self):
73 ft = diaspy.streams.FollowedTags(test_connection)
74 ft.create('test')
75
76
77 class ConnectionTest(unittest.TestCase):
78 def testLoginWithoutUsername(self):
79 connection = diaspy.connection.Connection(pod=__pod__)
80 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
81
82 def testLoginWithoutPassword(self):
83 connection = diaspy.connection.Connection(pod=__pod__)
84 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
85
86 def testGettingUserInfo(self):
87 info = test_connection.getUserInfo()
88 self.assertEqual(dict, type(info))
89
90
91 class ClientTests(unittest.TestCase):
92 def testGettingStream(self):
93 client = diaspy.client.Client(test_connection)
94 stream = client.get_stream()
95 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
96
97 def testGettingNotifications(self):
98 client = diaspy.client.Client(test_connection)
99 notifications = client.get_notifications()
100 self.assertEqual(list, type(notifications))
101 if notifications: self.assertEqual(dict, type(notifications[0]))
102
103 def testGettingTagAsList(self):
104 client = diaspy.client.Client(test_connection)
105 tag = client.get_tag('foo')
106 self.assertEqual(list, type(tag))
107 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
108
109 def testGettingTagAsStream(self):
110 client = diaspy.client.Client(test_connection)
111 tag = client.get_tag('foo', stream=True)
112 self.assertEqual(diaspy.streams.Generic, type(tag))
113 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
114
115 def testGettingMailbox(self):
116 client = diaspy.client.Client(test_connection)
117 mailbox = client.get_mailbox()
118 self.assertEqual(list, type(mailbox))
119 self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
120
121
122 if __name__ == '__main__':
123 unittest.main()
124 c = diaspy.connection.Connection(__pod__, __username__, __passwd__)
125 c.login()
126 stream = diaspy.modules.Stream(c)
127 for post in stream:
128 if post['text'] == '#diaspy test': post.delete()