Last commit before merging into master:
authorMarek Marecki <triviuss@gmail.com>
Mon, 20 May 2013 19:41:45 +0000 (21:41 +0200)
committerMarek Marecki <triviuss@gmail.com>
Mon, 20 May 2013 19:41:45 +0000 (21:41 +0200)
`diaspy.streams.Activity` now inherits from `Stream` and not `Generic`
`diaspy.streams.Aspects` got some refactoring,
new tests added,
testconf file must be updated in order for new tests to run (documented in `testconf.md`),

diaspy/streams.py
testconf.md
tests.py

index 39080e32ad7f861ea8343de3b3bef1e0f3b2c0ee..50d7b6f347caabd2717690ca21cc82c11f2d9943 100644 (file)
@@ -192,7 +192,7 @@ class Stream(Generic):
         return request.json()['data']['photo']['id']
 
 
-class Activity(Generic):
+class Activity(Stream):
     """Stream representing user's activity.
     """
     _location = 'activity.json'
@@ -232,38 +232,31 @@ class Aspects(Generic):
     An example call would be `aspects.json?aspect_ids=23,5,42`
     """
     _location = 'aspects.json'
-    _id_regexp = re.compile(r'<a href="/aspects/[0-9]+/edit" rel="facebox"')
 
-    def getID(self, aspect):
+    def getAspectID(self, aspect_name):
         """Returns id of an aspect of given name.
         Returns -1 if aspect is not found.
 
-        :param aspect: aspect name (must be spelled exactly as when created)
-        :type aspect: str
+        :param aspect_name: aspect name (must be spelled exactly as when created)
+        :type aspect_name: str
         :returns: int
         """
         id = -1
         aspects = self._connection.getUserInfo()['aspects']
-        for item in aspects:
-            if item['name'] == aspect: id = item['id']
+        for aspect in aspects:
+            if aspect['name'] == aspect_name: id = aspect['id']
         return id
 
     def filterByIDs(self, ids):
         self._location += '?{0}'.format(','.join(ids))
         self.fill()
 
-    def _getaid(self, response):
-        """Extracts id of just created aspect.
-        """
-        id = self._id_regexp.search(response.text).group(0).split('/')[2]
-        return int(id)
-
     def add(self, aspect_name, visible=0):
         """This function adds a new aspect.
-        Status code 422 is accepteb because it is returned by D* when
+        Status code 422 is accepted because it is returned by D* when
         you try to add aspect already present on your aspect list.
 
-        :returns: id of created aspect (or -1 if status_code was 422)
+        :returns: id of created aspect
         """
         data = {'authenticity_token': self._connection.get_token(),
                 'aspect[name]': aspect_name,
@@ -273,13 +266,15 @@ class Aspects(Generic):
         if request.status_code not in [200, 422]:
             raise Exception('wrong status code: {0}'.format(request.status_code))
 
-        if request.status_code == 422: id = -1
-        else: id = self._getaid(request)
+        id = self.getAspectID(aspect_name)
         return id
 
-    def remove(self, aspect_id=0, name=''):
+    def remove(self, aspect_id=-1, name=''):
         """This method removes an aspect.
-        500 is accepted because although the D* will
+        You can give it either id or name of the aspect.
+        When both are specified, id takes precedence over name.
+
+        Status code 500 is accepted because although the D* will
         go nuts it will remove the aspect anyway.
 
         :param aspect_id: id fo aspect to remove
@@ -287,7 +282,7 @@ class Aspects(Generic):
         :param name: name of aspect to remove
         :type name: str
         """
-        if not aspect_id and name: aspect_id = self.getID(name)
+        if aspect_id == -1 and name: aspect_id = self.getAspectID(name)
         data = {'authenticity_token': self._connection.get_token()}
         request = self._connection.delete('aspects/{}'.format(aspect_id),
                                           data=data)
index 15fbb435686fe7990f95c6e39194acbbe12794a3..042e89bbfb03718d0ea70d370afb0ad449a84108 100644 (file)
@@ -17,3 +17,11 @@ Template file:
     guid = '12345678abcdefgh'
     # your name as others see it
     diaspora_name = 'Marek Marecki'
+
+    #   both names are created
+    test_aspect_name = 'diaspy-test'
+    #   but this one will be deletd `by name`
+    test_aspect_name_fake = 'diaspy-test-fake'
+
+    #   needed here for tests to work
+    test_aspect_id = -1
index 0cdaf0396e0c58b308507d6b10f60a2696c2e244..6e1184bdd705cf521d1694c3a2bd30edbb2a89ab 100644 (file)
--- a/tests.py
+++ b/tests.py
@@ -38,7 +38,6 @@ test_connection.login()
 print('[ CONNECTED ]\n')
 
 post_text = '#diaspy test no. {0}'.format(test_count)
-test_aspect_id = -1
 
 
 #######################################
@@ -119,22 +118,23 @@ class StreamTest(unittest.TestCase):
 
     def testAspectsAdd(self):
         aspects = diaspy.streams.Aspects(test_connection)
-        test_aspect_id = aspects.add('diaspy-test')
-        aspects.add('diaspy-test-false')
+        aspects.add(testconf.test_aspect_name_fake)
+        testconf.test_aspect_id = aspects.add(testconf.test_aspect_name)
 
     def testAspectsGettingID(self):
         aspects = diaspy.streams.Aspects(test_connection)
-        id = aspects.getID('diaspy-test')
-        self.assertEqual(int, type(id))
-        self.assertEqual(test_aspect_id, id)
+        id = aspects.getAspectID(testconf.test_aspect_name)
+        self.assertEqual(testconf.test_aspect_id, id)
 
     def testAspectsRemoveById(self):
         aspects = diaspy.streams.Aspects(test_connection)
-        aspects.remove(test_aspect_id)
+        aspects.remove(testconf.test_aspect_id)
+        self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name))
 
     def testAspectsRemoveByName(self):
         aspects = diaspy.streams.Aspects(test_connection)
-        aspects.remove(name='diaspy-test-false')
+        aspects.remove(name=testconf.test_aspect_name_fake)
+        self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name_fake))
 
     def testActivity(self):
         activity = diaspy.streams.Activity(test_connection)
@@ -177,11 +177,9 @@ class UserTests(unittest.TestCase):
 class PostTests(unittest.TestCase):
     def testStringConversion(self):
         s = diaspy.streams.Stream(test_connection)
-        print(str(s[0]))
 
     def testRepr(self):
         s = diaspy.streams.Stream(test_connection)
-        print(repr(s[0]))
 
 
 if __name__ == '__main__': unittest.main()