def test_basic_communication(self): monarch.config.rule.add('notification', 'deal', 1) now = datetime.datetime.now() now_2 = now + datetime.timedelta(minutes=2) now_4 = now + datetime.timedelta(minutes=4) array = [] with monarch.throttle('notification', 10, 'deal') as pipe: if pipe: array.append(1) with Stub(proxy=datetime.datetime.now()) as new_now: from datetime import datetime as new_datetime new_datetime.now() >> now_2 with monarch.throttle('notification', 10, 'deal') as pipe: if pipe: array.append(2) with Stub(proxy=datetime.datetime.now()) as new_now: from datetime import datetime as new_datetime new_datetime.now() >> now_4 with monarch.throttle('notification', 10, 'deal') as pipe: if pipe: array.append(3) self.assertTrue(array==[1,2,3])
def test_month_invoice_from_person(self): from transaction import Transaction with Stub() as first_person: first_person.id >> 1 # Credit card for first person only. self.credit_card.person_id = first_person.id with Stub() as first_transaction: first_transaction.credit_card_id >> self.credit_card.id first_transaction.value >> 100.50 first_transaction.month >> 10 with Stub() as second_transaction: second_transaction.credit_card_id >> self.credit_card.id second_transaction.value >> 200.10 second_transaction.month >> 10 with Stub() as third_transaction: third_transaction.credit_card_id >> self.credit_card.id third_transaction.value >> 50.20 third_transaction.month >> 11 Transaction.objects = { 1: first_transaction, 2: second_transaction, 3: third_transaction } CreditCard.month_invoice_from_person(10, first_person) |should| equal_to( 300.60 )
def test_handleRequest(self): f = self.fakeRequest self.t.storage = Stub() self.t.fileResponse = Stub() self.t.jsonResponse = Stub() for value in ['xls', 'json', 'csv']: f.GET = dict(table=value) self.t.handleRequest(f)
def test_settings_cary_over(self): test_script = "test_script();" app_id = "1235" with Stub() as parser: parser.delete_first_token() parser.parse(('endblock', )).render(Dummy()) >> test_script with Stub() as settings: settings.FACEBOOK_APP_ID >> app_id node = fb_init_block(parser, Dummy(), settings=settings) content = node.render(Dummy()) self.assertNotEqual(-1, content.find(test_script)) self.assertNotEqual(-1, content.find(app_id))
def test_credit_cards(self): with Stub() as first_credit_card: first_credit_card.person_id >> self.person.id first_credit_card.id >> 1 # Another person credit card with Stub() as second_credit_card: second_credit_card.person_id >> 'another-id' second_credit_card.id >> 2 from credit_card import CreditCard CreditCard.objects = {1: first_credit_card, 2: second_credit_card} self.person.credit_cards() | should | equal_to([first_credit_card])
def test_associate_credit_card(self): with Stub() as credit_card: credit_card.id >> 1 self.transaction.associate_credit_card(credit_card) self.transaction.credit_card_id | should | equal_to(credit_card.id)
def setUp(self): popit_api_instance = ApiInstance.objects.create( url='http://popit.org/api/v1') response_json = open( "elecciones/tests/sample_data/candideit_api_response.json") fiera_candidate_json = open( "elecciones/tests/sample_data/fiera_candidate.json") fiera_sin_links_json = open( "elecciones/tests/sample_data/fiera_sin_links.json") fiera_sin_twitter_json = open( "elecciones/tests/sample_data/fiera_con_un_link_pero_sin_twitter.json" ) self.parsed_elections = json.load(response_json) self.parsed_fiera = json.load(fiera_candidate_json) self.fiera_sin_links = json.load(fiera_sin_links_json) self.fiera_sin_twitter = json.load(fiera_sin_twitter_json) self.username = "******" self.api_key = "keyfiera" self.syncronizer = Syncronizer(self.username, self.api_key) with Stub() as api: api.election.get(username=self.username, api_key=self.api_key, offset=0) >> self.parsed_elections api.candidate(176).get(username=self.username, api_key=self.api_key) >> self.parsed_fiera #Here is where I mock the api self.syncronizer.api = api
def test_associate_seller(self): with Stub() as seller: seller.id >> 1 self.transaction.associate_seller(seller) self.transaction.seller_id | should | equal_to(seller.id)
def __given_jid_handle_is_currently_marked_as_offline(self, AJidHandle): with Stub() as presence: presence.getFrom() >> AJidHandle.getJid() presence.getShow() >> None presence.getType() >> 'unavailable' presence.getStatus() >> None self.__PresenceManager.updatePresence(presence)
def __given_jid_handle_status_msg_is_currently(self, AJidHandle, statusMsg): with Stub() as presence: presence.getFrom() >> AJidHandle.getJid() presence.getShow() >> None presence.getType() >> 'unavailable' presence.getStatus() >> statusMsg self.__PresenceManager.updatePresence(presence)
def test_month_transactions(self): from transaction import Transaction with Stub() as first_transaction: first_transaction.month >> 10 with Stub() as second_transaction: second_transaction.month >> 10 with Stub() as third_transaction: third_transaction.month >> 11 Transaction.objects = { 1: first_transaction, 2: second_transaction, 3: third_transaction } # October's transactions CreditCard.month_transactions(10) |should| equal_to( [first_transaction, second_transaction] )
def test_transactions_list(self): with Stub() as first_transaction: first_transaction.seller_id >> 1 first_transaction.month >> 10 with Stub() as second_transaction: second_transaction.seller_id >> 1 second_transaction.month >> 10 with Stub() as third_transaction: third_transaction.seller_id >> 1 third_transaction.month >> 11 Transaction.objects = { 1: first_transaction, 2: second_transaction, 3: third_transaction } # October's transactions Seller.transactions_list(10) | should | equal_to( [first_transaction, second_transaction])
def test_it_does_not_have_problems_with_other_links(self): with Stub() as api: api.candidate(178).get( username=self.username, api_key=self.api_key) >> self.fiera_sin_twitter self.syncronizer.api = api election = Eleccion.objects.create( nombre="laeleccion", popit_api_instance=popit_api_instance) candidate = Candidato.objects.create(nombre="Fiera", eleccion=election) self.syncronizer.sync_twitter(candidate, 178) fiera = Candidato.objects.get(nombre="Fiera") self.assertTrue(fiera.twitter is None)
def test_it_loads_several_pages(self): response_json_1 = open("elecciones/tests/sample_data/big_json_1.json") response_json_2 = open("elecciones/tests/sample_data/big_json_2.json") parsed_elections_1 = json.load(response_json_1) parsed_elections_2 = json.load(response_json_2) username = "******" api_key = "keyfiera" syncronizer = Syncronizer(username, api_key) with Stub() as api: api.election.get(username=self.username, api_key=self.api_key, offset=0) >> parsed_elections_1 api.election.get(username=self.username, api_key=self.api_key, offset=20) >> parsed_elections_2 api.candidate( kind_of(int)).get(username=self.username, api_key=self.api_key) >> self.parsed_fiera #Here is where I mock the api syncronizer.api = api syncronizer.sync_elections() self.assertEquals(Eleccion.objects.count(), 26)
def __given_second_jid_handle_is_available_and_first_is_not(self): with Stub() as PresenceManager: PresenceManager.isJidHandleAvailable(self.__JidHandles[0]) >> False PresenceManager.isJidHandleAvailable(self.__JidHandles[1]) >> True self.__PresenceManager = PresenceManager
def __given_nobody_is_available(self): self.__JidHandles = [] with Stub() as PresenceManager: PresenceManager.isJidHandleAvailable() >> False self.__PresenceManager = PresenceManager
def __given_that_the_confirmation_request_threshold_in_seconds_is( self, seconds): with Stub() as MyConfig: MyConfig.getConfirmationRequestSecondThreshold() >> str(seconds) self.__MyConfig = MyConfig
def __given_all_jid_handles_are_available(self): with Stub() as PresenceManager: PresenceManager.isJidHandleAvailable(any()) >> True self.__PresenceManager = PresenceManager
def test_olderThanNow(self): f = extra_filters.OlderThanNow('foo') s = Stub() f.enabled(s, True)
def test_list_from_person(self): with Stub() as person: person.id >> 1 self.credit_card.person_id = person.id CreditCard.list_from_person(person) |should| equal_to([self.credit_card])