From 7260454dce8a9a42b42201292a259a91041492f4 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 16 Apr 2014 11:47:19 -0300 Subject: [PATCH] dont explode if the group have no places to go --- luncho/blueprints/voting.py | 12 ++++++++++-- tests/vote_tests.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/luncho/blueprints/voting.py b/luncho/blueprints/voting.py index 0fc82ac..b895d43 100644 --- a/luncho/blueprints/voting.py +++ b/luncho/blueprints/voting.py @@ -241,6 +241,13 @@ def get_vote(group_id): # calculate the decrementating value, based on the number of places max_places = min(current_app.config['PLACES_IN_VOTE'], 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) LOG.debug('For {places}, the decrement factor is {decrement}'.format( places=max_places, decrement=decrement)) @@ -256,8 +263,8 @@ def get_vote(group_id): # get the casted votes vote_value = 1.0 for cast in CastedVote.query.filter_by(vote=vote.cast): - if not cast.place in points: - points[cast.place] = 0.0; + if cast.place not in points: + points[cast.place] = 0.0 points[cast.place] += vote_value vote_value -= decrement @@ -284,6 +291,7 @@ def get_vote(group_id): closed=closed, results=result) + # ---------------------------------------------------------------------- # Helpers # ---------------------------------------------------------------------- diff --git a/tests/vote_tests.py b/tests/vote_tests.py index 932189f..27e7617 100644 --- a/tests/vote_tests.py +++ b/tests/vote_tests.py @@ -282,6 +282,23 @@ class TestVote(LunchoTests): self.assertJsonError(rv, 404, 'Group not found') 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__': unittest.main()