Changes in tests configuration, small refactoring of `User()`
authorMarek Marecki <triviuss@gmail.com>
Fri, 3 May 2013 20:37:03 +0000 (22:37 +0200)
committerMarek Marecki <triviuss@gmail.com>
Fri, 3 May 2013 20:37:03 +0000 (22:37 +0200)
diaspy/people.py
manual/people.mdown
testconf.md
tests.py

index fdb47be52fe87903d000bc0424a6363f0e51ceca..b834e6d60f73a55efbce140b5a6e7c4789633f2f 100644 (file)
@@ -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.
index 4aa2178bae13c0b665a08f8a2651de75401b25ce..75e5be5b22085321596b652c09e8bdca34bb9f3d 100644 (file)
@@ -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);
 
 
index 5580c0abed6b7fe9e1bda1a5f30669b4a7345e95..15fbb435686fe7990f95c6e39194acbbe12794a3 100644 (file)
@@ -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'
index a37541374bf84a57a547e1c07841376a3e5d994c..4499311719b505e6f12e3887189bbfa6e1e6e4d5 100644 (file)
--- 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)