def test_any_arg_matches_property(self): prop_stub = Spy(CollaboratorWithProperty) when(prop_stub).prop.returns(5) with Stub(Collaborator) as stub: stub.method_accepting_property(prop=anything()).returns(2) assert_that(stub.method_accepting_property(prop_stub.prop), is_(2)) assert prop_stub.prop == 5
def test_load_dataset_forced_fallback(self, api_client, dw, dataset_key): when(api_client).download_datapackage(equal_to(dataset_key), anything()).raises(RestApiError) # Forced update dataset = dw.load_dataset(dataset_key, force_update=True) assert_that( api_client.download_datapackage, called().times(1).with_args(equal_to(dataset_key), anything())) assert_that(dataset.raw_data, has_length(3))
def test_inline_stub(self): #Stub()创建free stub inline_stub_free = Stub() #使用when()设置方法参数和返回值 when(inline_stub_free).foo(1).returns("I am inline free stub") assert_that(inline_stub_free.foo(1), is_("I am inline free stub")) #Stub(Collaborator)创建stub inline_stub = Stub(bs.bodyService) # 使用when()设置方法参数和返回值 when(inline_stub).get_height().returns("188cm") assert_that(inline_stub.get_height(), is_("188cm"))
def test_inline_spy(self): #Spy()创建free spy spy_inline_free = Spy() #使用when()设置方法参数和返回值 when(spy_inline_free).foo().returns("I am inline foo") #调用方法 spy_inline_free.foo() #验证调用情况 assert_that(spy_inline_free.foo(), is_("I am inline foo")) assert_that(spy_inline_free.foo, called()) #Spy()创建spy spy_inline = Spy(ss.salaryService) #使用when()设置方法参数 when(spy_inline).set_salary(ANY_ARG) #调用方法 spy_inline.set_salary("12m") #验证调用情况 assert_that(spy_inline.set_salary, called().with_args("12m"))
from expects import expect, equal, have_properties from doublex import Spy, when from agenda_create_action import AgendaCreateAction from agenda_service import AgendaService from agenda_models import Contact A_NAME = 'a_name' A_SURNAME = 'a_surname' A_PHONE_NUMBER = 'a_phone_number' AN_EMAIL = 'an_email' with describe('Agenda create action'): with context('creating a contact'): with it('returns the created contact'): contact = { 'name': A_NAME, 'surname': A_SURNAME, 'phone_number': A_PHONE_NUMBER, 'email': AN_EMAIL } an_agenda_service = Spy(AgendaService) a_contact = Contact(A_NAME, A_SURNAME, A_PHONE_NUMBER, AN_EMAIL) when(an_agenda_service).add(A_NAME, A_SURNAME, A_PHONE_NUMBER, AN_EMAIL).returns(a_contact) create_action = AgendaCreateAction(an_agenda_service) creation = create_action.execute(contact) expect(creation).to(have_properties(name=A_NAME))
with context('POST /events'): with before.each: webhook_server.ACTION = Spy(securecscc.CreateFindingFromEvent) with it('returns a 201'): result = self.app.post('/events', data=fixtures.payload_from_webhook(), content_type='application/json', headers=self.authorization_headers) expect(result.status_code).to(equal(http.client.CREATED)) with it('returns new created finding'): finding = {'id': 'irrelevant id'} when(webhook_server.ACTION).run(fixtures.event_in_webhook()).returns(finding) result = self.app.post('/events', data=fixtures.payload_from_webhook(), content_type='application/json', headers=self.authorization_headers) expect(json.loads(result.data)).to(equal([finding])) with context('when authentication header is not present'): with it('returns a 403'): result = self.app.post('/events', data=fixtures.payload_from_webhook(), content_type='application/json') expect(result.status_code).to(equal(http.client.FORBIDDEN))
result = self.app.get('/health') expect(result.status_code).to(equal(http.client.OK)) with context('POST /events'): with before.each: sysdig_secure_webhook.ACTION = Spy( securecscc.CreateFindingFromEvent) self.finding = securecscc.Finding( finding_id='irrelevant finding id', source='irrelevant source', category='irrelevant category', event_time='irrelevant event_time', ) when(sysdig_secure_webhook.ACTION).run( fixtures.event_in_webhook()).returns(self.finding) with it('returns a 201'): result = self.app.post('/events', data=fixtures.payload_from_webhook(), content_type='application/json', headers=self.authorization_headers) expect(result.status_code).to(equal(http.client.CREATED)) with it('returns new created finding'): result = self.app.post('/events', data=fixtures.payload_from_webhook(), content_type='application/json', headers=self.authorization_headers)
with it('is alive'): result = self.app.get('/health') expect(result.status_code).to(equal(http.client.OK)) with context('POST /events'): with before.each: falco_webhook.ACTION = Spy(securecscc.CreateFindingFromEvent) self.finding = securecscc.Finding( finding_id='irrelevant finding id', source='irrelevant source', category='irrelevant category', event_time='irrelevant event_time', ) when(falco_webhook.ACTION).run(fixtures.event_falco()).returns( self.finding) with it('returns a 201'): result = self.app.post('/events', data=fixtures.payload_from_falco(), content_type='application/json', headers=self.authorization_headers) expect(result.status_code).to(equal(http.client.CREATED)) with it('returns new created finding'): result = self.app.post('/events', data=fixtures.payload_from_falco(), content_type='application/json', headers=self.authorization_headers)
with context('POST /events'): with before.each: falco_server.ACTION = Spy(securecscc.CreateFindingFromEvent) with it('returns a 201'): result = self.app.post('/events', data=fixtures.payload_from_falco(), content_type='application/json', headers=self.authorization_headers) expect(result.status_code).to(equal(http.client.CREATED)) with it('returns new created finding'): finding = {'id': 'irrelevant id'} when(falco_server.ACTION).run(fixtures.event_falco()).returns(finding) result = self.app.post('/events', data=fixtures.payload_from_falco(), content_type='application/json', headers=self.authorization_headers) expect(json.loads(result.data)).to(equal(finding)) with context('when authentication header is not present'): with it('returns a 403'): result = self.app.post('/events', data=fixtures.payload_from_webhook(), content_type='application/json') expect(result.status_code).to(equal(http.client.FORBIDDEN))