# Path: tests/unit/v1/test__helpers.py
# class AsyncIter:
#     """Utility to help recreate the effect of an async generator. Useful when
#     you need to mock a system that requires `async for`.
#     """
# 
#     def __init__(self, items):
#         self.items = items
# 
#     async def __aiter__(self):
#         for i in self.items:
#             yield i
# 
# Path: tests/unit/v1/test__helpers.py
# class AsyncMock(mock.MagicMock):
#     async def __call__(self, *args, **kwargs):
#         return super(AsyncMock, self).__call__(*args, **kwargs)
# 
# Path: tests/unit/v1/test_base_query.py
# def _make_credentials():
#     import google.auth.credentials
# 
#     return mock.Mock(spec=google.auth.credentials.Credentials)
# 
# Path: tests/unit/v1/test_base_query.py
# def _make_query_response(**kwargs):
#     # kwargs supported are ``skipped_results``, ``name`` and ``data``
#     from google.cloud.firestore_v1.types import document
#     from google.cloud.firestore_v1.types import firestore
#     from google.cloud._helpers import _datetime_to_pb_timestamp
#     from google.cloud.firestore_v1 import _helpers
# 
#     now = datetime.datetime.utcnow()
#     read_time = _datetime_to_pb_timestamp(now)
#     kwargs["read_time"] = read_time
# 
#     name = kwargs.pop("name", None)
#     data = kwargs.pop("data", None)
#     if name is not None and data is not None:
#         document_pb = document.Document(name=name, fields=_helpers.encode_dict(data))
#         delta = datetime.timedelta(seconds=100)
#         update_time = _datetime_to_pb_timestamp(now - delta)
#         create_time = _datetime_to_pb_timestamp(now - 2 * delta)
#         document_pb._pb.update_time.CopyFrom(update_time)
#         document_pb._pb.create_time.CopyFrom(create_time)
# 
#         kwargs["document"] = document_pb
# 
#     return firestore.RunQueryResponse(**kwargs)
# 
# Path: tests/unit/v1/test_base_query.py
# def _make_cursor_pb(pair):
#     from google.cloud.firestore_v1 import _helpers
#     from google.cloud.firestore_v1.types import query
# 
#     values, before = pair
#     value_pbs = [_helpers.encode_value(value) for value in values]
#     return query.Cursor(values=value_pbs, before=before)


import types
import mock
import pytest
from tests.unit.v1.test__helpers import AsyncIter
from tests.unit.v1.test__helpers import AsyncMock
from tests.unit.v1.test_base_query import _make_credentials
from tests.unit.v1.test_base_query import _make_query_response
from tests.unit.v1.test_base_query import _make_cursor_pb
    from google.cloud.firestore_v1.async_query import AsyncQuery
    from google.cloud.firestore_v1 import _helpers
    from google.api_core.retry import Retry
    from google.cloud import firestore
    from google.cloud.firestore_v1.base_query import _enum_from_direction
    from google.cloud.firestore_v1.types import document
    from google.cloud.firestore_v1.types import firestore
    from google.cloud.firestore_v1.types import document
    from google.cloud.firestore_v1.types import firestore
    from google.cloud.firestore_v1 import _helpers
    from google.api_core.retry import Retry
    from google.cloud.firestore_v1.async_query import AsyncCollectionGroup
    from google.cloud.firestore_v1 import _helpers
    from google.api_core.retry import Retry
    from google.cloud.firestore_v1.async_client import AsyncClient
    client = _make_client()
    firestore_api = AsyncMock(spec=["run_query"])
    firestore_api.run_query.return_value = AsyncIter([])
    client._firestore_api_internal = firestore_api
    query = client.collection("asdf")._query()

    chunks = []
    async for chunk in query._chunkify(10):
        chunks.append(chunk)

    assert chunks == [[]]


@pytest.mark.asyncio
async def test_asyncquery_chunkify_w_chunksize_lt_limit():

    client = _make_client()
    firestore_api = AsyncMock(spec=["run_query"])
    doc_ids = [
        f"projects/project-project/databases/(default)/documents/asdf/{index}"
        for index in range(5)
    ]
    responses1 = [
        firestore.RunQueryResponse(document=document.Document(name=doc_id),)
        for doc_id in doc_ids[:2]
    ]
    responses2 = [
        firestore.RunQueryResponse(document=document.Document(name=doc_id),)
        for doc_id in doc_ids[2:4]
    ]

# Path: tests/unit/v1/test__helpers.py
# class AsyncIter:
#     """Utility to help recreate the effect of an async generator. Useful when
#     you need to mock a system that requires `async for`.
#     """
# 
#     def __init__(self, items):
#         self.items = items
# 
#     async def __aiter__(self):
#         for i in self.items:
#             yield i
# 
# Path: tests/unit/v1/test__helpers.py
# class AsyncMock(mock.MagicMock):
#     async def __call__(self, *args, **kwargs):
#         return super(AsyncMock, self).__call__(*args, **kwargs)
# 
# Path: tests/unit/v1/test_base_query.py
# def _make_credentials():
#     import google.auth.credentials
# 
#     return mock.Mock(spec=google.auth.credentials.Credentials)
# 
# Path: tests/unit/v1/test_base_query.py
# def _make_query_response(**kwargs):
#     # kwargs supported are ``skipped_results``, ``name`` and ``data``
#     from google.cloud.firestore_v1.types import document
#     from google.cloud.firestore_v1.types import firestore
#     from google.cloud._helpers import _datetime_to_pb_timestamp
#     from google.cloud.firestore_v1 import _helpers
# 
#     now = datetime.datetime.utcnow()
#     read_time = _datetime_to_pb_timestamp(now)
#     kwargs["read_time"] = read_time
# 
#     name = kwargs.pop("name", None)
#     data = kwargs.pop("data", None)
#     if name is not None and data is not None:
#         document_pb = document.Document(name=name, fields=_helpers.encode_dict(data))
#         delta = datetime.timedelta(seconds=100)
#         update_time = _datetime_to_pb_timestamp(now - delta)
#         create_time = _datetime_to_pb_timestamp(now - 2 * delta)
#         document_pb._pb.update_time.CopyFrom(update_time)
#         document_pb._pb.create_time.CopyFrom(create_time)
# 
#         kwargs["document"] = document_pb
# 
#     return firestore.RunQueryResponse(**kwargs)
# 
# Path: tests/unit/v1/test_base_query.py
# def _make_cursor_pb(pair):
#     from google.cloud.firestore_v1 import _helpers
#     from google.cloud.firestore_v1.types import query
# 
#     values, before = pair
#     value_pbs = [_helpers.encode_value(value) for value in values]
#     return query.Cursor(values=value_pbs, before=before)


# Path: tests/unit/v1/test_async_query.py
import types
import mock
import pytest
from tests.unit.v1.test__helpers import AsyncIter
from tests.unit.v1.test__helpers import AsyncMock
from tests.unit.v1.test_base_query import _make_credentials
from tests.unit.v1.test_base_query import _make_query_response
from tests.unit.v1.test_base_query import _make_cursor_pb
    from google.cloud.firestore_v1.async_query import AsyncQuery
    from google.cloud.firestore_v1 import _helpers
    from google.api_core.retry import Retry
    from google.cloud import firestore
    from google.cloud.firestore_v1.base_query import _enum_from_direction
    from google.cloud.firestore_v1.types import document
    from google.cloud.firestore_v1.types import firestore
    from google.cloud.firestore_v1.types import document
    from google.cloud.firestore_v1.types import firestore
    from google.cloud.firestore_v1 import _helpers
    from google.api_core.retry import Retry
    from google.cloud.firestore_v1.async_query import AsyncCollectionGroup
    from google.cloud.firestore_v1 import _helpers
    from google.api_core.retry import Retry
    from google.cloud.firestore_v1.async_client import AsyncClient

    client = _make_client()
    firestore_api = AsyncMock(spec=["run_query"])
    firestore_api.run_query.return_value = AsyncIter([])
    client._firestore_api_internal = firestore_api
    query = client.collection("asdf")._query()

    chunks = []
    async for chunk in query._chunkify(10):
        chunks.append(chunk)

    assert chunks == [[]]


@pytest.mark.asyncio
async def test_asyncquery_chunkify_w_chunksize_lt_limit():

    client = _make_client()
    firestore_api = AsyncMock(spec=["run_query"])
    doc_ids = [
        f"projects/project-project/databases/(default)/documents/asdf/{index}"
        for index in range(5)
    ]
    responses1 = [
        firestore.RunQueryResponse(document=document.Document(name=doc_id),)
        for doc_id in doc_ids[:2]
    ]
    responses2 = [
        firestore.RunQueryResponse(document=document.Document(name=doc_id),)
        for doc_id in doc_ids[2:4]
    ]

    responses3 = [