Anonymous avatar Anonymous committed a752e7a

Primeiros testes e mocks

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
 
 import unittest
-from comofas2 import Comofas
+from comofas_questions import ComofasQuestions
+from comofas_answers import ComofasAnswers
+from comofas_twitterer import ComofasTwitterer
 from pynswers.Answers import Answers
 import twitter
+
 from comofas_credentials import *
 
 class AnswerProtocol(object):
         return resps
 
 class TwitterProtocol(object):
-
-    def PostUpdate(self,mensagem):
+    def PostUpdate(self, text, parameters):
         return "ok"
 
-class TestComofas(unittest.TestCase):
+
+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
+            
+
+
+class TestComofasQuestions(unittest.TestCase):
 
     def setUp(self):
-        pass
+        #sobrecarrega padrão com mocks
+        self.questions = ComofasQuestions(twitter_connection = TwitterProtocol())
+        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)
+    
+    def testGetQuestionsReturnsEssentialThings(self):
+        r = self.results[0]
+        self.assertTrue(isinstance(r['id'], int))
+        self.assertTrue(isinstance(r['from_user'], unicode))
+        self.assertTrue(isinstance(r['from_user_id'], int))
+        self.assertTrue(isinstance(r['iso_language_code'], unicode))
+        self.assertTrue(isinstance(r['text'], unicode))
+        
 
 
 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"}'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.