def test_paging_newest(self): """ Test that we can page through the quotes in the order that they were added. """ user = users.User('*****@*****.**') for i in range(models.PAGE_SIZE): quoteid = models.add_quote('This is a test.', user) self.assertNotEqual(quoteid, None) quotes, next = models.get_quotes_newest() self.assertEqual(len(quotes), models.PAGE_SIZE) self.assertEqual(next, None) quoteid = models.add_quote('This is a test.', user) self.assertNotEqual(quoteid, None) quotes, next = models.get_quotes_newest() self.assertEqual(len(quotes), models.PAGE_SIZE) self.assertNotEqual(next, None) quotes, next = models.get_quotes_newest(next) self.assertEqual(len(quotes), 1) self.assertEqual(next, None) # Cleanup models.del_quote(quoteid, user) quotes, next = models.get_quotes_newest() for q in quotes: models.del_quote(q.key().id(), user)
def test_add_quote(self): """ Add and remove quotes from the system. """ user = users.User('*****@*****.**') quoteid = models.add_quote('This is a test.', user) time.sleep(1.1) quoteid2 = models.add_quote('This is a test2.', user) self.assertNotEqual(quoteid, None) self.assertNotEqual(quoteid, 0) # Get the added quotes by creation order quotes, next = models.get_quotes_newest() self.assertEqual(quotes[0].key().id(), quoteid2) self.assertEqual(models.get_quote(quoteid2).key().id(), quoteid2) self.assertEqual(len(quotes), 2) # Remove one quote models.del_quote(quoteid2, user) quotes, next = models.get_quotes_newest() self.assertEqual(quotes[0].key().id(), quoteid) self.assertEqual(len(quotes), 1) # Remove last remaining quote models.del_quote(quoteid, user) quotes, next = models.get_quotes_newest() self.assertEqual(len(quotes), 0)
def test_del_quote_perms(self): """ Permissions of removing quotes. """ user = users.User('*****@*****.**') user2 = users.User('*****@*****.**') quoteid = models.add_quote('This is a test.', user) # Get the added quotes by creation order quotes, next = models.get_quotes_newest() self.assertEqual(quotes[0].key().id(), quoteid) self.assertEqual(len(quotes), 1) # Remove one quote, should fail to remove the quote models.del_quote(quoteid, user2) # Confirm the quote is still in the system quotes, next = models.get_quotes_newest() self.assertEqual(quotes[0].key().id(), quoteid) self.assertEqual(len(quotes), 1) # Remove one remaining quote models.del_quote(quoteid, user) quotes, next = models.get_quotes_newest() self.assertEqual(len(quotes), 0)
def post(self): """Add a quote to the system.""" user = users.get_current_user() text = self.request.get('newtidbit').strip() if len(text) > 500: text = text[:500] if not text: self.redirect('/') return uri = self.request.get('tidbituri').strip() parsed_uri = urlparse.urlparse(uri) progress_id, progress_msg, greeting = get_greeting() if uri and ( not parsed_uri.scheme or not parsed_uri.netloc ): template_values = { 'progress_id': progress_id, 'progress_msg': progress_msg, 'greeting': greeting, 'loggedin': user, 'text' : text, 'uri' : uri, 'error_msg' : 'The supplied link is not a valid absolute URI' } template_file = os.path.join(os.path.dirname(__file__), 'templates/add_quote_error.html' ) self.response.out.write(template.render(template_file, template_values)) else: quote_id = models.add_quote(text, user, uri=uri) if quote_id is not None: models.set_vote(long(quote_id), user, 1) self.redirect('/recent/') else: template_values = { 'progress_id': progress_id, 'progress_msg': progress_msg, 'greeting': greeting, 'loggedin': user, 'text' : text, 'uri' : uri, 'error_msg' : 'An error occured while adding this quote, please try again.' } template_file = os.path.join(os.path.dirname(__file__), 'templates/add_quote_error.html' ) self.response.out.write(template.render(template_file, template_values))
def test_game_progress(self): email = '*****@*****.**' user = users.User(email) hasVoted, hasAddedQuote = models.get_progress(user) self.assertFalse(hasVoted) self.assertFalse(hasAddedQuote) quoteid0 = models.add_quote('This is a test.', user, _created=1) hasVoted, hasAddedQuote = models.get_progress(user) self.assertFalse(hasVoted) self.assertTrue(hasAddedQuote) models.set_vote(quoteid0, user, 1) hasVoted, hasAddedQuote = models.get_progress(user) self.assertTrue(hasVoted) self.assertTrue(hasAddedQuote)
def test_voting(self): """ Test the voting system behaves as defined in the design document. """ user = users.User('*****@*****.**') user2 = users.User('*****@*****.**') # Day 1 - [quote 0 and 1 are added on Day 1 and # get 5 and 3 votes respectively. Rank is q0, q1.] # q0 (5) = 1 * 4 + 5 = 9 # q1 (3) = 1 * 4 + 3 = 7 quoteid0 = models.add_quote('This is a test.', user, _created=1) quoteid1 = models.add_quote('This is a test.', user, _created=1) models.set_vote(quoteid0, user, 1) models.set_vote(quoteid1, user, 3) quotes, next = models.get_quotes() self.assertEqual(models.voted(quotes[1], user), 1) self.assertEqual(models.voted(quotes[0], user), 3) self.assertEqual(quotes[0].key().id(), quoteid1) self.assertEqual(quotes[1].key().id(), quoteid0) models.set_vote(quoteid0, user, 5) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid0) self.assertEqual(quotes[1].key().id(), quoteid1) # q0 (5) + (3) = 1 * 4 + 8 = 12 # q1 (3) + (0) = 1 * 4 + 3 = 7 # q2 (3) = 2 * 4 + 3 = 11 quoteid2 = models.add_quote('This is a test.', user, _created=2) models.set_vote(quoteid0, user, 8) models.set_vote(quoteid1, user, 3) models.set_vote(quoteid2, user, 3) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid0) self.assertEqual(quotes[1].key().id(), quoteid2) self.assertEqual(quotes[2].key().id(), quoteid1) # q0 (5) + (3) = 1 * 4 + 8 = 12 # q1 (3) + (0) = 1 * 4 + 3 = 7 # q2 (3) + (2) = 2 * 5 + 4 = 14 # q3 (5) = 3 * 4 + 5 = 17 quoteid3 = models.add_quote('This is a test.', user, _created=3) models.set_vote(quoteid0, user, 8) models.set_vote(quoteid1, user, 3) models.set_vote(quoteid2, user, 5) models.set_vote(quoteid3, user, 5) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid3) self.assertEqual(quotes[1].key().id(), quoteid2) self.assertEqual(quotes[2].key().id(), quoteid0) self.assertEqual(quotes[3].key().id(), quoteid1) # q0 (5) + (3) + (1) = 1 * 4 + 9 = 13 # q1 (3) + (0) = 1 * 4 + 3 = 7 # q2 (3) + (2) = 2 * 5 + 4 = 14 # q3 (0) = 3 * 4 + 0 = 12 models.set_vote(quoteid0, user, 9) models.set_vote(quoteid3, user, 0) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid2) self.assertEqual(quotes[1].key().id(), quoteid0) self.assertEqual(quotes[2].key().id(), quoteid3) self.assertEqual(quotes[3].key().id(), quoteid1) models.del_quote(quoteid0, user) models.del_quote(quoteid1, user) models.del_quote(quoteid2, user) models.del_quote(quoteid3, user)