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")
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)
def test_conflict(self): slug = "abcd" Election(key_name=slug).put() user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": slug}) fetched = Election.all().fetch(2) self.assertEqual(len(fetched), 1)
def test_lowercased(self): request = "AbCd" user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": request}) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.key().name(), request.lower())
def test_omitted(self): user = self.login() app = TestApp(application) response = app.post("/create", params={"title": "Yet Another Design Competition"}) fetched = Election.all().fetch(2) self.assertEqual(len(fetched), 0)
def test_multiline(self): user = self.login() app = TestApp(application) description = "Election Description goes Here.\nMore lines are explicitly allowed." response = app.post("/create", params={"slug": "abcd", "description": description}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, description)
def test_trimmed(self): user = self.login() app = TestApp(application) title = " Election Title goes Here\n" response = app.post("/create", params={"slug": "abcd", "title": title}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.title, title.strip())
def test_explicit(self): user = self.login() app = TestApp(application) when = datetime(*(datetime.now() + timedelta(days=4, seconds=42)).timetuple()[:6]) response = app.post("/create", params={"slug": "abcd", "closes": str(when)}) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.closes, when)
def test_blank(self): # The title, if blank, should default to the slug. user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": "abcd", "title": ""}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.title, fetched.key().name())
def test_explicit(self): # An empty string for the public attribute is interpreted as false. user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": "abcd", "public": ""}) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.public, False)
def test_trimmed(self): user = self.login() app = TestApp(application) description = " Election Description goes Here\n" response = app.post("/create", params={"slug": "abcd", "description": description}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, description.strip())
def test_trimmed(self): # Spaces and hyphens should be trimmed from the ends of the slug. request = "-inner: " user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": request}) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.key().name(), "inner")
def test_default(self): # The description has something resembling a reasonable default. user = self.login() app = TestApp(application) default = "Created by " + user.nickname() response = app.post("/create", params={"slug": "abcd"}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, default)
def test_punctuation(self): # The slug should replace punctuation with hyphens. request = "one and two, three/four; five" user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": request}) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.key().name(), "one-and-two-three-four-five")
def test_numbers(self): # Periods should be allowed, particularly in numbers. request = "something-1.3" user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": request}) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.key().name(), request)
def test_creation_ignored(self): # The save routine should ignore a submitted created parameter. user = self.login() now = datetime.now() app = TestApp(application) response = app.post("/create", params={"slug": "abcd", "created": now + timedelta(days=2)}) fetched = Election.all().fetch(1)[0] self.assertAlmostEqual(fetched.created, datetime.now(), delta=timedelta(seconds=2))
def test_blank(self): # The description, if blank, should default to the slug. user = self.login() app = TestApp(application) default = "Created by " + user.nickname() response = app.post("/create", params={"slug": "abcd", "description": ""}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, default)
def test_added(self): user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": "abcd"}) fetched = Election.all().fetch(1)[0] self.assertAlmostEqual(fetched.starts, datetime.now(), delta=timedelta(seconds=2))
def test_user_ignored(self): # The save routine should ignore a submitted user parameter. user = self.login() email = "*****@*****.**" other = User(email=email) app = TestApp(application) response = app.post("/create", params={"slug": "abcd", "user": email}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.creator, user)
def test_title(self): # The election's title should be displayed on its main page. slug = "abcd" title = "Yet another design contest" Election(key_name=slug, title=title).put() app = TestApp(application) response = app.get("/" + slug) print response self.assertIn(title, response)
def test_form(self): # The creation form should fill in the slug properly. user = self.login() app = TestApp(application) page = app.get("/create") slug = "abcd" page.form.set("slug", slug) response = page.form.submit() fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.key().name(), slug)
def test_form(self): # The creation form should fill in the title properly. user = self.login() app = TestApp(application) page = app.get("/create") title = "Election Title goes Here" page.form.set("slug", "abcd") page.form.set("title", title) response = page.form.submit() fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.title, title)
def test_form(self): # The creation form should fill in the description properly. user = self.login() app = TestApp(application) page = app.get("/create") description = "Election Description goes Here" page.form.set("slug", "abcd") page.form.set("description", description) response = page.form.submit() fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, description)
def test_multiline(self): user = self.login() app = TestApp(application) description = "Election Description goes Here.\nMore lines are explicitly allowed." response = app.post("/create", params={ "slug": "abcd", "description": description }) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, description)
def test_trimmed(self): user = self.login() app = TestApp(application) description = " Election Description goes Here\n" response = app.post("/create", params={ "slug": "abcd", "description": description }) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, description.strip())
def test_blank(self): # The description, if blank, should default to the slug. user = self.login() app = TestApp(application) default = "Created by " + user.nickname() response = app.post("/create", params={ "slug": "abcd", "description": "" }) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.description, default)
def test_form(self): # The creation form should have a checkbox for the public property. # However, it shouldn't set the approved property. user = self.login() app = TestApp(application) page = app.get("/create") page.form.set("slug", "abcd") page.form.set("public", "1") response = page.form.submit() fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.public, True) self.assertEquals(fetched.approved, False)
def test_explicit(self): user = self.login() app = TestApp(application) when = datetime(*(datetime.now() + timedelta(days=4, seconds=42)).timetuple()[:6]) response = app.post("/create", params={ "slug": "abcd", "closes": str(when) }) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.closes, when)
def test_creation_ignored(self): # The save routine should ignore a submitted created parameter. user = self.login() now = datetime.now() app = TestApp(application) response = app.post("/create", params={ "slug": "abcd", "created": now + timedelta(days=2) }) fetched = Election.all().fetch(1)[0] self.assertAlmostEqual(fetched.created, datetime.now(), delta=timedelta(seconds=2))
def test_public_link(self): # Approved, currently-open elections should have a valid link on the front page. slug = "abcd" title = "Yet another design contest" closing = datetime.now() + timedelta(days=1) Election(key_name=slug, title=title, public=True, approved=True, closes=closing).put() app = TestApp(application) response = app.get("/") response.click(title) self.assertIn(title, response)
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 check_shown(self, shown, public=True, approved=True, closes=+1): slug = "abcd" title = "Yet another design contest" closing = datetime.now() + timedelta(days=closes) Election(key_name=slug, title=title, public=public, approved=approved, closes=closing).put() app = TestApp(application) response = app.get("/") print response if shown: self.assertIn(title, response) self.assertIn("/" + slug, response) else: self.assertNotIn(title, response) self.assertNotIn("/" + slug, response)
def test_empty(self): user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": ""}) fetched = Election.all().fetch(2) self.assertEqual(len(fetched), 0)
def test_checked(self): user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": "abcd", "public": "1"}) fetched = Election.all().fetch(1)[0] self.assertEqual(fetched.public, True)
def test_added(self): user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": "abcd"}) fetched = Election.all().fetch(1)[0] self.assertGreater(fetched.closes, datetime.now() + timedelta(days=7))
def test_user_added(self): user = self.login() app = TestApp(application) response = app.post("/create", params={"slug": "abcd"}) fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.creator, user)
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)
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)
def test_model(self): user = User(email="*****@*****.**") Election(creator = user).put() fetched = Election.all().fetch(1)[0] self.assertEquals(fetched.creator, user)
def get(self): current = Election.gql("ORDER BY created DESC LIMIT 10") self.render("list.html", elections=current)