5 # failure to import any of the modules below indicates failed tests
6 # modules used by diaspy
14 #### test suite configuration variables: can be adjusted to your liking
16 __pod__
= testconf
.__pod
__
17 __username__
= testconf
.__username
__
18 __passwd__
= testconf
.__passwd
__
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):
30 test_count_file
= open('TEST_COUNT', 'w')
31 test_count_file
.write(str(test_count
))
32 test_count_file
.close()
34 # Test connection setup
35 print('Running test no. {0}'.format(test_count
))
36 print('Running tests on connection to pod: "{0}"'.format(__pod__
))
38 print('Connecting to pod...\t', end
='')
40 test_connection
= diaspy
.connection
.Connection(pod
=__pod__
, username
=__username__
, password
=__passwd__
)
41 test_connection
.login()
42 print('[ CONNECTED ]\n')
46 input('Hit [Return] to continue...')
52 #######################################
53 #### TEST SUITE CODE ####
54 #######################################
55 class ConnectionTest(unittest
.TestCase
):
56 def testLoginWithoutUsername(self
):
57 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
58 self
.assertRaises(diaspy
.connection
.LoginError
, connection
.login
, password
='foo')
60 def testLoginWithoutPassword(self
):
61 connection
= diaspy
.connection
.Connection(pod
=__pod__
)
62 self
.assertRaises(diaspy
.connection
.LoginError
, connection
.login
, username
='user')
64 def testGettingUserInfo(self
):
65 info
= test_connection
.getUserInfo()
66 self
.assertEqual(dict, type(info
))
69 class ClientTests(unittest
.TestCase
):
70 def testGettingStream(self
):
71 client
= diaspy
.client
.Client(test_connection
)
72 stream
= client
.get_stream()
73 if len(stream
): self
.assertEqual(diaspy
.models
.Post
, type(stream
[0]))
75 def testGettingNotifications(self
):
76 client
= diaspy
.client
.Client(test_connection
)
77 notifications
= client
.get_notifications()
78 self
.assertEqual(list, type(notifications
))
79 if notifications
: self
.assertEqual(dict, type(notifications
[0]))
81 def testGettingTagAsList(self
):
82 client
= diaspy
.client
.Client(test_connection
)
83 tag
= client
.get_tag('foo')
84 self
.assertEqual(list, type(tag
))
85 if tag
: self
.assertEqual(diaspy
.models
.Post
, type(tag
[0]))
87 def testGettingTagAsStream(self
):
88 client
= diaspy
.client
.Client(test_connection
)
89 tag
= client
.get_tag('foo', stream
=True)
90 self
.assertEqual(diaspy
.streams
.Generic
, type(tag
))
91 if tag
: self
.assertEqual(diaspy
.models
.Post
, type(tag
[0]))
93 def testGettingMailbox(self
):
94 client
= diaspy
.client
.Client(test_connection
)
95 mailbox
= client
.get_mailbox()
96 self
.assertEqual(list, type(mailbox
))
97 self
.assertEqual(diaspy
.conversations
.Conversation
, type(mailbox
[0]))
100 class StreamTest(unittest
.TestCase
):
101 def testGetting(self
):
102 stream
= diaspy
.streams
.Generic(test_connection
)
104 def testGettingLength(self
):
105 stream
= diaspy
.streams
.Generic(test_connection
)
108 def testClearing(self
):
109 stream
= diaspy
.streams
.Stream(test_connection
)
111 self
.assertEqual(0, len(stream
))
113 def testPurging(self
):
114 stream
= diaspy
.streams
.Stream(test_connection
)
115 post
= stream
.post('#diaspy test')
119 self
.assertNotIn(post
.post_id
, [p
.post_id
for p
in stream
])
121 def testPostingText(self
):
122 stream
= diaspy
.streams
.Stream(test_connection
)
123 post
= stream
.post('#diaspy test no. {0}'.format(test_count
))
124 self
.assertEqual(diaspy
.models
.Post
, type(post
))
126 def testPostingImage(self
):
127 stream
= diaspy
.streams
.Stream(test_connection
)
128 stream
.post_picture('./test-image.png')
130 def testingAddingTag(self
):
131 ft
= diaspy
.streams
.FollowedTags(test_connection
)
135 class UserTests(unittest
.TestCase
):
136 def testHandleSeparatorRaisingExceptions(self
):
137 user
= diaspy
.people
.User(test_connection
)
138 handles
= ['user.pod.example.com',
139 'user@podexamplecom',
141 'use r@pod.example.com',
142 'user0@pod300 example.com',
145 self
.assertRaises(Exception, user
._sephandle
, h
)
147 def testGettingUserByHandle(self
):
148 user
= diaspy
.people
.User(test_connection
)
149 user
.fetchhandle(testconf
.diaspora_id
)
150 self
.assertEqual(testconf
.guid
, user
['guid'])
151 self
.assertEqual(testconf
.diaspora_name
, user
['diaspora_name'])
152 self
.assertIn('id', user
.data
)
153 self
.assertIn('image_urls', user
.data
)
154 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
156 def testGettingUserByGUID(self
):
157 user
= diaspy
.people
.User(test_connection
)
158 user
.fetchguid(testconf
.guid
)
159 self
.assertEqual(testconf
.diaspora_id
, user
['diaspora_id'])
160 self
.assertEqual(testconf
.diaspora_name
, user
['diaspora_name'])
161 self
.assertIn('id', user
.data
)
162 self
.assertIn('image_urls', user
.data
)
163 self
.assertEqual(type(user
.stream
), diaspy
.streams
.Outer
)
166 if __name__
== '__main__':
168 c
= diaspy
.connection
.Connection(__pod__
, __username__
, __passwd__
)
170 stream
= diaspy
.modules
.Stream(c
)
172 if post
['text'] == '#diaspy test': post
.delete()