Skip to content
Browse files

fixed bug with zero division on 0 following

  • Loading branch information...
1 parent a556cc7 commit b91e61bf089d8e6c1618dae14d33de5faab2344c @peterbe committed Sep 21, 2011
Showing with 41 additions and 10 deletions.
  1. +2 −2 handlers.py
  2. +1 −1 templates/following.html
  3. +1 −1 templates/following_error.html
  4. +37 −6 tests/test_handlers.py
View
4 handlers.py
@@ -383,7 +383,7 @@ def _set_ratio(self, options, key):
value = options[key]
followers = options['info'][value]['followers_count']
following = options['info'][value]['friends_count']
- ratio = 1.0 * followers / following
+ ratio = 1.0 * followers / max(following, 1)
options['info'][value]['ratio'] = '%.1f' % ratio
self.redis.sadd('allusernames', value)
key = 'ratios'
@@ -394,7 +394,7 @@ def _set_ratio(self, options, key):
-@route(r'/coolest')
+@route(r'/coolest', name='coolest')
class CoolestHandler(BaseHandler):
def get(self):
View
2 templates/following.html
@@ -78,7 +78,7 @@
</tr>
{% if info[username].get('rank') and info[this_username].get('rank') %}
<tr>
- <td class="label">rank<sup>1</sup>:</td>
+ <td class="label"><a href="{{ reverse_url('coolest') }}">rank</a> <sup>1</sup>:</td>
<td>{{ info[username]['rank'] }}</td>
<td>{{ info[this_username]['rank'] }}</td>
</tr>
View
2 templates/following_error.html
@@ -8,7 +8,7 @@
<p>Usually this just means the Twitter API is overloaded and needs a moments rest.</p>
-<h2 style="padding:20px;text-align:center"><a href=".">Try again &rarr;</a></h2>
+<h2 style="padding:20px;text-align:center"><a href="">Try again &rarr;</a></h2>
{% end %}
View
43 tests/test_handlers.py
@@ -41,13 +41,9 @@ def test_twitter_login_twitter_failing(self):
self.assertTrue('twitter.com' in response.headers['location'])
response = self.client.get(url, {'oauth_token':'xxx'})
- self.assertEqual(response.code, 302)
- to_url = self.reverse_url('auth_twitter')
- self.assertTrue(to_url in response.headers['location'])
-
- response = self.client.get(response.headers['location'])
self.assertEqual(response.code, 200)
- self.assertTrue(to_url in response.body)
+ self.assertTrue("Sorry" in response.body)
+ self.assertTrue("Try again" in response.body)
def _login(self, username=u'peterbe', name=u'Peter Bengtsson',
email=None):
@@ -347,6 +343,41 @@ def test_following_temporary_glitch_on_friendship(self):
self.assertTrue("Unable to look up friendship for obama"
in response.body)
+ def test_following_someone_following_0(self):
+ url = self.reverse_url('following', 'obama')
+ response = self.client.get(url)
+ self.assertEqual(response.code, 302)
+ self.assertTrue(self.reverse_url('auth_twitter') in
+ response.headers['location'])
+
+ self._login()
+ FollowingHandler.twitter_request = \
+ make_mock_twitter_request({
+ "/friendships/show": {u'relationship': {
+ u'target': {u'followed_by': False,
+ u'following': False,
+ u'screen_name': u'obama'}}},
+ "/users/show?screen_name=obama": {u'followers_count': 41700,
+ u'following': False,
+ u'friends_count': 0,
+ u'name': u'Barak',
+ u'screen_name': u'obama',
+ },
+ "/users/show?screen_name=peterbe": {
+ u'followers_count': 417,
+ u'following': False,
+ u'friends_count': 330,
+ u'name': u'Peter Bengtsson',
+ u'screen_name': u'peterbe',
+ }
+ })
+
+ response = self.client.get(url)
+ self.assertEqual(response.code, 200)
+ self.assertTrue('<title>obama is too cool for me' in response.body)
+ self.assertTrue('%.1f' % (417.0/330) in response.body)
+ self.assertTrue('%.1f' % (41700.0/1) in response.body)
+
def make_twitter_get_authenticated_user_callback(struct):
def twitter_get_authenticated_user(self, callback, **kw):

0 comments on commit b91e61b

Please sign in to comment.
Something went wrong with that request. Please try again.