def test_get_avl_cabs_fail(self, inputs): session = MockSession() session.query('').filter('')._all = self.dummy_routes_data inputs.side_effect = ['A', 'E', 8] test_class = Register() allow(test_class).show_avl_cabs.return_value(True) assert test_class.get_avl_cab(self.emp_id, session) is False
def test_and_return_future_multiple_values(self): allow(self.subject).instance_method.and_return_future('Bob Barker', 'Drew Carey') result1 = self.subject.instance_method() result2 = self.subject.instance_method() assert result1.result() == 'Bob Barker' assert result2.result() == 'Drew Carey'
def test_process_google_token_invalid_token(): allow(id_token).verify_oauth2_token.and_raise( ValueError('Token Error')) with pytest.raises(APIBaseException) as exec_info: SessionService.process_google_token('valid_token') assert exec_info.value.detail.code == AccountsErrorCodes.INVALID_GOOGLE_TOKEN_ID.code
def test_passes_when_called_more_than_at_least_times(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.at_least(1).times subject.instance_method() subject.instance_method()
def test_calls_are_chainable(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.at_most(1).times.at_most(2).times subject.instance_method() subject.instance_method()
def test_custom_alias(self, git): allow(requests).post.and_return(support.issue_create()) runner = CliRunner() result = runner.invoke( cli.cli, ['custom', 'project.id', '1', 'summary', 'slug me']) assert result.exit_code == 0 assert result.output == u'\x1b(0lqqqqqqqwqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk\x1b(B\n\x1b(0x\x1b(B id \x1b(0x\x1b(B key \x1b(0x\x1b(B self \x1b(0x\x1b(B\n\x1b(0tqqqqqqqnqqqqqqqqnqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu\x1b(B\n\x1b(0x\x1b(B 10000 \x1b(0x\x1b(B TST-24 \x1b(0x\x1b(B http://www.example.com/jira/rest/api/2/issue/10000 \x1b(0x\x1b(B\n\x1b(0mqqqqqqqvqqqqqqqqvqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj\x1b(B\n'
def test_matches_most_specific_allowance(self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_varargs.and_return('bar') allow(subject).method_with_varargs.with_args('baz').and_return('blah') assert subject.method_with_varargs('baz') == 'blah'
def test_stubs_constructors(self): with no_builtin_verification(): user = object() allow(UserWithCustomNew).__new__.and_return(user) assert UserWithCustomNew('Alice', 25) is user
def test_callable_instance_attribute(self, test_class): user = test_class('Alice', 25) allow(user).callable_instance_attribute.and_return('Bob Barker') assert user.callable_instance_attribute() == 'Bob Barker' teardown() assert user.callable_instance_attribute() == 'dummy result'
def test_raises_when_stubbing_instance_methods(self): User = ClassDouble('doubles.testing.User') with raises(VerifyingDoubleError) as e: allow(User).instance_method assert re.search(r"not callable directly on", str(e))
def test_restores_class_methods_on_teardown(self, test_class): allow(test_class).class_method.and_return('overridden value') teardown() assert test_class.class_method( 'foo') == 'class_method return value: foo'
def test_allows_stubs_on_existing_methods(self): with no_builtin_verification(): date = InstanceDouble('datetime.date') allow(date).ctime assert date.ctime() is None
def test_mocking__enter__and__exit__works(self): user = InstanceDouble('doubles.testing.User') allow(user).__enter__.and_return('bob barker') allow(user).__exit__ with user as u: assert u == 'bob barker'
def test_airport_take_off(): airport = Airport() weather = Weather() allow(weather).weather_check.and_return('sunny') airport.land("plane") airport.take_off("plane") assert airport.hangar == []
def test_restores_instance_methods_on_teardown(self, test_class): user = test_class('Alice', 25) allow(user).get_name.and_return('Bob') teardown() assert user.get_name() == 'Alice'
def test_addcomment(self): allow(requests).post.and_return(support.comment()) runner = CliRunner() result = runner.invoke(cli.cli, ['issue', 'DEV-101', 'addcomment', 'A comment']) assert result.exit_code == 0 assert result.output == u'\x1b(0lqqqqqqqqqqqqqwqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk\x1b(B\n\x1b(0x\x1b(B author_name \x1b(0x\x1b(B body \x1b(0x\x1b(B updated \x1b(0x\x1b(B\n\x1b(0tqqqqqqqqqqqqqnqqqqqqqqqqqqqnqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu\x1b(B\n\x1b(0x\x1b(B fred \x1b(0x\x1b(B YUUUGE bug. \x1b(0x\x1b(B 2016-05-18T12:19:03.615+0000 \x1b(0x\x1b(B\n\x1b(0mqqqqqqqqqqqqqvqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj\x1b(B\n'
def test_stubing_property_with_args_raises(self, test_class): user = test_class('Alice', 25) with raises(VerifyingDoubleArgumentError) as e: allow(user).some_property.with_args(1) assert str(e.value) == 'Properties do not accept arguments.'
def test_record(vcr_service, cassette, call, mock_server, use_known_peers): allow(cassette).can_replay.and_return(False) expect(cassette).record.with_args( proxy.Request( serviceName='service', endpoint='endpoint', headers='headers', body='body', knownPeers=[mock_server.hostport] if use_known_peers else [], hostPort='' if use_known_peers else mock_server.hostport, ), proxy.Response( code=0, headers='response headers', body='response body', ), ) mock_server.expect_call('endpoint').and_write( headers='response headers', body='response body', ).once() response = yield call( service='service', endpoint='endpoint', headers='headers', body='body', ) assert response.body.headers == 'response headers' assert response.body.body == 'response body'
def test__construct_query_for_partition(self, boundaries, expected_query, extractor): allow(extractor)._get_boundaries_for_import.and_return(boundaries) actual_query = extractor._construct_query_for_partition( extractor.partition) expected_query = " ".join(expected_query.split()) assert actual_query == expected_query
def test_passes_when_called_twice(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.twice() subject.instance_method() subject.instance_method()
def test_skip_builtin_verification_does_not_affect_non_builtins(self): with no_builtin_verification(): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method with raises(VerifyingDoubleArgumentError): subject.instance_method('bar')
def test_process_google_token_invalid_token(): allow(id_token).verify_oauth2_token.and_raise( ValueError('Token Error')) with pytest.raises(NotAuthenticated) as exec_info: SessionService.process_google_token('valid_token') assert exec_info.value.detail.code == INVALID_GOOGLE_TOKEN_ID['code']
def test_matcher_raises_an_exception(self): def func(): raise Exception('Bob Barker') allow(self.subject).instance_method.with_args_validator(func) with raises(UnallowedMethodCallError): self.subject.instance_method()
def test_unexpected_error(vcr_service, cassette, call): allow(cassette).can_replay.and_raise(SomeException("great sadness")) with pytest.raises(VCRProxy.VCRServiceError) as exc_info: yield call('endpoint', 'body') assert 'great sadness' in str(exc_info)
def test_instance_exception(): some_rw = InstanceDouble('tchannel.rw.ReadWriter') c_rw = rw.instance(ClassWithArgs, ('x', some_rw), ('y', rw.number(4))) allow(some_rw).read.and_raise(ReadError("great sadness")) with pytest.raises(ReadError): c_rw.read(bio([1, 2, 3, 4]))
def test_allows_any_arguments_if_none_are_specified(self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_positional_arguments.and_return('bar') assert subject.method_with_positional_arguments( 'unspecified argument') == 'bar'
def test_dictionary_read_error(): some_rw = InstanceDouble('tchannel.rw.ReadWriter') allow(some_rw).read.and_raise(ReadError('great sadness')) dict_rw = rw.dictionary(('foo', some_rw)) with pytest.raises(ReadError): dict_rw.read(BytesIO())
def test_matcher_with_kwargs_args_returns_true(self): def func(bar=None): return True allow(self.subject).instance_method.with_args_validator( func).and_return('Bob') self.subject.instance_method() == 'Bob'
def test_matcher_with_positional_and_kwargs_returns_true(self): def func(foo, bar=None): return True allow(self.subject).method_with_default_args.with_args_validator( func).and_return('Bob') self.subject.method_with_default_args('bob', bar='Barker') == 'Bob'
def test_matcher_with_kwargs_args_returns_false(self): def func(bar=None): return False allow(self.subject).instance_method.with_args_validator(func) with raises(UnallowedMethodCallError): self.subject.instance_method()
def test_allows_stubs_on_existing_class_methods(self): with no_builtin_verification(): date = InstanceDouble('datetime.date') allow(date).today.with_args() assert date.today() is None
def test_caches_result(self, category): allow(category)._generate_child_pages.and_return( [dummy_page(slug='page-1')], None, ) original = category.children assert original is category.children
def test_does_not_returns_drafts_if_not_include_drafts(self, category): category.site.include_drafts = False allow(category)._generate_child_pages.and_return([ dummy_page(slug='page-1', published=False), dummy_page(slug='page-2', published=True), ]) assert category.children[0].slug == 'page-2'
def test_record(vcr_service, cassette, call, mock_server): allow(cassette).can_replay.and_return(False) expect(cassette).record.with_args( VCRProxy.Request( serviceName='service', endpoint='endpoint', headers='headers', body='body', hostPort=mock_server.hostport, ), VCRProxy.Response(0, 'response headers', 'response body'), ) mock_server.expect_call('endpoint').and_write( headers='response headers', body='response body', ).once() response = yield call( service='service', endpoint='endpoint', headers='headers', body='body', ) assert response.headers == 'response headers' assert response.body == 'response body'
def test_write_protected(vcr_service, cassette, call): cassette.record_mode = 'none' cassette.write_protected = True allow(cassette).can_replay.and_return(False) with pytest.raises(VCRProxy.CannotRecordInteractionsError): yield call('endpoint', 'request body')
def test_write_protected(vcr_service, cassette, call): cassette.record_mode = 'none' cassette.write_protected = True allow(cassette).can_replay.and_return(False) with pytest.raises(proxy.CannotRecordInteractionsError): yield call('endpoint', 'request body')
def test_unexpected_error(vcr_service, cassette, call): allow(cassette).can_replay.and_raise(SomeException("great sadness")) with pytest.raises(proxy.VCRServiceError) as exc_info: yield call('endpoint', 'body') assert 'great sadness' in str(exc_info)
def test_raises_if_incorrect_call_signature_used(self): with raises(VerifyingDoubleArgumentError): allow(doubles.testing).top_level_function.with_args( 'bob', 'barker', 'is_great' )
def test_proxies_docstring(self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_doc assert subject.method_with_doc.__doc__ == ( """A basic method of OldStyleUser to illustrate existance of a docstring""" )
def test_returns_not_found_when_gmaps_returns_no_results(self): gmapsMock = InstanceDouble("googlemaps.Client") allow(gmapsMock).geocode("No loc", bounds=self.SF_BOUNDS).and_return([]) geocoder = MovieLocationGeocoder(gmapsMock) res = geocoder.geocode("No loc") assert not res.found
def test_does_not_intefere_with_unstubbed_objects(self, test_class): alice = test_class('Alice', 25) bob = test_class('Bob', 25) allow(alice).__exit__.and_return('user') assert alice.__exit__(None, None, None) == 'user' assert bob.__exit__(None, None, None) is None
def test_does_not_intefere_with_unstubbed_objects(self, test_class): alice = test_class('Alice', 25) peter = test_class('Peter', 25) allow(alice).__call__.and_return('alice') assert alice() == 'alice' assert peter() == 'user was called'
def test_stubbing_property_does_not_affect_other_instances(self, test_class): user_1 = test_class('Bob', 25) user_2 = test_class('Drew', 25) allow(user_1).some_property.and_return('Barker') assert user_1.some_property == 'Barker' assert user_2.some_property == 'some_property return value'
def test_allow_can_fail(): test_object = TestClass() allow(test_object).method.with_args( any_string.containing('todd') ) with pytest.raises(UnallowedMethodCallError): test_object.method('bob barker')