Commits

Anonymous committed 2468593

Novos testes (passando) e refactoring

Comments (0)

Files changed (3)

comofas_questions.py

         para executar testes offline.
         """
         self.twitter_connection = twitter_connection
+        self.urllib = urllib
+        
         # Esta linha não funciona, não descobrimos pq
         # self.twitter_connection.SetXTwitterHeaders('fasassim',"http://twitter.com/fasassim",'Ni!')
         self.search_url = 'http://search.twitter.com/search.json?q=' + KEYWORD
         """
         Este metodo retorna todos os resultados, sem filtrar por text e from_user
         """
-        feed = urllib.urlopen('http://search.twitter.com/search.json?q=' + KEYWORD)
+        feed = self.urllib.urlopen(self.search_url)
         updates = simplejson.loads(feed.read())
         return updates['results'] 
 
                     if DEBUG: print q
             
             self.persist_questions()
-        
+
+class Question(object):
+    #in progress, not used yet
+    def __init__(self, json):
+        self.id = json['id']
+        self.from_user = json['from_user']
+        self.from_user_id = json['from_user_id']
+        self.iso_language_code = json['iso_language_code']
+        self.text = json['text']
+    
+
 
 if __name__=='__main__':
     twitter_api = twitter.Api(username=TWITTER_USERNAME, password=TWITTER_PASSWORD)
 # -*- coding: utf-8 -*-
 
 import unittest
-from comofas_questions import ComofasQuestions
+from comofas_questions import ComofasQuestions, Question
 from comofas_answers import ComofasAnswers
 from comofas_twitterer import ComofasTwitterer
 from pynswers.Answers import Answers
 
 
 class MockUrllib(object):
-    class urlopen(object):
-        def __init__(self, url):
-            self.url = url
-        def read(self):            
-            if self.url.startswith('http://search.twitter.com/search.json?q='):
-                mock = open('test_mock_questions.json','r')
-                json = mock.read()
-                mock.close()
-                return json
+    def urlopen(self, url):
+        if url.startswith('http://search.twitter.com/search.json?q='):
+            return open('test_mock_questions.json','r')
+               
             
 
 
     def setUp(self):
         #sobrecarrega padrão com mocks
         self.questions = ComofasQuestions(twitter_connection = TwitterProtocol())
-        self.questions.urllib = MockUrllib()
-        
+        self.questions.urllib = MockUrllib()        
         self.results = self.questions.get_questions()
-    
-    def testUrllibMockado(self):                
-        feed = self.questions.urllib.urlopen(self.questions.search_url)
-        self.assertTrue(feed.read().startswith("\'{\"results\":["))
         
     def testGetQuestionsReturnsSomething(self):
         self.failIfEqual(None, self.results)
         self.assertTrue(isinstance(r['from_user_id'], int))
         self.assertTrue(isinstance(r['iso_language_code'], unicode))
         self.assertTrue(isinstance(r['text'], unicode))
+    
+    def testQuestionObjectIsCorrectlyInstantiated(self):
+        q = Question(self.results[0])
+        self.assertTrue(isinstance(q, Question))
+    
+    def testQuestionObjectHasEssentialProperties(self):
+        q = Question(self.results[0])
+        self.assertTrue(isinstance(q.id, int))
+        self.assertTrue(isinstance(q.from_user, unicode))
+        self.assertTrue(isinstance(q.from_user_id, int))
+        self.assertTrue(isinstance(q.iso_language_code, unicode))
+        self.assertTrue(isinstance(q.text, unicode))
         
+class TestComofasTwitts(unittest.TestCase):
+
+    def setUp(self):
+        #sobrecarrega padrão com mocks
+        self.twitts = ComofasTwitterer(twitter_connection = TwitterProtocol())
+
 
 
 if __name__ == '__main__':

test_mock_questions.json

-'{"results":[{"text":"E eu, que n\\u00e3o como no McDo e quero os brindes do McLanche Infeliz, comofas?","to_user_id":null,"from_user":"EuTergiverso","id":1286191703,"from_user_id":1308575,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/58248104\\/D._na_Rede_normal.jpg","created_at":"Fri, 06 Mar 2009 01:03:10 +0000"},{"text":"\\u00c9 s\\u00e9rio, pra que serve? comofas","to_user_id":null,"from_user":"biellebrunner","id":1286135710,"from_user_id":6259180,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/88805060\\/phoebe_normal.jpg","created_at":"Fri, 06 Mar 2009 00:47:36 +0000"},{"text":"quero minha velha vida de volta, comofas?","to_user_id":null,"from_user":"juliadarwich","id":1286062332,"from_user_id":3160622,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/static.twitter.com\\/images\\/default_profile_normal.png","created_at":"Fri, 06 Mar 2009 00:27:21 +0000"},{"text":"Desisto do BLIP, num lembro a senha COMOFAS???? =\\/","to_user_id":null,"from_user":"rafinha02","id":1286037352,"from_user_id":2134622,"iso_language_code":"pt","source":"<a href="http:\\/\\/83degrees.com\\/to\\/powertwitter">Power Twitter<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/88790782\\/DSC01010_normal.JPG","created_at":"Fri, 06 Mar 2009 00:20:20 +0000"},{"text":"Quero ficar @maisperto de casar... Mas a minha patroa que ficar @maisperto de ganhar um Note novo... #Comofas","to_user_id":null,"from_user":"triplosentido","id":1285996984,"from_user_id":29246,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/63429860\\/DSC00045_normal.JPG","created_at":"Fri, 06 Mar 2009 00:09:18 +0000"},{"text":"A gente quer fazer reserva no restaurante, mas o restaurante n\\u00e3o atende. #comofas","to_user_id":null,"from_user":"nat_gunji","id":1285980709,"from_user_id":91679,"iso_language_code":"pt","source":"<a href="http:\\/\\/www.tweetdeck.com\\/">TweetDeck<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/58605801\\/2725739724_6841666519_o_normal.jpg","created_at":"Fri, 06 Mar 2009 00:04:52 +0000"},{"text":"t\\u00e1. a quem eu penso que engano? t\\u00f4 hiper-mega-power frustada. comofas pra ficar de bom humor de novo? \\u00ac\\u00ac","to_user_id":null,"from_user":"deboramariano","id":1285966111,"from_user_id":2558834,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/87186569\\/Feij\\u00e3o_com_Rock_050_normal.jpg","created_at":"Fri, 06 Mar 2009 00:01:04 +0000"},{"text":"po! pq nao consigo mudar o email daqui? coloquei outro email + a senha e apareceu: 1 erro impediu este current user de ser salvo...#comofas?","to_user_id":null,"from_user":"magalyprado","id":1285959463,"from_user_id":69720,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/75035378\\/magalyja_normal.jpg","created_at":"Thu, 05 Mar 2009 23:59:28 +0000"},{"text":"eu quero ficar @maisperto do f\\u00edsico da Jessica Alba  #comofas","to_user_id":null,"from_user":"paulamiguel","id":1285940117,"from_user_id":1195304,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitterfox.net\\/">TwitterFox<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/87746328\\/lalalamodif992c_normal.jpg","created_at":"Thu, 05 Mar 2009 23:54:21 +0000"},{"text":"falei p sair e me arrependi,comofas?","to_user_id":null,"from_user":"bruhh","id":1285923996,"from_user_id":1371418,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/58820653\\/bruhhh_002_normal.jpg","created_at":"Thu, 05 Mar 2009 23:50:05 +0000"},{"text":"@edulm eu te adicionei no msn mas n\\u00e3o aparece aqui comofas","to_user_id":4607803,"to_user":"edulm","from_user":"rickenboy","id":1285889742,"from_user_id":3169768,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/68477787\\/1_normal.JPG","created_at":"Thu, 05 Mar 2009 23:41:06 +0000"},{"text":"Eu adorei do CD da Kelly Clarkson comofas\\/","to_user_id":null,"from_user":"yuitaki","id":1285868157,"from_user_id":2794991,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/67977663\\/delicia_normal.gif","created_at":"Thu, 05 Mar 2009 23:35:36 +0000"},{"text":"comofas pra ficar sem fazer nada?","to_user_id":null,"from_user":"miraxburguer","id":1285847303,"from_user_id":3947771,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/83196429\\/S6300436_normal.JPG","created_at":"Thu, 05 Mar 2009 23:30:20 +0000"},{"text":"S\\u00f3 acaba quando termina. Mas n\\u00e3o termina nuuuunca... Comofas?","to_user_id":null,"from_user":"vanessa_aguiar","id":1285811050,"from_user_id":167755,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/80648034\\/baixa_normal.jpg","created_at":"Thu, 05 Mar 2009 23:21:07 +0000"},{"text":"@fseixas o melhor de tudo \\u00e9: "Anima\\u00e7\\u00f5es -> Flash ou Youtube?" youtube pra anima\\u00e7\\u00e3o, #comofas\\/","to_user_id":10468,"to_user":"fseixas","from_user":"jaderubini","id":1285785313,"from_user_id":603768,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitterfox.net\\/">TwitterFox<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/80911503\\/Photo_47_Editada_normal.jpg","created_at":"Thu, 05 Mar 2009 23:14:45 +0000"}],"since_id":0,"max_id":1286191703,"refresh_url":"?since_id=1286191703&q=comofas","results_per_page":15,"next_page":"?page=2&max_id=1286191703&q=comofas","completed_in":0.02515,"page":1,"query":"comofas"}'
+{"results":[{"text":"E eu, que n\\u00e3o como no McDo e quero os brindes do McLanche Infeliz, comofas?","to_user_id":null,"from_user":"EuTergiverso","id":1286191703,"from_user_id":1308575,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/58248104\\/D._na_Rede_normal.jpg","created_at":"Fri, 06 Mar 2009 01:03:10 +0000"},{"text":"\\u00c9 s\\u00e9rio, pra que serve? comofas","to_user_id":null,"from_user":"biellebrunner","id":1286135710,"from_user_id":6259180,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/88805060\\/phoebe_normal.jpg","created_at":"Fri, 06 Mar 2009 00:47:36 +0000"},{"text":"quero minha velha vida de volta, comofas?","to_user_id":null,"from_user":"juliadarwich","id":1286062332,"from_user_id":3160622,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/static.twitter.com\\/images\\/default_profile_normal.png","created_at":"Fri, 06 Mar 2009 00:27:21 +0000"},{"text":"Desisto do BLIP, num lembro a senha COMOFAS???? =\\/","to_user_id":null,"from_user":"rafinha02","id":1286037352,"from_user_id":2134622,"iso_language_code":"pt","source":"<a href="http:\\/\\/83degrees.com\\/to\\/powertwitter">Power Twitter<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/88790782\\/DSC01010_normal.JPG","created_at":"Fri, 06 Mar 2009 00:20:20 +0000"},{"text":"Quero ficar @maisperto de casar... Mas a minha patroa que ficar @maisperto de ganhar um Note novo... #Comofas","to_user_id":null,"from_user":"triplosentido","id":1285996984,"from_user_id":29246,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/63429860\\/DSC00045_normal.JPG","created_at":"Fri, 06 Mar 2009 00:09:18 +0000"},{"text":"A gente quer fazer reserva no restaurante, mas o restaurante n\\u00e3o atende. #comofas","to_user_id":null,"from_user":"nat_gunji","id":1285980709,"from_user_id":91679,"iso_language_code":"pt","source":"<a href="http:\\/\\/www.tweetdeck.com\\/">TweetDeck<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/58605801\\/2725739724_6841666519_o_normal.jpg","created_at":"Fri, 06 Mar 2009 00:04:52 +0000"},{"text":"t\\u00e1. a quem eu penso que engano? t\\u00f4 hiper-mega-power frustada. comofas pra ficar de bom humor de novo? \\u00ac\\u00ac","to_user_id":null,"from_user":"deboramariano","id":1285966111,"from_user_id":2558834,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/87186569\\/Feij\\u00e3o_com_Rock_050_normal.jpg","created_at":"Fri, 06 Mar 2009 00:01:04 +0000"},{"text":"po! pq nao consigo mudar o email daqui? coloquei outro email + a senha e apareceu: 1 erro impediu este current user de ser salvo...#comofas?","to_user_id":null,"from_user":"magalyprado","id":1285959463,"from_user_id":69720,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/75035378\\/magalyja_normal.jpg","created_at":"Thu, 05 Mar 2009 23:59:28 +0000"},{"text":"eu quero ficar @maisperto do f\\u00edsico da Jessica Alba  #comofas","to_user_id":null,"from_user":"paulamiguel","id":1285940117,"from_user_id":1195304,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitterfox.net\\/">TwitterFox<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/87746328\\/lalalamodif992c_normal.jpg","created_at":"Thu, 05 Mar 2009 23:54:21 +0000"},{"text":"falei p sair e me arrependi,comofas?","to_user_id":null,"from_user":"bruhh","id":1285923996,"from_user_id":1371418,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/58820653\\/bruhhh_002_normal.jpg","created_at":"Thu, 05 Mar 2009 23:50:05 +0000"},{"text":"@edulm eu te adicionei no msn mas n\\u00e3o aparece aqui comofas","to_user_id":4607803,"to_user":"edulm","from_user":"rickenboy","id":1285889742,"from_user_id":3169768,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/68477787\\/1_normal.JPG","created_at":"Thu, 05 Mar 2009 23:41:06 +0000"},{"text":"Eu adorei do CD da Kelly Clarkson comofas\\/","to_user_id":null,"from_user":"yuitaki","id":1285868157,"from_user_id":2794991,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/67977663\\/delicia_normal.gif","created_at":"Thu, 05 Mar 2009 23:35:36 +0000"},{"text":"comofas pra ficar sem fazer nada?","to_user_id":null,"from_user":"miraxburguer","id":1285847303,"from_user_id":3947771,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/83196429\\/S6300436_normal.JPG","created_at":"Thu, 05 Mar 2009 23:30:20 +0000"},{"text":"S\\u00f3 acaba quando termina. Mas n\\u00e3o termina nuuuunca... Comofas?","to_user_id":null,"from_user":"vanessa_aguiar","id":1285811050,"from_user_id":167755,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitter.com\\/">web<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/80648034\\/baixa_normal.jpg","created_at":"Thu, 05 Mar 2009 23:21:07 +0000"},{"text":"@fseixas o melhor de tudo \\u00e9: "Anima\\u00e7\\u00f5es -> Flash ou Youtube?" youtube pra anima\\u00e7\\u00e3o, #comofas\\/","to_user_id":10468,"to_user":"fseixas","from_user":"jaderubini","id":1285785313,"from_user_id":603768,"iso_language_code":"pt","source":"<a href="http:\\/\\/twitterfox.net\\/">TwitterFox<\\/a>","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/80911503\\/Photo_47_Editada_normal.jpg","created_at":"Thu, 05 Mar 2009 23:14:45 +0000"}],"since_id":0,"max_id":1286191703,"refresh_url":"?since_id=1286191703&q=comofas","results_per_page":15,"next_page":"?page=2&max_id=1286191703&q=comofas","completed_in":0.02515,"page":1,"query":"comofas"}