def test_aggregated_list_rest( transport: str = "rest", request_type=compute.AggregatedListDiskTypesRequest): client = DiskTypesClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # Designate an appropriate value for the returned response. return_value = compute.DiskTypeAggregatedList( id="id_value", items={ "key_value": compute.DiskTypesScopedList(disk_types=[ compute.DiskType( creation_timestamp="creation_timestamp_value") ]) }, kind="kind_value", next_page_token="next_page_token_value", self_link="self_link_value", unreachables=["unreachables_value"], warning=compute.Warning(code=compute.Warning.Code.CLEANUP_FAILED), ) # Wrap the value into a proper Response obj json_return_value = compute.DiskTypeAggregatedList.to_json( return_value) response_value = Response() response_value.status_code = 200 response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value response = client.aggregated_list(request) # Establish that the response is the type that we expect. assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.DiskTypesScopedList(disk_types=[ compute.DiskType(creation_timestamp="creation_timestamp_value") ]) } assert response.kind == "kind_value" assert response.next_page_token == "next_page_token_value" assert response.self_link == "self_link_value" assert response.unreachables == ["unreachables_value"] assert response.warning == compute.Warning( code=compute.Warning.Code.CLEANUP_FAILED)
def test_get_rest_flattened(): client = DiskTypesClient( credentials=ga_credentials.AnonymousCredentials(), ) # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # Designate an appropriate value for the returned response. return_value = compute.DiskType() # Wrap the value into a proper Response obj json_return_value = compute.DiskType.to_json(return_value) response_value = Response() response_value.status_code = 200 response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. client.get( project="project_value", zone="zone_value", disk_type="disk_type_value", ) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert "disk_type_value" in http_call[1] + str(body)
def test_get_rest(transport: str = "rest", request_type=compute.GetDiskTypeRequest): client = DiskTypesClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # Designate an appropriate value for the returned response. return_value = compute.DiskType( creation_timestamp="creation_timestamp_value", default_disk_size_gb="default_disk_size_gb_value", deprecated=compute.DeprecationStatus(deleted="deleted_value"), description="description_value", id="id_value", kind="kind_value", name="name_value", region="region_value", self_link="self_link_value", valid_disk_size="valid_disk_size_value", zone="zone_value", ) # Wrap the value into a proper Response obj json_return_value = compute.DiskType.to_json(return_value) response_value = Response() response_value.status_code = 200 response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value response = client.get(request) # Establish that the response is the type that we expect. assert isinstance(response, compute.DiskType) assert response.creation_timestamp == "creation_timestamp_value" assert response.default_disk_size_gb == "default_disk_size_gb_value" assert response.deprecated == compute.DeprecationStatus( deleted="deleted_value") assert response.description == "description_value" assert response.id == "id_value" assert response.kind == "kind_value" assert response.name == "name_value" assert response.region == "region_value" assert response.self_link == "self_link_value" assert response.valid_disk_size == "valid_disk_size_value" assert response.zone == "zone_value"
def test_list_pager(): client = RegionDiskTypesClient( credentials=ga_credentials.AnonymousCredentials(), ) # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # Set the response as a series of pages response = ( compute.RegionDiskTypeList( items=[ compute.DiskType(), compute.DiskType(), compute.DiskType(), ], next_page_token="abc", ), compute.RegionDiskTypeList( items=[], next_page_token="def", ), compute.RegionDiskTypeList( items=[ compute.DiskType(), ], next_page_token="ghi", ), compute.RegionDiskTypeList(items=[ compute.DiskType(), compute.DiskType(), ], ), ) # Two responses for two calls response = response + response # Wrap the values into proper Response objs response = tuple( compute.RegionDiskTypeList.to_json(x) for x in response) return_values = tuple(Response() for i in response) for return_val, response_val in zip(return_values, response): return_val._content = response_val.encode("UTF-8") return_val.status_code = 200 req.side_effect = return_values metadata = () pager = client.list(request={}) assert pager._metadata == metadata results = list(pager) assert len(results) == 6 assert all(isinstance(i, compute.DiskType) for i in results) pages = list(client.list(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token