def test_with_mocks(mock_marshal_param): del getPetById_spec['parameters'][0]['required'] getPetById_spec['parameters'][0]['default'] = 99 request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) op.construct_params(request_dict, op_kwargs={}) assert 1 == mock_marshal_param.call_count
def test_with_timeouts(_1, minimal_swagger_spec, getPetById_spec, request_dict, timeout_kv): request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) key, value = timeout_kv request = op.construct_request(petId=34, _request_options={key: value}) assert request[key] == value
def test_required_parameter_missing( minimal_swagger_spec, getPetById_spec, request_dict): request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) with pytest.raises(SwaggerMappingError) as excinfo: op.construct_params(request_dict, op_kwargs={}) assert 'required parameter' in str(excinfo.value)
def test_with_mocks(mock_marshal_param): get_op = minimal_swagger_spec.spec_dict['paths']['/pet/{petId}']['get'] del get_op['parameters'][0] op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', {})) op.construct_params(request_dict, op_kwargs={}) assert 0 == mock_marshal_param.call_count assert 0 == len(request_dict)
def test_simple(mock_marshal_param, minimal_swagger_spec, getPetById_spec, request_dict): request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) construct_params(op, request_dict, op_kwargs={'petId': 34, 'api_key': 'foo'}) assert 2 == mock_marshal_param.call_count
def command_request(self, method, path): """ Returns a callable request for a given http method and API path. You can then use this request to execute the command, and get the response value: >>> rqst = client.command_request('get', '/api/hcl') >>> resp, ok = rqst() Parameters ---------- method : str the http method value, ['get', 'put', 'post', ...] path : str the API route string value, for example: "/api/resources/vlan-pools/{id}" Returns ------- Request The request instance you can then use to exeute the command. """ op = self.client.swagger_spec.get_op_for_request(method, path) if not op: raise RuntimeError('no command found for (%s, %s)' % (method, path)) return Request(self.client, CallableOperation(op))
def test_required_parameter_missing( minimal_swagger_spec, getPetById_spec, request_dict): request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) with pytest.raises(SwaggerMappingError) as excinfo: construct_params(op, request_dict, op_kwargs={}) assert 'required parameter' in str(excinfo.value)
def test_no_params(mock_marshal_param, minimal_swagger_spec, request_dict): get_op = minimal_swagger_spec.spec_dict['paths']['/pet/{petId}']['get'] del get_op['parameters'][0] op = CallableOperation( Operation.from_spec(minimal_swagger_spec, '/pet/{petId}', 'get', {})) construct_params(op, request_dict, op_kwargs={}) assert 0 == mock_marshal_param.call_count assert 0 == len(request_dict)
def test_validate_header_parameter_from_request_options( mock_marshal_param, minimal_swagger_spec, getPetById_spec, request_dict): request_dict['url'] = '/pet/{petId}' request_dict['headers']['api_key'] = 'api_key' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'delete', getPetById_spec)) construct_params(op, request_dict, op_kwargs={'petId': 1}) assert 2 == mock_marshal_param.call_count
def test_with_timeouts( mock_marshal_param, minimal_swagger_spec, getPetById_spec, request_dict, timeout_kv, ): request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) k, v = timeout_kv request = construct_request(op, request_options={k: v}, petId=34, api_key='foo') assert request[k] == v assert mock_marshal_param.call_count == 2
def test_non_required_parameter_with_default_used( mock_marshal_param, minimal_swagger_spec, getPetById_spec, request_dict): del getPetById_spec['parameters'][0]['required'] getPetById_spec['parameters'][0]['default'] = 99 request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) construct_params(op, request_dict, op_kwargs={'api_key': 'foo'}) assert 2 == mock_marshal_param.call_count
def test_use_msgpack( minimal_swagger_spec, getPetById_spec, ): op = CallableOperation( Operation.from_spec(minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) request = construct_request( op, request_options={ 'use_msgpack': True, }, petId=1, ) assert request['headers']['Accept'] == 'application/msgpack'
def test_use_msgpack( minimal_swagger_spec, getPetById_spec, ): op = CallableOperation( Operation.from_spec(minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) request_options = { 'use_msgpack': True, 'headers': { 'Some-Header': 'header-value' } } # type: Dict[str, Any] request = construct_request( op, request_options=request_options, petId=1, ) assert request['headers']['Accept'] == 'application/msgpack' assert request['headers']['Some-Header'] == 'header-value', \ "Requested header should be present" assert 'Accept' not in request_options['headers'], \ "Original request options should not be modified"
def test_extra_parameter_error(minimal_swagger_spec, request_dict): op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', {})) with pytest.raises(SwaggerMappingError) as excinfo: op.construct_params(request_dict, op_kwargs={'extra_param': 'bar'}) assert 'does not have parameter' in str(excinfo.value)
def test_success(minimal_swagger_spec, getPetById_spec, request_dict): request_dict['url'] = '/pet/{petId}' op = CallableOperation( Operation.from_spec(minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) assert op.__doc__.startswith('[GET] Find pet by ID')
def _make_ops_map(self): ops = {} for res_name, res in self._api.swagger_spec.resources.items(): for op_name, op in res.operations.items(): ops[op_name] = ResponseWrapper(CallableOperation(op)) return ops
def test_with_mocks(mock_marshal_param): request_dict['url'] = '/pet/{petId}' op = CallableOperation(Operation.from_spec( minimal_swagger_spec, '/pet/{petId}', 'get', getPetById_spec)) op.construct_params(request_dict, op_kwargs={'petId': 34}) assert 1 == mock_marshal_param.call_count
def test_extra_parameter_error(minimal_swagger_spec, request_dict): op = CallableOperation( Operation.from_spec(minimal_swagger_spec, '/pet/{petId}', 'get', {})) with pytest.raises(SwaggerMappingError) as excinfo: construct_params(op, request_dict, op_kwargs={'extra_param': 'bar'}) assert 'does not have parameter' in str(excinfo.value)