Commit | Line | Data |
---|---|---|
94c2d637 MM |
1 | #!/usr/bin/env python3 |
2 | ||
3 | import unittest | |
4 | ||
313c9233 | 5 | # failure to import any of the modules below indicates failed tests |
94c2d637 | 6 | # modules used by diaspy |
88ec6cda MM |
7 | import requests |
8 | import re | |
94c2d637 MM |
9 | # actual diaspy code |
10 | import diaspy | |
11 | ||
12 | ||
62f1912f | 13 | #### SETUP STUFF |
94c2d637 | 14 | #### test suite configuration variables: can be adjusted to your liking |
fab8b736 MM |
15 | import testconf |
16 | __pod__ = testconf.__pod__ | |
17 | __username__ = testconf.__username__ | |
18 | __passwd__ = testconf.__passwd__ | |
94c2d637 | 19 | |
505fc964 | 20 | |
62f1912f MM |
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)) | |
94c2d637 | 34 | |
c1490af7 | 35 | print('Running tests on connection to pod: "{0}"\t'.format(__pod__), end='') |
62f1912f MM |
36 | test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) |
37 | test_connection.login() | |
c1490af7 | 38 | print('[ CONNECTED ]\n') |
62f1912f MM |
39 | |
40 | ||
41 | #### Test suite code | |
7a31b4aa MM |
42 | class StreamTest(unittest.TestCase): |
43 | def testGetting(self): | |
c1490af7 | 44 | stream = diaspy.streams.Generic(test_connection) |
7a31b4aa MM |
45 | |
46 | def testGettingLength(self): | |
c1490af7 | 47 | stream = diaspy.streams.Generic(test_connection) |
7a31b4aa MM |
48 | len(stream) |
49 | ||
50 | def testClearing(self): | |
62f1912f | 51 | stream = diaspy.streams.Stream(test_connection) |
7a31b4aa MM |
52 | stream.clear() |
53 | self.assertEqual(0, len(stream)) | |
54 | ||
505fc964 | 55 | def testPurging(self): |
62f1912f | 56 | stream = diaspy.streams.Stream(test_connection) |
505fc964 MM |
57 | post = stream.post('#diaspy test') |
58 | stream.update() | |
59 | post.delete() | |
60 | stream.purge() | |
62f1912f | 61 | self.assertNotIn(post.post_id, [p.post_id for p in stream]) |
505fc964 | 62 | |
7a31b4aa | 63 | def testPostingText(self): |
c1490af7 | 64 | stream = diaspy.streams.Stream(test_connection) |
62f1912f | 65 | post = stream.post('#diaspy test no. {0}'.format(test_count)) |
7a31b4aa MM |
66 | self.assertEqual(diaspy.models.Post, type(post)) |
67 | ||
68 | def testPostingImage(self): | |
c1490af7 | 69 | stream = diaspy.streams.Stream(test_connection) |
7a31b4aa MM |
70 | stream.post_picture('./test-image.png') |
71 | ||
beaa09fb | 72 | def testingAddingTag(self): |
fbb19900 | 73 | ft = diaspy.streams.FollowedTags(test_connection) |
0ed63ce6 | 74 | ft.add('test') |
fbb19900 | 75 | |
7a31b4aa | 76 | |
385e7ebe MM |
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 | ||
264336e2 | 86 | def testGettingUserInfo(self): |
c1490af7 | 87 | info = test_connection.getUserInfo() |
264336e2 MM |
88 | self.assertEqual(dict, type(info)) |
89 | ||
7a31b4aa MM |
90 | |
91 | class ClientTests(unittest.TestCase): | |
264336e2 | 92 | def testGettingStream(self): |
c1490af7 | 93 | client = diaspy.client.Client(test_connection) |
264336e2 | 94 | stream = client.get_stream() |
7a31b4aa | 95 | if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0])) |
264336e2 MM |
96 | |
97 | def testGettingNotifications(self): | |
c1490af7 | 98 | client = diaspy.client.Client(test_connection) |
264336e2 MM |
99 | notifications = client.get_notifications() |
100 | self.assertEqual(list, type(notifications)) | |
101 | if notifications: self.assertEqual(dict, type(notifications[0])) | |
102 | ||
62f1912f | 103 | def testGettingTagAsList(self): |
c1490af7 | 104 | client = diaspy.client.Client(test_connection) |
fab8b736 MM |
105 | tag = client.get_tag('foo') |
106 | self.assertEqual(list, type(tag)) | |
107 | if tag: self.assertEqual(diaspy.models.Post, type(tag[0])) | |
108 | ||
62f1912f | 109 | def testGettingTagAsStream(self): |
c1490af7 | 110 | client = diaspy.client.Client(test_connection) |
62f1912f MM |
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 | ||
fab8b736 | 115 | def testGettingMailbox(self): |
c1490af7 | 116 | client = diaspy.client.Client(test_connection) |
fab8b736 MM |
117 | mailbox = client.get_mailbox() |
118 | self.assertEqual(list, type(mailbox)) | |
119 | self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0])) | |
120 | ||
f2eaa3c7 | 121 | |
1232dac5 MM |
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() |