# Path: tests/system/utils/clear_datastore.py
# FETCH_MAX = 20
# ALL_KINDS = (
#     "Character",
#     "Company",
#     "Kind",
#     "Person",
#     "Post",
#     "uuid_key",
#     "timestamp_key",
# )
# TRANSACTION_MAX_GROUPS = 5
# MAX_DEL_ENTITIES = 500
# def print_func(message):
# def get_ancestors(entities):
# def delete_chunks(client, results):
# def remove_kind(kind, client):
# def remove_all_entities(client):
# def main():
# 
# Path: tests/system/utils/populate_datastore.py
# ANCESTOR = ("Book", "GoT")
# RICKARD = ANCESTOR + ("Character", "Rickard")
# EDDARD = RICKARD + ("Character", "Eddard")
# KEY_PATHS = (
#     RICKARD,
#     EDDARD,
#     ANCESTOR + ("Character", "Catelyn"),
#     EDDARD + ("Character", "Arya"),
#     EDDARD + ("Character", "Sansa"),
#     EDDARD + ("Character", "Robb"),
#     EDDARD + ("Character", "Bran"),
#     EDDARD + ("Character", "Jon Snow"),
# )
# CHARACTERS = (
#     {"name": u"Rickard", "family": u"Stark", "appearances": 0, "alive": False},
#     {"name": u"Eddard", "family": u"Stark", "appearances": 9, "alive": False},
#     {
#         "name": u"Catelyn",
#         "family": [u"Stark", u"Tully"],
#         "appearances": 26,
#         "alive": False,
#     },
#     {"name": u"Arya", "family": u"Stark", "appearances": 33, "alive": True},
#     {"name": u"Sansa", "family": u"Stark", "appearances": 31, "alive": True},
#     {"name": u"Robb", "family": u"Stark", "appearances": 22, "alive": False},
#     {"name": u"Bran", "family": u"Stark", "appearances": 25, "alive": True},
#     {"name": u"Jon Snow", "family": u"Stark", "appearances": 32, "alive": True},
# )
# LARGE_CHARACTER_TOTAL_OBJECTS = 2500
# LARGE_CHARACTER_NAMESPACE = "LargeCharacterEntity"
# LARGE_CHARACTER_KIND = "LargeCharacter"
#     MAX_STRING = (string.ascii_lowercase * 58)[:1500]
#         ENTITIES_TO_BATCH = 25
# def print_func(message):
# def add_large_character_entities(client=None):
#     def put_objects(count):
# def add_characters(client=None):
# def add_uid_keys(client=None):
# def add_timestamp_keys(client=None):
# def main():


import pytest
from google.api_core import exceptions
from test_utils.retry import RetryErrors
from .utils import clear_datastore
from .utils import populate_datastore
from . import _helpers



retry_503 = RetryErrors(exceptions.ServiceUnavailable)


def _make_iterator(query, **kw):
    # Do retry for errors raised during initial API call
    return retry_503(query.fetch)(**kw)


def _pull_iterator(query, **kw):
    return list(_make_iterator(query, **kw))


def _do_fetch(query, **kw):
    # Do retry for errors raised during iteration
    return retry_503(_pull_iterator)(query, **kw)


@pytest.fixture(scope="session")
def query_client(datastore_client):
    return _helpers.clone_client(datastore_client, namespace=None)


@pytest.fixture(scope="session")
def ancestor_key(query_client, in_emulator):

    # In the emulator, re-populating the datastore is cheap.
    if in_emulator:

# Path: tests/system/utils/clear_datastore.py
# FETCH_MAX = 20
# ALL_KINDS = (
#     "Character",
#     "Company",
#     "Kind",
#     "Person",
#     "Post",
#     "uuid_key",
#     "timestamp_key",
# )
# TRANSACTION_MAX_GROUPS = 5
# MAX_DEL_ENTITIES = 500
# def print_func(message):
# def get_ancestors(entities):
# def delete_chunks(client, results):
# def remove_kind(kind, client):
# def remove_all_entities(client):
# def main():
# 
# Path: tests/system/utils/populate_datastore.py
# ANCESTOR = ("Book", "GoT")
# RICKARD = ANCESTOR + ("Character", "Rickard")
# EDDARD = RICKARD + ("Character", "Eddard")
# KEY_PATHS = (
#     RICKARD,
#     EDDARD,
#     ANCESTOR + ("Character", "Catelyn"),
#     EDDARD + ("Character", "Arya"),
#     EDDARD + ("Character", "Sansa"),
#     EDDARD + ("Character", "Robb"),
#     EDDARD + ("Character", "Bran"),
#     EDDARD + ("Character", "Jon Snow"),
# )
# CHARACTERS = (
#     {"name": u"Rickard", "family": u"Stark", "appearances": 0, "alive": False},
#     {"name": u"Eddard", "family": u"Stark", "appearances": 9, "alive": False},
#     {
#         "name": u"Catelyn",
#         "family": [u"Stark", u"Tully"],
#         "appearances": 26,
#         "alive": False,
#     },
#     {"name": u"Arya", "family": u"Stark", "appearances": 33, "alive": True},
#     {"name": u"Sansa", "family": u"Stark", "appearances": 31, "alive": True},
#     {"name": u"Robb", "family": u"Stark", "appearances": 22, "alive": False},
#     {"name": u"Bran", "family": u"Stark", "appearances": 25, "alive": True},
#     {"name": u"Jon Snow", "family": u"Stark", "appearances": 32, "alive": True},
# )
# LARGE_CHARACTER_TOTAL_OBJECTS = 2500
# LARGE_CHARACTER_NAMESPACE = "LargeCharacterEntity"
# LARGE_CHARACTER_KIND = "LargeCharacter"
#     MAX_STRING = (string.ascii_lowercase * 58)[:1500]
#         ENTITIES_TO_BATCH = 25
# def print_func(message):
# def add_large_character_entities(client=None):
#     def put_objects(count):
# def add_characters(client=None):
# def add_uid_keys(client=None):
# def add_timestamp_keys(client=None):
# def main():


# Path: tests/system/test_query.py
import pytest
from google.api_core import exceptions
from test_utils.retry import RetryErrors
from .utils import clear_datastore
from .utils import populate_datastore
from . import _helpers




retry_503 = RetryErrors(exceptions.ServiceUnavailable)


def _make_iterator(query, **kw):
    # Do retry for errors raised during initial API call
    return retry_503(query.fetch)(**kw)


def _pull_iterator(query, **kw):
    return list(_make_iterator(query, **kw))


def _do_fetch(query, **kw):
    # Do retry for errors raised during iteration
    return retry_503(_pull_iterator)(query, **kw)


@pytest.fixture(scope="session")
def query_client(datastore_client):
    return _helpers.clone_client(datastore_client, namespace=None)


@pytest.fixture(scope="session")
def ancestor_key(query_client, in_emulator):

    # In the emulator, re-populating the datastore is cheap.
    if in_emulator:

        populate_datastore.add_characters(client=query_client)