# Path: google/cloud/ndb/context.py
# @property
# def context(self):
#     return self._context.get()
# 
# Path: google/cloud/ndb/exceptions.py
# class Error(Exception):
# class ContextError(Error):
# class BadValueError(Error):
# class BadArgumentError(Error):
# class BadRequestError(Error):
# class Rollback(Error):
# class BadQueryError(Error):
# class BadFilterError(Error):
# class NoLongerImplementedError(NotImplementedError):
# class Cancelled(Error):
# class NestedRetryException(Error):
#     def __init__(self):
#     def __init__(self, filter):
#     def __init__(self):
# 
# Path: google/cloud/ndb/tasklets.py
# class Future(object):
# class _TaskletFuture(Future):
# class _MultiFuture(Future):
# class Return(Exception):
# class QueueFuture(object):
# class ReducingFuture(object):
# class SerialQueueFuture(object):
#     def __init__(self, info="Unknown"):
#     def __repr__(self):
#     def done(self):
#     def running(self):
#     def wait(self):
#     def check_success(self):
#     def set_result(self, result):
#     def set_exception(self, exception):
#     def _finish(self):
#     def result(self):
#     def exception(self):
#     def get_traceback(self):
#     def add_done_callback(self, callback):
#     def cancel(self):
#     def cancelled(self):
#     def wait_any(futures):
#     def wait_all(futures):
#     def __init__(self, generator, context, info="Unknown"):
#     def _advance_tasklet(self, send_value=None, error=None):
#         def done_callback(yielded):
#     def cancel(self):
# def _get_return_value(stop):
#     def __init__(self, dependencies):
#     def __repr__(self):
#     def _dependency_done(self, dependency):
#     def cancel(self):
# def tasklet(wrapped):
#     def tasklet_wrapper(*args, **kwargs):
# def wait_any(futures):
# def wait_all(futures):
# def sleep(seconds):
# def add_flow_exception(*args, **kwargs):
# def make_context(*args, **kwargs):
# def make_default_context(*args, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __init__(self, *args, **kwargs):
# def set_context(*args, **kwargs):
# def synctasklet(wrapped):
#     def synctasklet_wrapper(*args, **kwargs):
# def toplevel(wrapped):
#     def toplevel_wrapper(*args, **kwargs):
# 
# Path: google/cloud/ndb/_transaction.py
# class _Propagation(object):
#     def __init__(self, propagation, join=None):
#     def _handle_nested(self):
#     def _handle_mandatory(self):
#     def _handle_allowed(self):
#     def _handle_independent(self):
#     def _handle_join(self):
#     def handle_propagation(self):
# def in_transaction():
# def transaction(
#     callback,
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=False,
#     xg=True,
#     propagation=None,
# ):
# def transaction_async(
#     callback,
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=False,
#     xg=True,
#     propagation=None,
# ):
# def transaction_async_(
#     callback,
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=False,
#     xg=True,
#     propagation=None,
# ):
# def _transaction_async(context, callback, read_only=False):
#     def run_inner_loop(inner_context):
# def transactional(
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=True,
#     xg=True,
#     propagation=None,
# ):
#     def transactional_wrapper(wrapped):
#         def transactional_inner_wrapper(*args, **kwargs):
#             def callback():
# def transactional_async(
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=True,
#     xg=True,
#     propagation=None,
# ):
#     def transactional_async_wrapper(wrapped):
#         def transactional_async_inner_wrapper(*args, **kwargs):
#             def callback():
# def transactional_tasklet(
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=True,
#     xg=True,
#     propagation=None,
# ):
#     def transactional_tasklet_wrapper(wrapped):
#         def transactional_tasklet_inner_wrapper(*args, **kwargs):
#             def callback():
# def non_transactional(allow_existing=True):
#     def non_transactional_wrapper(wrapped):
#         def non_transactional_inner_wrapper(*args, **kwargs):


import itertools
import logging
    import mock
import pytest
    from unittest import mock
from google.api_core import exceptions as core_exceptions
from google.cloud.ndb import context as context_module
from google.cloud.ndb import exceptions
from google.cloud.ndb import tasklets
from google.cloud.ndb import _transaction
    @staticmethod
    @mock.patch("google.cloud.ndb._transaction.transaction_async")
    def test_success(transaction_async):
        transaction_async.return_value.result.return_value = 42
        assert _transaction.transaction("callback") == 42
        transaction_async.assert_called_once_with(
            "callback",
            read_only=False,
            retries=3,
            join=False,
            xg=True,
            propagation=None,
        )


class Test_transaction_async:
    @staticmethod
    @pytest.mark.usefixtures("in_context")
    @mock.patch("google.cloud.ndb._datastore_api")
    def test_success(_datastore_api):
        context_module.get_context().cache["foo"] = "bar"

        def callback():
            # The transaction uses its own in-memory cache, which should be empty in
            # the transaction context and not include the key set above.
            context = context_module.get_context()
            assert not context.cache

            return "I tried, momma."


# Path: google/cloud/ndb/context.py
# @property
# def context(self):
#     return self._context.get()
# 
# Path: google/cloud/ndb/exceptions.py
# class Error(Exception):
# class ContextError(Error):
# class BadValueError(Error):
# class BadArgumentError(Error):
# class BadRequestError(Error):
# class Rollback(Error):
# class BadQueryError(Error):
# class BadFilterError(Error):
# class NoLongerImplementedError(NotImplementedError):
# class Cancelled(Error):
# class NestedRetryException(Error):
#     def __init__(self):
#     def __init__(self, filter):
#     def __init__(self):
# 
# Path: google/cloud/ndb/tasklets.py
# class Future(object):
# class _TaskletFuture(Future):
# class _MultiFuture(Future):
# class Return(Exception):
# class QueueFuture(object):
# class ReducingFuture(object):
# class SerialQueueFuture(object):
#     def __init__(self, info="Unknown"):
#     def __repr__(self):
#     def done(self):
#     def running(self):
#     def wait(self):
#     def check_success(self):
#     def set_result(self, result):
#     def set_exception(self, exception):
#     def _finish(self):
#     def result(self):
#     def exception(self):
#     def get_traceback(self):
#     def add_done_callback(self, callback):
#     def cancel(self):
#     def cancelled(self):
#     def wait_any(futures):
#     def wait_all(futures):
#     def __init__(self, generator, context, info="Unknown"):
#     def _advance_tasklet(self, send_value=None, error=None):
#         def done_callback(yielded):
#     def cancel(self):
# def _get_return_value(stop):
#     def __init__(self, dependencies):
#     def __repr__(self):
#     def _dependency_done(self, dependency):
#     def cancel(self):
# def tasklet(wrapped):
#     def tasklet_wrapper(*args, **kwargs):
# def wait_any(futures):
# def wait_all(futures):
# def sleep(seconds):
# def add_flow_exception(*args, **kwargs):
# def make_context(*args, **kwargs):
# def make_default_context(*args, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __init__(self, *args, **kwargs):
# def set_context(*args, **kwargs):
# def synctasklet(wrapped):
#     def synctasklet_wrapper(*args, **kwargs):
# def toplevel(wrapped):
#     def toplevel_wrapper(*args, **kwargs):
# 
# Path: google/cloud/ndb/_transaction.py
# class _Propagation(object):
#     def __init__(self, propagation, join=None):
#     def _handle_nested(self):
#     def _handle_mandatory(self):
#     def _handle_allowed(self):
#     def _handle_independent(self):
#     def _handle_join(self):
#     def handle_propagation(self):
# def in_transaction():
# def transaction(
#     callback,
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=False,
#     xg=True,
#     propagation=None,
# ):
# def transaction_async(
#     callback,
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=False,
#     xg=True,
#     propagation=None,
# ):
# def transaction_async_(
#     callback,
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=False,
#     xg=True,
#     propagation=None,
# ):
# def _transaction_async(context, callback, read_only=False):
#     def run_inner_loop(inner_context):
# def transactional(
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=True,
#     xg=True,
#     propagation=None,
# ):
#     def transactional_wrapper(wrapped):
#         def transactional_inner_wrapper(*args, **kwargs):
#             def callback():
# def transactional_async(
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=True,
#     xg=True,
#     propagation=None,
# ):
#     def transactional_async_wrapper(wrapped):
#         def transactional_async_inner_wrapper(*args, **kwargs):
#             def callback():
# def transactional_tasklet(
#     retries=_retry._DEFAULT_RETRIES,
#     read_only=False,
#     join=True,
#     xg=True,
#     propagation=None,
# ):
#     def transactional_tasklet_wrapper(wrapped):
#         def transactional_tasklet_inner_wrapper(*args, **kwargs):
#             def callback():
# def non_transactional(allow_existing=True):
#     def non_transactional_wrapper(wrapped):
#         def non_transactional_inner_wrapper(*args, **kwargs):


# Path: tests/unit/test__transaction.py
import itertools
import logging
    import mock
import pytest
    from unittest import mock
from google.api_core import exceptions as core_exceptions
from google.cloud.ndb import context as context_module
from google.cloud.ndb import exceptions
from google.cloud.ndb import tasklets
from google.cloud.ndb import _transaction

    @staticmethod
    @mock.patch("google.cloud.ndb._transaction.transaction_async")
    def test_success(transaction_async):
        transaction_async.return_value.result.return_value = 42
        assert _transaction.transaction("callback") == 42
        transaction_async.assert_called_once_with(
            "callback",
            read_only=False,
            retries=3,
            join=False,
            xg=True,
            propagation=None,
        )


class Test_transaction_async:
    @staticmethod
    @pytest.mark.usefixtures("in_context")
    @mock.patch("google.cloud.ndb._datastore_api")
    def test_success(_datastore_api):
        context_module.get_context().cache["foo"] = "bar"

        def callback():
            # The transaction uses its own in-memory cache, which should be empty in
            # the transaction context and not include the key set above.
            context = context_module.get_context()
            assert not context.cache

            return "I tried, momma."


        begin_future = tasklets.Future("begin transaction")