with description('Reporter'): with before.all: self.y_test = ['category_1', 'category_2', 'category_1'] self.y_predict = ['category_1', 'category_1', 'category_2'] with it('should show the accuracy report'): with Spy() as accuracy: accuracy.score(self.y_test, self.y_predict).returns(0.99) reporter = Reporter(accuracy_score_function=accuracy.score, classification_report_function=None, confusion_matrix_function=None) reporter.show_accuracy_score(self.y_test, self.y_predict) expect(accuracy.score).to(have_been_called_with(self.y_test, self.y_predict)) with it('should show the precision, recall and f1_score report'): with Spy() as metric: metric.classification_report(self.y_test, self.y_predict).returns('') reporter = Reporter(accuracy_score_function=None, classification_report_function=metric.classification_report, confusion_matrix_function=None) reporter.show_precision_recall_and_f1_score(self.y_test, self.y_predict) expect(metric.classification_report).to(have_been_called_with(self.y_test, self.y_predict)) with it('should show the confusion matrix report'): with Spy() as metric: cm = [[1, 0], [0, 1]] metric.confusion_matrix(self.y_test,
message_publisher = Spy() receiver = Receiver( consumers=[events_consumer, basket_consumer], message_publisher=message_publisher) with message_publisher: message_publisher.publish(ANY_ARG).delegates(receiver.process) total_price = 0 num_items = 0 receiver.process(create_basket_created_event(basket_id)) expect(message_publisher.publish).not_to(have_been_called) receiver.process(create_add_item_command(basket_id, SHIRT_ID)) expect(message_publisher.publish).to(have_been_called_with( have_key('kind', ITEM_ADDED)).once) total_price += ITEMS[SHIRT_ID]['price'] num_items += 1 receiver.process(create_add_item_command(basket_id, SHIRT_ID)) expect(message_publisher.publish).to(have_been_called_with( have_key('kind', ITEM_ADDED)).twice) total_price += ITEMS[SHIRT_ID]['price'] num_items += 1 receiver.process(create_add_item_command(basket_id, HAT_ID)) expect(message_publisher.publish).to(have_been_called_with( have_key('kind', ITEM_ADDED)).exactly(3)) total_price += ITEMS[HAT_ID]['price'] num_items += 1
with it('should persist a custom log level'): logger = getLogger('amadeus') logger.setLevel(10) self.valid_params['logger'] = logger amadeus = Client(**self.valid_params) expect(amadeus.logger).to(be(logger)) expect(amadeus.logger.level).to(be(10)) with it('should warn when an unrecognized option is passed in'): logger = Spy() self.valid_params['logger'] = logger self.valid_params['foobar'] = 'test' Client(**self.valid_params) expect(logger.warning).to( have_been_called_with('Unrecognized option: foobar')) with it('should default to the test host'): amadeus = Client(**self.valid_params) expect(amadeus.host).to(equal(Client.HOSTS['test'])) with it('should allow for setting a different hostname'): self.valid_params['hostname'] = 'production' amadeus = Client(**self.valid_params) expect(amadeus.host).to(equal(Client.HOSTS['production'])) with it('should allow for setting a full different host'): host = 'http://foo.bar.com/' self.valid_params['host'] = host amadeus = Client(**self.valid_params) expect(amadeus.host).to(equal(host))
request.result = {'access_token': 'abc', 'expires_in': 1799} with Spy(Client) as client: client._unauthenticated_request(ANY_ARG).returns(request) client.client_id = '123' client.client_secret = '234' self.client = client with it('should make a new API call if no token has been loaded yet'): access_token = AccessToken(self.client) expect(access_token._bearer_token()).to(equal('Bearer abc')) expect(self.client._unauthenticated_request).to( have_been_called_with( 'POST', '/v1/security/oauth2/token', { 'grant_type': 'client_credentials', 'client_id': '123', 'client_secret': '234' })) with it('should return a cached token if it still valid'): access_token = AccessToken(self.client) expect(access_token._bearer_token()).to(equal('Bearer abc')) expect(access_token._bearer_token()).to(equal('Bearer abc')) expect(self.client._unauthenticated_request).to( have_been_called.once) with it('should return a cached token if it still valid'): access_token = AccessToken(self.client) expect(access_token._bearer_token()).to(equal('Bearer abc')) access_token.expires_at = 0
from mamba import description, it from expects import expect from doublex import Spy from doublex_expects import have_been_called_with import securecscc with description(securecscc.CreateCSCCNotificationChannel) as self: with it('creates a notification channel in Sysdig Secure'): settings = securecscc.Settings() sysdig_client = Spy(securecscc.SysdigSecureClient) action = securecscc.CreateCSCCNotificationChannel(sysdig_client) webhook_url = 'irrelevant webhook url' webhook_authentication_token = 'irrelevant webhook authentication token' action.run(webhook_url, webhook_authentication_token) expect(sysdig_client.create_webhook_notification_channel).\ to(have_been_called_with('Google Security Command Center', webhook_url, webhook_authentication_token))
sut = PypiPackageVersionRetriever(http_client) sut.retrieve_version(a_package_name) expect(http_client.get).to(have_been_called) with it('calls an http_client get function with a valid url'): a_package_name = 'a_package_name' http_client = Spy() sut = PypiPackageVersionRetriever(http_client) sut.retrieve_version(a_package_name) expected_url = 'https://pypi.python.org/pypi/{}/json'.format( a_package_name) expect(http_client.get).to(have_been_called_with(expected_url)) with context('when we make an http call FOOOOO'): with it('retrieve a json with the info BARRRRR'): a_package_name = 'a_package_name' http_client = Spy() sut = PypiPackageVersionRetriever(http_client) result = sut.retrieve_version(a_package_name) expected_result = 42 expect(result).to(equal(expected_url)) #################### PROPUESTA ################## with context('when the http query is done'):
self.surname = surname A_NAME = 'a_name' A_SURNAME = 'a_surname' with description('CustomerService'): with context('add a customer'): with it('calls repository put function'): customer = Customer(name=A_NAME, surname=A_SURNAME) customer_repository = Spy() customer_service = CustomerService(customer_repository) customer_service.add(customer) expect(customer_repository.put).to(have_been_called_with(customer)) with context('remove a customer'): with it('calls repository delete function'): customer = Customer(name=A_NAME, surname=A_SURNAME) customer_repository = Spy() customer_service = CustomerService(customer_repository) customer_service.remove(customer) expect(customer_repository.delete).to( have_been_called_with(customer)) with description('InMemoryRepository'): with context('put a customer'): with it('stores in memory'):
from mamba import description, context, it from doublex import Spy, Stub, ANY_ARG from expects import expect from doublex_expects import have_been_called_with from leonor.actions.request_assistance import RequestAssistance def a_requested_event(): return { "body": "{\"hospital\": \"an_hospital\", \"phone_number\": \"a_phone_number\", \"hour\": \"an_hour\"}" } with description('RequestAssistance', 'unit'): with context('When a user request assistance'): with it('should send a notification with the encoded request data'): notifier_spy = Spy() with Stub() as encoding_service_stub: encoding_service_stub.encode(ANY_ARG).returns('an_encoded_string') action = RequestAssistance(notifier=notifier_spy, encoding_service=encoding_service_stub) action.execute(event=a_requested_event()) expected_message = 'NUEVA CITA:\nHora: an_hour\nPinche en el siguiente link para aceptar:\n' \ 'http://psicovid.org/cita_an_encoded_string' expect(notifier_spy.notify).to(have_been_called_with(message=expected_message))