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 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_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_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 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 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)