File manager - Edit - /home/u478019808/domains/bestandroidphones.store/public_html/static/img/logo/testing.tar
Back
mock_test.py 0000644 00000031573 15025430243 0007115 0 ustar 00 # # Copyright 2015 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for apitools.base.py.testing.mock.""" import unittest import httplib2 import six from apitools.base.protorpclite import messages from apitools.base.py import base_api from apitools.base.py import exceptions from apitools.base.py.testing import mock from samples.fusiontables_sample.fusiontables_v1 import \ fusiontables_v1_client as fusiontables from samples.fusiontables_sample.fusiontables_v1 import \ fusiontables_v1_messages as fusiontables_messages def _GetApiServices(api_client_class): return dict( (name, potential_service) for name, potential_service in six.iteritems(api_client_class.__dict__) if (isinstance(potential_service, type) and issubclass(potential_service, base_api.BaseApiService))) class CustomException(Exception): pass class MockTest(unittest.TestCase): def testMockFusionBasic(self): with mock.Client(fusiontables.FusiontablesV1) as client_class: client_class.column.List.Expect( request=1, response=2, enable_type_checking=False) client = fusiontables.FusiontablesV1(get_credentials=False) self.assertEqual(client.column.List(1), 2) with self.assertRaises(mock.UnexpectedRequestException): client.column.List(3) def testMockFusionException(self): with mock.Client(fusiontables.FusiontablesV1) as client_class: client_class.column.List.Expect( request=1, exception=exceptions.HttpError({'status': 404}, '', ''), enable_type_checking=False) client = fusiontables.FusiontablesV1(get_credentials=False) with self.assertRaises(exceptions.HttpError): client.column.List(1) def testMockFusionTypeChecking(self): with mock.Client(fusiontables.FusiontablesV1) as client_class: messages = client_class.MESSAGES_MODULE client_class.column.List.Expect( messages.FusiontablesColumnListRequest(tableId='foo'), messages.ColumnList(items=[], totalItems=0)) client = fusiontables.FusiontablesV1(get_credentials=False) self.assertEqual( client.column.List( messages.FusiontablesColumnListRequest(tableId='foo')), messages.ColumnList(items=[], totalItems=0)) def testMockFusionTypeCheckingErrors(self): with mock.Client(fusiontables.FusiontablesV1) as client_class: messages = client_class.MESSAGES_MODULE # Wrong request type. with self.assertRaises(exceptions.ConfigurationValueError): client_class.column.List.Expect( messages.FusiontablesColumnInsertRequest(), messages.ColumnList(items=[], totalItems=0)) # Wrong response type. with self.assertRaises(exceptions.ConfigurationValueError): client_class.column.List.Expect( messages.FusiontablesColumnListRequest(tableId='foo'), messages.Column()) # No error if checking is disabled. client_class.column.List.Expect( messages.FusiontablesColumnInsertRequest(), messages.Column(), enable_type_checking=False) client_class.column.List( messages.FusiontablesColumnInsertRequest()) def testMockIfAnotherException(self): with self.assertRaises(CustomException): with mock.Client(fusiontables.FusiontablesV1) as client_class: client_class.column.List.Expect( request=1, response=2, enable_type_checking=False) raise CustomException('Something when wrong') def testMockFusionOrder(self): with mock.Client(fusiontables.FusiontablesV1) as client_class: client_class.column.List.Expect( request=1, response=2, enable_type_checking=False) client_class.column.List.Expect( request=2, response=1, enable_type_checking=False) client = fusiontables.FusiontablesV1(get_credentials=False) self.assertEqual(client.column.List(1), 2) self.assertEqual(client.column.List(2), 1) def testMockFusionWrongOrder(self): with mock.Client(fusiontables.FusiontablesV1) as client_class: client_class.column.List.Expect( request=1, response=2, enable_type_checking=False) client_class.column.List.Expect( request=2, response=1, enable_type_checking=False) client = fusiontables.FusiontablesV1(get_credentials=False) with self.assertRaises(mock.UnexpectedRequestException): self.assertEqual(client.column.List(2), 1) with self.assertRaises(mock.UnexpectedRequestException): self.assertEqual(client.column.List(1), 2) def testMockFusionTooMany(self): with mock.Client(fusiontables.FusiontablesV1) as client_class: client_class.column.List.Expect( request=1, response=2, enable_type_checking=False) client = fusiontables.FusiontablesV1(get_credentials=False) self.assertEqual(client.column.List(1), 2) with self.assertRaises(mock.UnexpectedRequestException): self.assertEqual(client.column.List(2), 1) def testMockFusionTooFew(self): with self.assertRaises(mock.ExpectedRequestsException): with mock.Client(fusiontables.FusiontablesV1) as client_class: client_class.column.List.Expect( request=1, response=2, enable_type_checking=False) client_class.column.List.Expect( request=2, response=1, enable_type_checking=False) client = fusiontables.FusiontablesV1(get_credentials=False) self.assertEqual(client.column.List(1), 2) def testFusionUnmock(self): with mock.Client(fusiontables.FusiontablesV1): client = fusiontables.FusiontablesV1(get_credentials=False) mocked_service_type = type(client.column) client = fusiontables.FusiontablesV1(get_credentials=False) self.assertNotEqual(type(client.column), mocked_service_type) def testRequestMacher(self): class Matcher(object): def __init__(self, eq): self._eq = eq def __eq__(self, other): return self._eq(other) with mock.Client(fusiontables.FusiontablesV1) as client_class: def IsEven(x): return x % 2 == 0 def IsOdd(x): return not IsEven(x) client_class.column.List.Expect( request=Matcher(IsEven), response=1, enable_type_checking=False) client_class.column.List.Expect( request=Matcher(IsOdd), response=2, enable_type_checking=False) client_class.column.List.Expect( request=Matcher(IsEven), response=3, enable_type_checking=False) client_class.column.List.Expect( request=Matcher(IsOdd), response=4, enable_type_checking=False) client = fusiontables.FusiontablesV1(get_credentials=False) self.assertEqual(client.column.List(2), 1) self.assertEqual(client.column.List(1), 2) self.assertEqual(client.column.List(20), 3) self.assertEqual(client.column.List(23), 4) def testClientUnmock(self): mock_client = mock.Client(fusiontables.FusiontablesV1) self.assertFalse(isinstance(mock_client, fusiontables.FusiontablesV1)) attributes = set(mock_client.__dict__.keys()) mock_client = mock_client.Mock() self.assertTrue(isinstance(mock_client, fusiontables.FusiontablesV1)) self.assertTrue(set(mock_client.__dict__.keys()) - attributes) mock_client.Unmock() self.assertFalse(isinstance(mock_client, fusiontables.FusiontablesV1)) self.assertEqual(attributes, set(mock_client.__dict__.keys())) def testMockHasMessagesModule(self): with mock.Client(fusiontables.FusiontablesV1) as mock_client: self.assertEqual(fusiontables_messages, mock_client.MESSAGES_MODULE) def testMockHasUrlProperty(self): with mock.Client(fusiontables.FusiontablesV1) as mock_client: self.assertEqual(fusiontables.FusiontablesV1.BASE_URL, mock_client.url) self.assertFalse(hasattr(mock_client, 'url')) def testMockHasOverrideUrlProperty(self): real_client = fusiontables.FusiontablesV1(url='http://localhost:8080', get_credentials=False) with mock.Client(fusiontables.FusiontablesV1, real_client) as mock_client: self.assertEqual('http://localhost:8080/', mock_client.url) def testMockHasHttpProperty(self): with mock.Client(fusiontables.FusiontablesV1) as mock_client: self.assertIsInstance(mock_client.http, httplib2.Http) self.assertFalse(hasattr(mock_client, 'http')) def testMockHasOverrideHttpProperty(self): real_client = fusiontables.FusiontablesV1(url='http://localhost:8080', http='SomeHttpObject', get_credentials=False) with mock.Client(fusiontables.FusiontablesV1, real_client) as mock_client: self.assertEqual('SomeHttpObject', mock_client.http) def testMockPreservesServiceMethods(self): services = _GetApiServices(fusiontables.FusiontablesV1) with mock.Client(fusiontables.FusiontablesV1): mocked_services = _GetApiServices(fusiontables.FusiontablesV1) self.assertEqual(services.keys(), mocked_services.keys()) for name, service in six.iteritems(services): mocked_service = mocked_services[name] methods = service.GetMethodsList() for method in methods: mocked_method = getattr(mocked_service, method) mocked_method_config = mocked_method.method_config() method_config = getattr(service, method).method_config() self.assertEqual(method_config, mocked_method_config) class _NestedMessage(messages.Message): nested = messages.StringField(1) class _NestedListMessage(messages.Message): nested_list = messages.MessageField(_NestedMessage, 1, repeated=True) class _NestedNestedMessage(messages.Message): nested = messages.MessageField(_NestedMessage, 1) class UtilTest(unittest.TestCase): def testMessagesEqual(self): self.assertFalse(mock._MessagesEqual( _NestedNestedMessage( nested=_NestedMessage( nested='foo')), _NestedNestedMessage( nested=_NestedMessage( nested='bar')))) self.assertTrue(mock._MessagesEqual( _NestedNestedMessage( nested=_NestedMessage( nested='foo')), _NestedNestedMessage( nested=_NestedMessage( nested='foo')))) def testListedMessagesEqual(self): self.assertTrue(mock._MessagesEqual( _NestedListMessage( nested_list=[_NestedMessage(nested='foo')]), _NestedListMessage( nested_list=[_NestedMessage(nested='foo')]))) self.assertTrue(mock._MessagesEqual( _NestedListMessage( nested_list=[_NestedMessage(nested='foo'), _NestedMessage(nested='foo2')]), _NestedListMessage( nested_list=[_NestedMessage(nested='foo'), _NestedMessage(nested='foo2')]))) self.assertFalse(mock._MessagesEqual( _NestedListMessage( nested_list=[_NestedMessage(nested='foo')]), _NestedListMessage( nested_list=[_NestedMessage(nested='bar')]))) self.assertFalse(mock._MessagesEqual( _NestedListMessage( nested_list=[_NestedMessage(nested='foo')]), _NestedListMessage( nested_list=[_NestedMessage(nested='foo'), _NestedMessage(nested='foo')]))) mock.py 0000644 00000034706 15025430243 0006057 0 ustar 00 # # Copyright 2015 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The mock module allows easy mocking of apitools clients. This module allows you to mock out the constructor of a particular apitools client, for a specific API and version. Then, when the client is created, it will be run against an expected session that you define. This way code that is not aware of the testing framework can construct new clients as normal, as long as it's all done within the context of a mock. """ import difflib import sys import six from apitools.base.protorpclite import messages from apitools.base.py import base_api from apitools.base.py import encoding from apitools.base.py import exceptions class Error(Exception): """Exceptions for this module.""" def _MessagesEqual(msg1, msg2): """Compare two protorpc messages for equality. Using python's == operator does not work in all cases, specifically when there is a list involved. Args: msg1: protorpc.messages.Message or [protorpc.messages.Message] or number or string, One of the messages to compare. msg2: protorpc.messages.Message or [protorpc.messages.Message] or number or string, One of the messages to compare. Returns: If the messages are isomorphic. """ if isinstance(msg1, list) and isinstance(msg2, list): if len(msg1) != len(msg2): return False return all(_MessagesEqual(x, y) for x, y in zip(msg1, msg2)) if (not isinstance(msg1, messages.Message) or not isinstance(msg2, messages.Message)): return msg1 == msg2 for field in msg1.all_fields(): field1 = getattr(msg1, field.name) field2 = getattr(msg2, field.name) if not _MessagesEqual(field1, field2): return False return True class UnexpectedRequestException(Error): def __init__(self, received_call, expected_call): expected_key, expected_request = expected_call received_key, received_request = received_call expected_repr = encoding.MessageToRepr( expected_request, multiline=True) received_repr = encoding.MessageToRepr( received_request, multiline=True) expected_lines = expected_repr.splitlines() received_lines = received_repr.splitlines() diff_lines = difflib.unified_diff(expected_lines, received_lines) diff = '\n'.join(diff_lines) if expected_key != received_key: msg = '\n'.join(( 'expected: {expected_key}({expected_request})', 'received: {received_key}({received_request})', '', )).format( expected_key=expected_key, expected_request=expected_repr, received_key=received_key, received_request=received_repr) super(UnexpectedRequestException, self).__init__(msg) else: msg = '\n'.join(( 'for request to {key},', 'expected: {expected_request}', 'received: {received_request}', 'diff: {diff}', '', )).format( key=expected_key, expected_request=expected_repr, received_request=received_repr, diff=diff) super(UnexpectedRequestException, self).__init__(msg) class ExpectedRequestsException(Error): def __init__(self, expected_calls): msg = 'expected:\n' for (key, request) in expected_calls: msg += '{key}({request})\n'.format( key=key, request=encoding.MessageToRepr(request, multiline=True)) super(ExpectedRequestsException, self).__init__(msg) class _ExpectedRequestResponse(object): """Encapsulation of an expected request and corresponding response.""" def __init__(self, key, request, response=None, exception=None): self.__key = key self.__request = request if response and exception: raise exceptions.ConfigurationValueError( 'Should specify at most one of response and exception') if response and isinstance(response, exceptions.Error): raise exceptions.ConfigurationValueError( 'Responses should not be an instance of Error') if exception and not isinstance(exception, exceptions.Error): raise exceptions.ConfigurationValueError( 'Exceptions must be instances of Error') self.__response = response self.__exception = exception @property def key(self): return self.__key @property def request(self): return self.__request def ValidateAndRespond(self, key, request): """Validate that key and request match expectations, and respond if so. Args: key: str, Actual key to compare against expectations. request: protorpc.messages.Message or [protorpc.messages.Message] or number or string, Actual request to compare againt expectations Raises: UnexpectedRequestException: If key or request dont match expectations. apitools_base.Error: If a non-None exception is specified to be thrown. Returns: The response that was specified to be returned. """ if key != self.__key or not (self.__request == request or _MessagesEqual(request, self.__request)): raise UnexpectedRequestException((key, request), (self.__key, self.__request)) if self.__exception: # Can only throw apitools_base.Error. raise self.__exception # pylint: disable=raising-bad-type return self.__response class _MockedMethod(object): """A mocked API service method.""" def __init__(self, key, mocked_client, real_method): self.__name__ = real_method.__name__ self.__key = key self.__mocked_client = mocked_client self.__real_method = real_method self.method_config = real_method.method_config config = self.method_config() self.__request_type = getattr(self.__mocked_client.MESSAGES_MODULE, config.request_type_name) self.__response_type = getattr(self.__mocked_client.MESSAGES_MODULE, config.response_type_name) def _TypeCheck(self, msg, is_request): """Ensure the given message is of the expected type of this method. Args: msg: The message instance to check. is_request: True to validate against the expected request type, False to validate against the expected response type. Raises: exceptions.ConfigurationValueError: If the type of the message was not correct. """ if is_request: mode = 'request' real_type = self.__request_type else: mode = 'response' real_type = self.__response_type if not isinstance(msg, real_type): raise exceptions.ConfigurationValueError( 'Expected {} is not of the correct type for method [{}].\n' ' Required: [{}]\n' ' Given: [{}]'.format( mode, self.__key, real_type, type(msg))) def Expect(self, request, response=None, exception=None, enable_type_checking=True, **unused_kwargs): """Add an expectation on the mocked method. Exactly one of response and exception should be specified. Args: request: The request that should be expected response: The response that should be returned or None if exception is provided. exception: An exception that should be thrown, or None. enable_type_checking: When true, the message type of the request and response (if provided) will be checked against the types required by this method. """ # TODO(jasmuth): the unused_kwargs provides a placeholder for # future things that can be passed to Expect(), like special # params to the method call. # Ensure that the registered request and response mocks actually # match what this method accepts and returns. if enable_type_checking: self._TypeCheck(request, is_request=True) if response: self._TypeCheck(response, is_request=False) # pylint: disable=protected-access # Class in same module. self.__mocked_client._request_responses.append( _ExpectedRequestResponse(self.__key, request, response=response, exception=exception)) # pylint: enable=protected-access def __call__(self, request, **unused_kwargs): # TODO(jasmuth): allow the testing code to expect certain # values in these currently unused_kwargs, especially the # upload parameter used by media-heavy services like bigquery # or bigstore. # pylint: disable=protected-access # Class in same module. if self.__mocked_client._request_responses: request_response = self.__mocked_client._request_responses.pop(0) else: raise UnexpectedRequestException( (self.__key, request), (None, None)) # pylint: enable=protected-access response = request_response.ValidateAndRespond(self.__key, request) if response is None and self.__real_method: response = self.__real_method(request) print(encoding.MessageToRepr( response, multiline=True, shortstrings=True)) return response return response def _MakeMockedService(api_name, collection_name, mock_client, service, real_service): class MockedService(base_api.BaseApiService): pass for method in service.GetMethodsList(): real_method = None if real_service: real_method = getattr(real_service, method) setattr(MockedService, method, _MockedMethod(api_name + '.' + collection_name + '.' + method, mock_client, real_method)) return MockedService class Client(object): """Mock an apitools client.""" def __init__(self, client_class, real_client=None): """Mock an apitools API, given its class. Args: client_class: The class for the API. eg, if you from apis.sqladmin import v1beta3 then you can pass v1beta3.SqladminV1beta3 to this class and anything within its context will use your mocked version. real_client: apitools Client, The client to make requests against when the expected response is None. """ if not real_client: real_client = client_class(get_credentials=False) self.__orig_class = self.__class__ self.__client_class = client_class self.__real_service_classes = {} self.__real_client = real_client self._request_responses = [] self.__real_include_fields = None def __enter__(self): return self.Mock() def Mock(self): """Stub out the client class with mocked services.""" client = self.__real_client or self.__client_class( get_credentials=False) class Patched(self.__class__, self.__client_class): pass self.__class__ = Patched for name in dir(self.__client_class): service_class = getattr(self.__client_class, name) if not isinstance(service_class, type): continue if not issubclass(service_class, base_api.BaseApiService): continue self.__real_service_classes[name] = service_class # pylint: disable=protected-access collection_name = service_class._NAME # pylint: enable=protected-access api_name = '%s_%s' % (self.__client_class._PACKAGE, self.__client_class._URL_VERSION) mocked_service_class = _MakeMockedService( api_name, collection_name, self, service_class, service_class(client) if self.__real_client else None) setattr(self.__client_class, name, mocked_service_class) setattr(self, collection_name, mocked_service_class(self)) self.__real_include_fields = self.__client_class.IncludeFields self.__client_class.IncludeFields = self.IncludeFields # pylint: disable=attribute-defined-outside-init self._url = client._url self._http = client._http return self def __exit__(self, exc_type, value, traceback): is_active_exception = value is not None self.Unmock(suppress=is_active_exception) if is_active_exception: six.reraise(exc_type, value, traceback) return True def Unmock(self, suppress=False): self.__class__ = self.__orig_class for name, service_class in self.__real_service_classes.items(): setattr(self.__client_class, name, service_class) delattr(self, service_class._NAME) self.__real_service_classes = {} del self._url del self._http self.__client_class.IncludeFields = self.__real_include_fields self.__real_include_fields = None requests = [(rq_rs.key, rq_rs.request) for rq_rs in self._request_responses] self._request_responses = [] if requests and not suppress and sys.exc_info()[1] is None: raise ExpectedRequestsException(requests) def IncludeFields(self, include_fields): if self.__real_client: return self.__real_include_fields(self.__real_client, include_fields) __init__.py 0000644 00000001135 15025430243 0006653 0 ustar 00 # # Copyright 2015 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Package marker file."""
| ver. 1.4 |
Github
|
.
| PHP 8.2.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings