From: Marek Marecki Date: Fri, 3 May 2013 20:37:03 +0000 (+0200) Subject: Changes in tests configuration, small refactoring of `User()` X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a8fdc14a20ee5623f6ab390921861ef68827ed6e;p=diaspy.git Changes in tests configuration, small refactoring of `User()` --- diff --git a/diaspy/people.py b/diaspy/people.py index fdb47be..b834e6d 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -29,30 +29,41 @@ class User: pod, user = handle[1], handle[0] return (pod, user) - def _gethandle(self, diaspora_id, protocol='https'): - """Get user data using handle. + def _postproc(self, request): + """Makes necessary modifications to user data and + sets up a stream. + + :param request: request object + :type request: request """ - pod, user = self._sephandle(diaspora_id) - request = self._connection.session.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user)) if request.status_code != 200: - raise Exception('wrong error code: {0}'.format()) + raise Exception('wrong error code: {0}'.format(request.status_code)) else: request = request.json() - data = request[0]['author'] - self.data = data + data, final = request[0]['author'], {} + names = [('id', 'id'), + ('diaspora_id', 'diaspora_id'), + ('guid', 'guid'), + ('name', 'diaspora_name'), + ('avatar', 'image_urls'), + ] + for d, f in names: + final[f] = data[d] + self.data = final self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid'])) + def _gethandle(self, diaspora_id, protocol='https'): + """Get user data using handle. + """ + pod, user = self._sephandle(diaspora_id) + request = self._connection.session.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user)) + self._postproc(request) + def _getguid(self, guid): """Get user data using guid. """ request = self._connection.get('people/{0}.json'.format(guid)) - if request.status_code != 200: - raise Exception('wrong error code: {0}'.format()) - else: - request = request.json() - data = request[0]['author'] - self.data = data - self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid'])) + self._postproc(request) def fetchguid(self, guid): """Fetch user data using guid. diff --git a/manual/people.mdown b/manual/people.mdown index 4aa2178..75e5be5 100644 --- a/manual/people.mdown +++ b/manual/people.mdown @@ -30,13 +30,16 @@ The object is subscriptable so you can do like this: '12345678abcdefgh' -User object contains following items: +User object contains following items in its `data` dict: * `id`, `str`, id of the user; * `guid`, `str`, guid of the user; * `diaspora_id`, `str`, D\* id of the user; -* `name`, `str`, name of the user; -* `avatar`, `dict`, links to avatar of the user; +* `diaspora_name`, `str`, name of the user; +* `image_urls`, `dict`, links to avatar of the user; + +Also `User()` object contains a stream for this user. + * `stream`, `diaspy.streams.Outer`, stream of the user (provides all methods of generic stream); diff --git a/testconf.md b/testconf.md index 5580c0a..15fbb43 100644 --- a/testconf.md +++ b/testconf.md @@ -15,3 +15,5 @@ Template file: __passwd__ = 'strong_password' diaspora_id = 'user@pod.example.com' guid = '12345678abcdefgh' + # your name as others see it + diaspora_name = 'Marek Marecki' diff --git a/tests.py b/tests.py index a375413..4499311 100644 --- a/tests.py +++ b/tests.py @@ -30,15 +30,28 @@ finally: test_count_file = open('TEST_COUNT', 'w') test_count_file.write(str(test_count)) test_count_file.close() + +# Test connection setup print('Running test no. {0}'.format(test_count)) +print('Running tests on connection to pod: "{0}"'.format(__pod__)) -print('Running tests on connection to pod: "{0}"\t'.format(__pod__), end='') -test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) -test_connection.login() -print('[ CONNECTED ]\n') +print('Connecting to pod...\t', end='') +try: + test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) + test_connection.login() + print('[ CONNECTED ]\n') + err = False +except: + print('[ FAIL ]') + input('Hit [Return] to continue...') + err = True +finally: + if err: raise -#### Test suite code +####################################### +#### TEST SUITE CODE #### +####################################### class ConnectionTest(unittest.TestCase): def testLoginWithoutUsername(self): connection = diaspy.connection.Connection(pod=__pod__) @@ -135,18 +148,18 @@ class UserTests(unittest.TestCase): user = diaspy.people.User(test_connection) user.fetchhandle(testconf.diaspora_id) self.assertEqual(testconf.guid, user['guid']) - self.assertEqual(testconf.name, user['name']) + self.assertEqual(testconf.diaspora_name, user['diaspora_name']) self.assertIn('id', user.data) - self.assertIn('avatar', user.data) + self.assertIn('image_urls', user.data) self.assertEqual(type(user.stream), diaspy.streams.Outer) def testGettingUserByGUID(self): user = diaspy.people.User(test_connection) user.fetchguid(testconf.guid) self.assertEqual(testconf.diaspora_id, user['diaspora_id']) - self.assertEqual(testconf.name, user['name']) + self.assertEqual(testconf.diaspora_name, user['diaspora_name']) self.assertIn('id', user.data) - self.assertIn('avatar', user.data) + self.assertIn('image_urls', user.data) self.assertEqual(type(user.stream), diaspy.streams.Outer)