Merge pull request #13 from marekjm/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
73 class ConnectionTest(unittest.TestCase):
74 def testLoginWithoutUsername(self):
75 connection = diaspy.connection.Connection(pod=__pod__)
76 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
77
78 def testLoginWithoutPassword(self):
79 connection = diaspy.connection.Connection(pod=__pod__)
80 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
81
82 def testGettingUserInfo(self):
83 info = test_connection.getUserInfo()
84 self.assertEqual(dict, type(info))
85
86
87 class ClientTests(unittest.TestCase):
88 def testGettingStream(self):
89 client = diaspy.client.Client(test_connection)
90 stream = client.get_stream()
91 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
92
93 def testGettingNotifications(self):
94 client = diaspy.client.Client(test_connection)
95 notifications = client.get_notifications()
96 self.assertEqual(list, type(notifications))
97 if notifications: self.assertEqual(dict, type(notifications[0]))
98
99 def testGettingTagAsList(self):
100 client = diaspy.client.Client(test_connection)
101 tag = client.get_tag('foo')
102 self.assertEqual(list, type(tag))
103 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
104
105 def testGettingTagAsStream(self):
106 client = diaspy.client.Client(test_connection)
107 tag = client.get_tag('foo', stream=True)
108 self.assertEqual(diaspy.streams.Generic, type(tag))
109 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
110
111 def testGettingMailbox(self):
112 client = diaspy.client.Client(test_connection)
113 mailbox = client.get_mailbox()
114 self.assertEqual(list, type(mailbox))
115 self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
116
117
118 if __name__ == '__main__':
119 unittest.main()
120 c = diaspy.connection.Connection(__pod__, __username__, __passwd__)
121 c.login()
122 stream = diaspy.modules.Stream(c)
123 for post in stream:
124 if post['text'] == '#diaspy test': post.delete()