Esempio n. 1
0
class VotePageTestCase(VotingTestCase):
    def setUp(self):
        super(VotingTestCase, self).setUp()
        self.contest = Election(key_name=self.id(), title="Yet another contest")
        self.contest.put()
        candidates = ["Favorite", "Middling", "Underdog"]

        self.candidates = []
        for title in candidates:
            candidate = Candidate(title=title, parent=self.contest)
            candidate.put()
            self.candidates.append(str(candidate.key().id()))

        candidates = db.GqlQuery("SELECT * FROM Candidate WHERE ANCESTOR IS :1", self.contest)
        self.assertEqual(set(str(c.key().id()) for c in candidates), set(self.candidates))

        self.user = self.login()
        self.app = TestApp(application)
        self.page = self.app.get("/" + self.contest.key().name() + "/vote")

    def vote(self, ranks):
        for item_id in ranks:
            self.page.form.set("c" + item_id, ranks[item_id])
        self.page.form.submit()
        return Vote.get_by_key_name(self.contest.key().name() + "/" + str(self.user.user_id()))

    def test_vote_user(self):
        vote = self.vote(dict(izip(self.candidates, count(2))))
        self.assertEquals(vote.voter, self.user)

    def test_vote_election(self):
        vote = self.vote(dict(izip(self.candidates, count(2))))
        self.assertEquals(vote.election.key(), self.contest.key())

    def test_vote_ranked(self):
        expected = str.join(";", self.candidates)
        vote = self.vote(dict(izip(self.candidates, count(2))))
        self.assertEquals(vote.ranks, expected)

    def test_voting_equal(self):
        expected = str.join(",", self.candidates)
        vote = self.vote(dict.fromkeys(self.candidates, 2))
        self.assertEquals(vote.ranks, expected)

    def test_voting_mixed(self):
        first, second, third = self.candidates
        expected = second + "," + third + ";" + first
        vote = self.vote({first: 4, second: 2, third: 2})
        self.assertEquals(vote.ranks, expected)

    def test_voting_with_unranked(self):
        first, second, third = self.candidates
        expected = second + ";" + third
        vote = self.vote({first: 0, second: 2, third: 4})
        self.assertEquals(vote.ranks, expected)

    def test_voting_all_unranked(self):
        vote = self.vote(dict.fromkeys(self.candidates, 0))
        self.assertIsNone(vote)
Esempio n. 2
0
    def post(self):
        election = None
        try:
            slug = self._slugify(self.request.get("slug"))
            assert slug not in self.reserved

            election = Election(key_name=slug)
            election.creator = users.get_current_user()

            election.title = self.request.get("title").strip() or slug
            election.public = bool(self.request.get("public"))

            default = "Created by " + election.creator.nickname()
            election.description = self.request.get(
                "description").strip() or default

            when = self.request.get("starts")
            if when:
                election.starts = date_parser().parse(when)
            else:
                election.starts = datetime.now()

            when = self.request.get("closes")
            if when:
                election.closes = date_parser().parse(when)
            else:
                election.closes = election.starts + timedelta(days=14)

            # Check for duplication as the absolute last thing before
            # inserting, for slightly better protection.
            # Consider rolling back if there are two of them after inserting.
            assert not Election.get_by_key_name(slug)
            election.put()
            self.redirect("/%s/candidate" % slug)
        except Exception, err:
            logging.exception("Failed to create election: %r", repr(locals()))
            self.render("create.html", defaults=election, error=err)
Esempio n. 3
0
 def post(self):
     election = None
     try:
         slug = self._slugify(self.request.get("slug"))
         assert slug not in self.reserved
         
         election = Election(key_name=slug)
         election.creator = users.get_current_user()
         
         election.title = self.request.get("title").strip() or slug
         election.public = bool(self.request.get("public"))
         
         default = "Created by " + election.creator.nickname()
         election.description = self.request.get("description").strip() or default
         
         when = self.request.get("starts")
         if when:
             election.starts = date_parser().parse(when)
         else:
             election.starts = datetime.now()
         
         when = self.request.get("closes")
         if when:
             election.closes = date_parser().parse(when)
         else:
             election.closes = election.starts + timedelta(days=14)
         
         # Check for duplication as the absolute last thing before
         # inserting, for slightly better protection.
         # Consider rolling back if there are two of them after inserting.
         assert not Election.get_by_key_name(slug)
         election.put()
         self.redirect("/%s/candidate" % slug)
     except Exception, err:
         logging.exception("Failed to create election: %r", repr(locals()))
         self.render("create.html", defaults=election, error=err)
Esempio n. 4
0
class VotePageTestCase(VotingTestCase):
    def setUp(self):
        super(VotingTestCase, self).setUp()
        self.contest = Election(key_name=self.id(),
                                title="Yet another contest")
        self.contest.put()
        candidates = [
            "Favorite",
            "Middling",
            "Underdog",
        ]

        self.candidates = []
        for title in candidates:
            candidate = Candidate(title=title, parent=self.contest)
            candidate.put()
            self.candidates.append(str(candidate.key().id()))

        candidates = db.GqlQuery(
            "SELECT * FROM Candidate WHERE ANCESTOR IS :1", self.contest)
        self.assertEqual(set(str(c.key().id()) for c in candidates),
                         set(self.candidates))

        self.user = self.login()
        self.app = TestApp(application)
        self.page = self.app.get("/" + self.contest.key().name() + "/vote")

    def vote(self, ranks):
        for item_id in ranks:
            self.page.form.set("c" + item_id, ranks[item_id])
        self.page.form.submit()
        return Vote.get_by_key_name(self.contest.key().name() + "/" +
                                    str(self.user.user_id()))

    def test_vote_user(self):
        vote = self.vote(dict(izip(self.candidates, count(2))))
        self.assertEquals(vote.voter, self.user)

    def test_vote_election(self):
        vote = self.vote(dict(izip(self.candidates, count(2))))
        self.assertEquals(vote.election.key(), self.contest.key())

    def test_vote_ranked(self):
        expected = str.join(";", self.candidates)
        vote = self.vote(dict(izip(self.candidates, count(2))))
        self.assertEquals(vote.ranks, expected)

    def test_voting_equal(self):
        expected = str.join(",", self.candidates)
        vote = self.vote(dict.fromkeys(self.candidates, 2))
        self.assertEquals(vote.ranks, expected)

    def test_voting_mixed(self):
        first, second, third = self.candidates
        expected = second + "," + third + ";" + first
        vote = self.vote({first: 4, second: 2, third: 2})
        self.assertEquals(vote.ranks, expected)

    def test_voting_with_unranked(self):
        first, second, third = self.candidates
        expected = second + ";" + third
        vote = self.vote({first: 0, second: 2, third: 4})
        self.assertEquals(vote.ranks, expected)

    def test_voting_all_unranked(self):
        vote = self.vote(dict.fromkeys(self.candidates, 0))
        self.assertIsNone(vote)