async def test_groupby_nested_groupings(): """Check that groupby works when nesting calls.""" seed = [ (0, 10, 20), (0, 11, 21), (0, 12, 21), (1, 13, 21), (1, 14, 22), (2, 15, 22), (3, 16, 23), (3, 17, 23) ] result = [] async for key1, group1 in aitertools.groupby(seed, lambda r: r[0]): async for key2, group2 in aitertools.groupby(group1, lambda r: r[2]): async for element in group2: assert key1 == element[0] assert key2 == element[2] result.append(element) assert seed == result
async def test_groupby_unused_group_iterable(): """Check if all keys are found when the group iter is unused.""" seed = [ (0, 10, 20), (0, 11, 21), (0, 12, 21), (1, 13, 21), (1, 14, 22), (2, 15, 22), (3, 16, 23), (3, 17, 23) ] result = await aitertools.alist( aitertools.groupby(seed, lambda r: r[0]) ) result = set(key for key, group in result) assert result == set([s[0] for s in seed])
async def test_groupby_uses_key_emits_all_values(): """Check the groupby happy path. Key func is used and all values appear.""" seed = [ (0, 10, 20), (0, 11, 21), (0, 12, 21), (1, 13, 21), (1, 14, 22), (2, 15, 22), (3, 16, 23), (3, 17, 23) ] result = [] async for key, group in aitertools.groupby(seed, lambda r: r[0]): async for element in group: assert key == element[0] result.append(element) assert seed == result
async def test_groupby_key_not_callable(): """Check that groupby raises a TypeError when the key is not callable.""" with pytest.raises(TypeError): await aitertools.anext(aitertools.groupby('abc', []))
async def test_groupby_too_many_args(): """Check that groupby raises TypeError when too many args.""" with pytest.raises(TypeError): await aitertools.anext(aitertools.groupby(1, 2, 3))
async def test_groupby_too_few_args(): """Check that groupby raises TypeError when too few args.""" with pytest.raises(TypeError): await aitertools.anext(aitertools.groupby())
async def test_groupby_empty_iterable(): """Check that groupby is empty when given an empty iterable.""" assert (await aitertools.alist( aitertools.groupby([]) )) == []