def _ExpectList(self, registries, batch_size=100, limit=None): """Create expected List() call(s). Based on the number of registries and batching parameters. Args: registries: list of DeviceRegistry batch_size: int, the number of results in each page limit: int or None, the total number of registries to limit """ if limit: registries = registries[:limit] slices, token_pairs = list_slicer.SliceList(registries, batch_size) for slice_, (current_token, next_token) in zip(slices, token_pairs): self.client.projects_locations_registries.List.Expect( self.messages.CloudiotProjectsLocationsRegistriesListRequest( parent='projects/{}/locations/us-central1'.format( self.Project()), pageToken=current_token, pageSize=batch_size), self.messages.ListDeviceRegistriesResponse( deviceRegistries=registries[slice_], nextPageToken=next_token))
def testEmptyList(self): list_ = [] page_size = 6 expected_slices_list = [] expect_token_pairs_list = [] actual_slices, actual_token_pairs = list_slicer.SliceList( list_, page_size) self.assertEqual((expected_slices_list, expect_token_pairs_list), (list(actual_slices), list(actual_token_pairs)))
def testPageSizeSmallerThanListLength(self): list_ = list(range(100)) page_size = 80 expected_slices_list = [slice(0, 80, None), slice(80, None, None)] expect_token_pairs_list = [(None, 'TOKEN80'), ('TOKEN80', None)] actual_slices, actual_token_pairs = list_slicer.SliceList( list_, page_size) self.assertEqual((expected_slices_list, expect_token_pairs_list), (list(actual_slices), list(actual_token_pairs)))
def testPageSizeEqualsListLength(self): list_ = list(range(100)) page_size = 100 expected_slices_list = [slice(0, None, None)] expect_token_pairs_list = [(None, None)] actual_slices, actual_token_pairs = list_slicer.SliceList( list_, page_size) self.assertEqual((expected_slices_list, expect_token_pairs_list), (list(actual_slices), list(actual_token_pairs)))
def _ExpectList(self, queues, limit=None, page_size=100): if limit: queues = queues[:limit] slices, token_pairs = list_slicer.SliceList(queues, page_size) for slice_, (current_token, next_token) in zip(slices, token_pairs): self.queues_service.List.Expect( self.messages.CloudtasksProjectsLocationsQueuesListRequest( parent=self.location_ref.RelativeName(), pageToken=current_token, pageSize=page_size), self.messages.ListQueuesResponse(queues=queues[slice_], nextPageToken=next_token))
def testList(self): project_ref = util.ParseProject(self.Project()) subs = [self.msgs.Subscription(name=str(i)) for i in range(200)] slices, token_pairs = list_slicer.SliceList(subs, 100) for slice_, (current_token, next_token) in zip(slices, token_pairs): self.subscriptions_service.List.Expect( self.msgs.PubsubProjectsSubscriptionsListRequest( project=project_ref.RelativeName(), pageSize=100, pageToken=current_token), self.msgs.ListSubscriptionsResponse(subscriptions=subs[slice_], nextPageToken=next_token)) result = self.subscriptions_client.List(project_ref) self.assertEqual(list(result), subs)
def testList(self): project_ref = util.ParseProject(self.Project()) topics_list = [self.msgs.Topic(name=str(i)) for i in range(200)] slices, token_pairs = list_slicer.SliceList(topics_list, 100) for slice_, (current_token, next_token) in zip(slices, token_pairs): self.topics_service.List.Expect( self.msgs.PubsubProjectsTopicsListRequest( project=project_ref.RelativeName(), pageSize=100, pageToken=current_token), self.msgs.ListTopicsResponse(topics=topics_list[slice_], nextPageToken=next_token)) result = self.topics_client.List(project_ref) self.assertEqual(list(result), topics_list)
def testListSubscriptions(self): list_subs_service = self.client.projects_topics_subscriptions topic_ref = util.ParseTopic('topic1', self.Project()) subs = [str(i) for i in range(200)] slices, token_pairs = list_slicer.SliceList(subs, 100) for slice_, (current_token, next_token) in zip(slices, token_pairs): list_subs_service.List.Expect( self.msgs.PubsubProjectsTopicsSubscriptionsListRequest( topic=topic_ref.RelativeName(), pageSize=100, pageToken=current_token), self.msgs.ListTopicSubscriptionsResponse( subscriptions=subs[slice_], nextPageToken=next_token)) result = self.topics_client.ListSubscriptions(topic_ref) self.assertEqual(list(result), subs)
def testPageSizeMuchSmallerThanListLength(self): list_ = list(range(100)) page_size = 6 expected_slices_list = [ slice(0, 6, None), slice(6, 12, None), slice(12, 18, None), slice(18, 24, None), slice(24, 30, None), slice(30, 36, None), slice(36, 42, None), slice(42, 48, None), slice(48, 54, None), slice(54, 60, None), slice(60, 66, None), slice(66, 72, None), slice(72, 78, None), slice(78, 84, None), slice(84, 90, None), slice(90, 96, None), slice(96, None, None) ] expect_token_pairs_list = [(None, 'TOKEN6'), ('TOKEN6', 'TOKEN12'), ('TOKEN12', 'TOKEN18'), ('TOKEN18', 'TOKEN24'), ('TOKEN24', 'TOKEN30'), ('TOKEN30', 'TOKEN36'), ('TOKEN36', 'TOKEN42'), ('TOKEN42', 'TOKEN48'), ('TOKEN48', 'TOKEN54'), ('TOKEN54', 'TOKEN60'), ('TOKEN60', 'TOKEN66'), ('TOKEN66', 'TOKEN72'), ('TOKEN72', 'TOKEN78'), ('TOKEN78', 'TOKEN84'), ('TOKEN84', 'TOKEN90'), ('TOKEN90', 'TOKEN96'), ('TOKEN96', None)] actual_slices, actual_token_pairs = list_slicer.SliceList( list_, page_size) self.assertEqual((expected_slices_list, expect_token_pairs_list), (list(actual_slices), list(actual_token_pairs)))
def _ExpectList(self, devices, batch_size=100, limit=None, device_ids=None, device_num_ids=None, field_mask=None): """Create expected List() call(s). Based on the number of devices and batching parameters. Args: devices: list of Device batch_size: int, the number of results in each page limit: int or None, the total number of devices to limit device_ids: list of str or None, the device IDs requested device_num_ids: list of int or None, the numerical device IDs requested field_mask: str or None, the requested fields """ if limit: devices = devices[:limit] slices, token_pairs = list_slicer.SliceList(devices, batch_size) registry_name = ('projects/{}/' 'locations/us-central1/' 'registries/my-registry').format(self.Project()) for slice_, (current_token, next_token) in zip(slices, token_pairs): self.client.projects_locations_registries_devices.List.Expect( self.messages. CloudiotProjectsLocationsRegistriesDevicesListRequest( parent=registry_name, pageToken=current_token, pageSize=batch_size, deviceIds=device_ids or [], deviceNumIds=device_num_ids or [], fieldMask=field_mask), self.messages.ListDevicesResponse(devices=devices[slice_], nextPageToken=next_token))
def _ExpectList(self, tasks, limit=None, page_size=100): """Create expected List() call(s). Based on the number of tasks and batching parameters. Args: tasks: list of Tasks limit: int or None, the total number of tasks to limit page_size: int, the number of results in each page """ if limit: tasks = tasks[:limit] slices, token_pairs = list_slicer.SliceList(tasks, page_size) for slice_, (current_token, next_token) in zip(slices, token_pairs): self.tasks_service.List.Expect( self.messages. CloudtasksProjectsLocationsQueuesTasksListRequest( parent=self.queue_ref.RelativeName(), pageToken=current_token, pageSize=page_size), self.messages.ListTasksResponse(tasks=tasks[slice_], nextPageToken=next_token))
def testPageSizeZero(self): list_ = list(range(100)) page_size = 0 with self.assertRaisesRegex(ValueError, r'range\(\) arg 3 must not be zero'): list_slicer.SliceList(list_, page_size)