Software tested and bugs shaken out after refactoring
authorMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 16:04:31 +0000 (18:04 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 16:04:31 +0000 (18:04 +0200)
diaspy/people.py
diaspy/streams.py
tests.py

index 0e15076ee513da01d91f57c33b82e6b6aad0ab36..c796569ddf2c7eba2a79e3d66cef3a8e7b128dfd 100644 (file)
@@ -29,12 +29,14 @@ class User():
 
     def __init__(self, connection, guid='', handle='', fetch='posts', id=0):
         self._connection = connection
+        self.handle = handle
+        self.guid = guid
         self.data = {
             'guid': guid,
             'handle': handle,
             'id': id,
         }
-        self._do_fetch(fetch)
+        self._fetch(fetch)
 
     def __getitem__(self, key):
         return self.data[key]
@@ -45,12 +47,13 @@ class User():
     def __repr__(self):
         return '{0} ({1})'.format(self['diaspora_name'], self['guid'])
 
-    def _do_fetch(self, fetch):
+    def _fetch(self, fetch):
+        """Fetch user posts or data.
+        """
         if fetch == 'posts':
-            if self['handle'] and self['guid']: self.fetchguid()
-            elif self['guid'] and not self['handle']: self.fetchguid()
-            elif self['handle'] and not self['guid']: self.fetchhandle()
-        elif fetch == 'data' and len(self['handle']):
+            if self.handle and not self.guid: self.fetchhandle()
+            else: self.fetchguid()
+        elif fetch == 'data' and self.handle:
             self.fetchprofile()
 
     def _sephandle(self):
@@ -58,9 +61,9 @@ class User():
 
         :returns: two-tuple (pod, user)
         """
-        if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', self['handle']) is None:
-            raise Exception('invalid handle: {0}'.format(self['handle']))
-        handle = self['handle'].split('@')
+        if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', self.handle) is None:
+            raise errors.UserError('invalid handle: {0}'.format(self.handle))
+        handle = self.handle.split('@')
         pod, user = handle[1], handle[0]
         return (pod, user)
 
@@ -101,7 +104,7 @@ class User():
     def fetchguid(self):
         """Fetch user data and posts using guid.
         """
-        request = self._connection.get('people/{0}.json'.format(self['guid']))
+        request = self._connection.get('people/{0}.json'.format(self.guid))
         self._postproc(request)
 
     def fetchprofile(self, protocol='https'):
index adc42eb0114986a72285b3bc7466f78fb3b9d2b1..2d522ce5df06a10b40713b0990ac4af9127a5a4c 100644 (file)
@@ -79,7 +79,7 @@ class Generic():
         for i in range(len(new_stream)):
             if new_stream[-i].id not in ids:
                 stream = [new_stream[-i]] + stream
-                ids.append(new_stream[-i].post_id)
+                ids.append(new_stream[-i].id)
         self._stream = stream
 
     def clear(self):
index 652788399a28b51d2c90786ba152f969231c1477..1302b63e13f70404151a2895ce75b3d18dcaf084 100644 (file)
--- a/tests.py
+++ b/tests.py
@@ -103,7 +103,7 @@ class StreamTest(unittest.TestCase):
         stream.update()
         post.delete()
         stream.purge()
-        self.assertNotIn(post.post_id, [p.post_id for p in stream])
+        self.assertNotIn(post.id, [p.id for p in stream])
 
     def testPostingText(self):
         stream = diaspy.streams.Stream(test_connection)
@@ -159,8 +159,8 @@ class UserTests(unittest.TestCase):
             self.assertRaises(Exception, user._sephandle, h)
 
     def testGettingUserByHandle(self):
-        user = diaspy.people.User(test_connection)
-        user.fetchhandle(testconf.diaspora_id)
+        user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
+        user.fetchhandle()
         self.assertEqual(testconf.guid, user['guid'])
         self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
         self.assertIn('id', user.data)
@@ -168,8 +168,8 @@ class UserTests(unittest.TestCase):
         self.assertEqual(type(user.stream), diaspy.streams.Outer)
 
     def testGettingUserByGUID(self):
-        user = diaspy.people.User(test_connection)
-        user.fetchguid(testconf.guid)
+        user = diaspy.people.User(test_connection, guid=testconf.guid)
+        user.fetchguid()
         self.assertEqual(testconf.diaspora_id, user['diaspora_id'])
         self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
         self.assertIn('id', user.data)