def test_group_size_exceeds_items(self): """ Tests divide_into_groups when the group size exceeds the number of items. """ actual = parserutils.divide_into_groups(['a', 'b', 'c', 'd', 'e'], 10) expected = [['a', 'b', 'c', 'd', 'e']] self.assertEqual(actual, expected)
def test_empty_list(self): """ Tests divide_into_groups for an empty list. """ actual = parserutils.divide_into_groups([], 10) expected = [] self.assertEqual(actual, expected)
def _factor_queries_by_component(queries, component, limit): """ Takes a list of ReservoirMediaQueries, a string representing the name of a query component/attribute (e.g., 'locations'), and an integer representing the API limit on the number of items for that attribute. Returns a list of queries that break up the orginal queries by the given component so than none of the new queries exceed the API limit for that component. """ if limit == None: limit = float('inf') new_queries = [] for query in queries: # get the items in the query component (e.g., query.locations) items = getattr(query, component) if len(items) > limit: # divide the items into groups no larger than the limit groups = parserutils.divide_into_groups(items, limit) # create a new query for each group for group in groups: new_query = deepcopy(query) setattr(new_query, component, group) new_queries.append(new_query) else: new_queries.append(query) return new_queries
def test_not_exact_multiple(self): """ Tests divide_into_groups when the number of items is not an exact multiple of the group size. """ actual = parserutils.divide_into_groups(['a', 'b', 'c', 'd', 'e'], 2) expected = [['a', 'b'], ['c', 'd'], ['e']] self.assertEqual(actual, expected)
def test_group_size_of_zero(self): """ Tests divide_into_groups for group size of 0. """ with self.assertRaises(ValueError): parserutils.divide_into_groups(['a', 'b', 'c', 'd', 'e'], 0)
def test_negative_group_size(self): """ Tests divide_into_groups for negative group size. """ with self.assertRaises(ValueError): parserutils.divide_into_groups(['a', 'b', 'c', 'd', 'e'], -1)