Browse Source

dont explode if the group have no places to go

master
Julio Biason 11 years ago
parent
commit
7260454dce
  1. 12
      luncho/blueprints/voting.py
  2. 17
      tests/vote_tests.py

12
luncho/blueprints/voting.py

@ -241,6 +241,13 @@ def get_vote(group_id):
# calculate the decrementating value, based on the number of places # calculate the decrementating value, based on the number of places
max_places = min(current_app.config['PLACES_IN_VOTE'], max_places = min(current_app.config['PLACES_IN_VOTE'],
len(group.places)) len(group.places))
if max_places == 0:
# this means the group have no places at all, so the result will
# *always* be an empty list, closed.
return jsonify(status='OK',
results=[],
closed=True)
decrement = round(1.0 / float(max_places), 1) decrement = round(1.0 / float(max_places), 1)
LOG.debug('For {places}, the decrement factor is {decrement}'.format( LOG.debug('For {places}, the decrement factor is {decrement}'.format(
places=max_places, decrement=decrement)) places=max_places, decrement=decrement))
@ -256,8 +263,8 @@ def get_vote(group_id):
# get the casted votes # get the casted votes
vote_value = 1.0 vote_value = 1.0
for cast in CastedVote.query.filter_by(vote=vote.cast): for cast in CastedVote.query.filter_by(vote=vote.cast):
if not cast.place in points: if cast.place not in points:
points[cast.place] = 0.0; points[cast.place] = 0.0
points[cast.place] += vote_value points[cast.place] += vote_value
vote_value -= decrement vote_value -= decrement
@ -284,6 +291,7 @@ def get_vote(group_id):
closed=closed, closed=closed,
results=result) results=result)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Helpers # Helpers
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------

17
tests/vote_tests.py

@ -282,6 +282,23 @@ class TestVote(LunchoTests):
self.assertJsonError(rv, 404, 'Group not found') self.assertJsonError(rv, 404, 'Group not found')
return return
def test_get_results_no_places(self):
"""Try to get the results the group have no places."""
group = self._group()
self.user.groups.append(group)
server.db.session.commit()
rv = self.get('/vote/{group_id}/'.format(group_id=group.id),
token=self.user.token)
self.assertJsonOk(rv)
data = json.loads(rv.data)
self.assertTrue('results' in data)
self.assertTrue('closed' in data)
self.assertEquals(len(data['results']), 0)
self.assertTrue(data['closed']) # voting shouldn't be closed yet
return
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save