Commits

Bruno Renié committed 5fc0231

Proper mocking for the twitter test

Comments (0)

Files changed (4)

dotim/activity/fixtures/test_fixtures.json

     "pk": 1,
     "model": "activity.eventconfig",
     "fields": {
-      "url": "dotim/activity/tests/web/twitter.brutasse.json",
+      "url": "https://twitter.com/statuses/user_timeline/brutasse.json",
       "event_type": "twitter"
     }
   },

dotim/activity/models.py

 import datetime
 import feedparser
-import urllib
+import json
+import requests
+
+from dateutil.parser import parse
 
 from django.db import models
-from django.utils import simplejson
 from django.utils.translation import ugettext_lazy as _
 
 
-feedparser.USER_AGENT = 'Dotim/dev +http://bruno.im'
+USER_AGENT = 'Dotim/dev (http://bruno.im)'
 
 TYPES = (
-        ('twitter', 'Twitter'),
-        ('bitbucket', 'Bitbucket'),
-        ('github', 'Github'),
-        ('delicious', 'Del.icio.us'),
-        ('gitorious', 'Gitorious'),
-        ('djangosnippets', 'Django Snippets'),
+    ('twitter', 'Twitter'),
+    ('bitbucket', 'Bitbucket'),
+    ('github', 'Github'),
+    ('delicious', 'Del.icio.us'),
+    ('gitorious', 'Gitorious'),
+    ('djangosnippets', 'Django Snippets'),
 )
 
 
 
     def __unicode__(self):
         keys = {
-                'twitter': -1,
-                'bitbucket': 3,
-                'github': -1,
-                'delicious': -1,
-                'gitorious': -1,
-                'djangosnippets': -2,
+            'twitter': -1,
+            'bitbucket': 3,
+            'github': -1,
+            'delicious': -1,
+            'gitorious': -1,
+            'djangosnippets': -2,
         }
         return u'%s: %s' % (self.event_type,
                             self.url.split('/')[keys[self.event_type]])
 
-    def get_twitter_username(self):
+    @property
+    def twitter_username(self):
         return self.url.split('/')[5].split('.')[0]
 
     def get_filters(self):
         Fetches the lates tweets from this user.
         It uses the JSON method since the RSS one truncates the long tweets.
         """
-        response = urllib.urlopen(self.url)
-        json = response.read()
-        items = simplejson.loads(json)
+        response = requests.get(self.url, timeout=10, headers={
+            'User-Agent': USER_AGENT,
+        })
+        items = json.loads(response.content)
         # Items will contain 20 elements by default. ?count=X cannot be > 200
 
         # TODO: add the RT's
                 continue
 
             desc = item['text']
-            url = 'http://twitter.com/%s/statuses/%s' % \
-                    (self.get_twitter_username(), item['id'])
-            date = feedparser._parse_date(item['created_at'])
-            date = datetime.datetime(*date[:6])
+            url = 'https://twitter.com/%s/statuses/%s' % \
+                    (self.twitter_username, item['id'])
+            date = parse(item['created_at'])
 
             if max_date is None or date > max_date:
                 max_date = date

dotim/activity/tests/__init__.py

 import random
 import time
 
+from mock import patch
+from requests import Response
+
 from django.test import TestCase
 from django.core.urlresolvers import reverse
 
     def test_unicode(self):
         conf = EventConfig.objects.get(pk=1)
         conf.url = 'http://twitter.com/statuses/user_timeline/brutasse.json'
-        self.assertEquals('%s' % conf, 'twitter: brutasse.json')
+        self.assertEqual('%s' % conf, 'twitter: brutasse.json')
 
         conf = EventConfig.objects.get(pk=2)
         conf.url = 'http://github.com/brutasse.atom'
-        self.assertEquals('%s' % conf, 'github: brutasse.atom')
+        self.assertEqual('%s' % conf, 'github: brutasse.atom')
 
     def test_twitter_username(self):
         conf = EventConfig.objects.get(pk=1)
         conf.url = 'http://twitter.com/statuses/user_timeline/brutasse.json'
-        self.assertEquals(conf.get_twitter_username(), 'brutasse')
+        self.assertEqual(conf.twitter_username, 'brutasse')
 
-    def test_update_twitter(self):
+    @patch('requests.get')
+    def test_update_twitter(self, get):
         conf = EventConfig.objects.get(pk=1)
-        conf.get_twitter_username = lambda: 'brutasse'
+        response = Response()
+        response.status_code = 200
+        with open(os.path.join(ROOT, 'twitter.brutasse.json')) as f:
+            response._content = f.read()
+        get.return_value = response
         conf.update()
-        self.assertTrue(Event.objects.count() > 3)
+        get.assert_called_with(
+            u'https://twitter.com/statuses/user_timeline/brutasse.json',
+            headers={'User-Agent': 'Dotim/dev (http://bruno.im)'}, timeout=10)
+        self.assertEqual(Event.objects.count(), 4)
 
-        self.assertTrue('#europython' in '%s' % Event.objects.get(pk=1))
+        self.assertTrue(
+            Event.objects.filter(title__contains='#europython').exists()
+        )
 
     def test_update_feed(self):
         """
         conf.filters = 'aspirator'
         conf.save()
         self._test_feed(2)
-        self.assertEquals(Event.objects.count(), 27)  # 35 if not filtered
+        self.assertEqual(Event.objects.count(), 27)  # 35 if not filtered
 
-    def test_error(self):
+    @patch('requests.get')
+    def test_error(self, get):
+        get.side_effect
         conf = EventConfig.objects.get(pk=2)
         conf.url += 'nostatus'
         conf.update(feedparser=FakeFeedParser)
-        self.assertEquals(Event.objects.count(), 0)
+        self.assertEqual(Event.objects.count(), 0)
 
     def _test_feed(self, id):
         conf = EventConfig.objects.get(pk=id)
     def test_typed_page_2(self):
         url = reverse('activity:page', args=['github', 2])
         response = self.client.get(url)
-        self.assertEquals(response.status_code, 200)
+        self.assertEqual(response.status_code, 200)
 easy-thumbnails==1.0.3
 feedparser==5.1.2
 gunicorn==0.14.6
+mock==1.0b1
 psycopg2==2.4.5
+python-dateutil==1.5
+requests==0.13.9
 wsgiref==0.1.2