def testLastQuestionShouldGetLatestRetrievedQuestion(self):
   first_question = Question(id=12345, asker="firstuser", data="blah")
   second_question = Question(id=12345, asker="seconduser", data="blah2")
   third_question = Question(id=12345, asker="thirduser", data="blah3")
   
   first_question.put()
   second_question.put()
   third_question.put()
   
   last_question = Question.last_question()
   self.assertEqual("thirduser", last_question.asker)
 def testShouldOnlyAskForQuestionsAfterLastQuestion(self):
     last_question = Question(id=12345, asker="Cheese", data="pants")
     last_question.put()
     
     test_questions = []
     queue = Mock({ "add": None }, taskqueue)
     twitterbot = Mock({ "questions_since": test_questions }, TwitterBot)
     listener = Listener(twitterbot, queue)
     
     listener.listen()
     last_question.delete()
     
     twitterbot.mockCheckCall(0, "questions_since", last_question)
     self.failIf(queue.mockGetNamedCalls("add"))
     
 def testAnswerAQuestion(self):
   question = Question.from_mention({ "id" : 12345, "user" : { "screen_name": "testusername"}, "text": "@username restaurants collingwood" })
   client = Mock()
   oracle = Mock({"answer": "cheesy cheese"})
   twitterbot = TwitterBot(client, oracle, None)
   twitterbot.answer(question)
   client.mockCheckCall(0, "reply", 12345, "testusername", "cheesy cheese")
   oracle.mockCheckCall(0, "answer", "restaurants collingwood")
 def testQuestionShouldHandleIntegerIds(self):
   logging.info("Before question.from mention")
   question = Question.from_mention({ "id": 1234, "user": { "screen_name" : "testusername" }, "text" : "@csausbot cheese"})
   logging.info("After question from mention, before asserts")
   self.assertEqual(1234, question.id)
   self.assertEqual("testusername", question.asker)
   self.assertEqual("cheese", question.data)
   logging.info("Do we get to here ever?")
class AnswererTest(unittest.TestCase):
    
    test_question = None
    
    def setUp(self):
        self.test_question = Question(id=12345, asker="Cheese", data="blah blah")
        self.test_question.put()
        
    def test_should_answer_question(self):
        logging.info("test_question is %r", self.test_question)
        twitterbot = Mock({"answer": None}, TwitterBot)
        answerer = Answerer(twitterbot)
        
        answerer.answer(12345)
        twitterbot.mockCheckCall(0, "answer", self.test_question)
        
    def tearDown(self):
        self.test_question.delete()
 def test_should_shorten_answers(self):
   question = Question.from_mention({ "id" : 12345, "user" : { "screen_name": "testusername"}, "text": "@username restaurants collingwood" })
   client = Mock()
   oracle = Mock({"answer": "http://longurl.com/blah/cheesy/cheese"})
   urlshortener = Mock({"shorten": "http://shorturl.com/edam", "__nonzero__": 1})
   
   twitterbot = TwitterBot(client, oracle, urlshortener)
   twitterbot.answer(question)
   
   oracle.mockCheckCall(0, "answer", "restaurants collingwood")
   urlshortener.mockCheckCall(0, "__nonzero__")
   urlshortener.mockCheckCall(1, "shorten", "http://longurl.com/blah/cheesy/cheese")
   client.mockCheckCall(0, "reply", 12345, "testusername", "http://shorturl.com/edam")
  def testShouldFetchOnlyNewQuestions(self):
    test_mentions = [ 
      { "id" : 12345, "user": { "screen_name": "nomiddlename" }, "text": "@csausbot testing, testing, one, two, three."},
      { "id" : 12345, "user": { "screen_name": "cheekymonkey" }, "text": "@csausbot      "},
      { "id" : 12345, "user": { "screen_name": "eh" }, "text": "This @csausbot is really cool."}
      ]
    client = Mock({ "mentions": test_mentions })
    twitterbot = TwitterBot(client, None, None)
    
    lastQuestion = Question.from_mention({ "id" : 67891, "user" : { "screen_name": "testusername"}, "text": "@username blah" })
    questions = twitterbot.questions_since(lastQuestion)
    client.mockCheckCall(0, "mentions", 67891)
    
    self.assertEqual(len(questions), 1)
    question = questions[0]
    self.assertEqual(question.asker, "nomiddlename")
    self.assertEqual(question.data, "testing, testing, one, two, three.")

    query = Question.all()
    query.filter('id = ', 12345)
    self.assertEqual(1, query.count())
    db_question = query.get()
    self.assertEqual(question.asker, db_question.asker)
    self.assertEqual(question.data, db_question.data)
 def tearDown(self):
   query = Question.all()
   query.filter('id = ', 12345)
   for question in query:
     question.delete()
 def setUp(self):
     self.test_question = Question(id=12345, asker="Cheese", data="blah blah")
     self.test_question.put()
 def testQuestionBeginsWithUsernameShouldBeValid(self):
   question = Question.from_mention({ "id": 1234, "user" : { "screen_name": "testusername" }, "text": "@cheese blah" })
   self.assert_(question.is_valid())
 def testQuestionSetupFromMention(self):
   question = Question.from_mention({ "id": 1234, "user": { "screen_name" : "testusername" }, "text" : "@csausbot cheese"})
   self.assertEqual(1234, question.id)
   self.assertEqual("testusername", question.asker)
   self.assertEqual("cheese", question.data)
 def testMissingAskerShouldNotBeValid(self):
   question = Question.from_mention({ "id": 1234, "text" : "@csausbot cheese"})
   self.assertEqual(None, question)
   question = Question.from_mention({ "id": 1234, "user" : { "cheese" : "biscuits" }, "text": "@csausbot mince"})
   self.assertEqual(None, question)
 def testMissingIdShouldNotBeValid(self):
   question = Question.from_mention({ "user": { "screen_name" : "testusername" }, "text": "@csausbot blah"})
   self.assertEqual(None, question)
 def testEmptyQuestionShouldNotBeValid(self):
   question = Question.from_mention({ "id": 1234, "user" : { "screen_name": "testusername" }, "text": "@cheese     "})
   self.assertEqual(None, question)
   
   question = Question.from_mention({ "id": 1234, "user" : { "screen_name": "testusername" }, "text": "@cheese"})
   self.assertEqual(None, question)
 def setUp(self):
   query = Question.all()
   query.filter('id = ', 12345)
   for question in query:
     question.delete()
 def testQuestionWithUsernameInMiddleShouldNotBeValid(self):
   question = Question.from_mention({ "id": 1234, "user" : { "screen_name": "testusername" }, "text": "blah @cheese blah"})
   self.assertEqual(None, question)
 def testPutShouldWriteQuestionToDatabase(self):
   question = Question.from_mention({ "id": 12345, "user": { "screen_name" : "testusername" }, "text" : "@csausbot cheese"})
   key = question.put()
 
   stored_question = Question.get(key)
   self.assertEqual("testusername", stored_question.asker)