Source

comofas / test.py

Full commit
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
from comofas_questions import ComofasQuestions, Question
from comofas_answers import ComofasAnswers
from comofas_twitterer import ComofasTwitterer
from pynswers.Answers import Answers
import twitter

from comofas_credentials import *

class AnswerProtocol(object):
    def questionSearch(self,**kws):
        resps = list()
        resp = dict()
        resp["ChosenAnswer"] = "Por mas que se tente ficar ileso aos erros inerentes os atos politicos, algum projetil vai ser enderecado ao seu alvo."
        resps.append(resp)

        return resps

class TwitterProtocol(object):
    def PostUpdate(self, text, parameters):
        return "ok"


class MockUrllib(object):
    def urlopen(self, url):
        if url.startswith('http://search.twitter.com/search.json?q='):
            return open('test_mock_questions.json','r')
               
            


class TestComofasQuestions(unittest.TestCase):

    def setUp(self):
        #sobrecarrega padrão com mocks
        self.questions = ComofasQuestions(twitter_connection = TwitterProtocol())
        self.questions.urllib = MockUrllib()        
        self.results = self.questions.get_questions()
        
    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))
    
    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__':
    unittest.main()