From: Marek Marecki Date: Sun, 7 Jul 2013 16:04:31 +0000 (+0200) Subject: Software tested and bugs shaken out after refactoring X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9aa1c9608f928dccf66cec272ed26d7ee003deac;p=diaspy.git Software tested and bugs shaken out after refactoring --- diff --git a/diaspy/people.py b/diaspy/people.py index 0e15076..c796569 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -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'): diff --git a/diaspy/streams.py b/diaspy/streams.py index adc42eb..2d522ce 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -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): diff --git a/tests.py b/tests.py index 6527883..1302b63 100644 --- 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)