예제 #1
0
    def test_success_callback_is_tasklet(_datastore_api):
        tasklet = tasklets.Future("tasklet")

        def callback():
            return tasklet

        begin_future = tasklets.Future("begin transaction")
        _datastore_api.begin_transaction.return_value = begin_future

        commit_future = tasklets.Future("commit transaction")
        _datastore_api.commit.return_value = commit_future

        future = _transaction.transaction_async(callback)

        _datastore_api.begin_transaction.assert_called_once_with(False,
                                                                 retries=0)
        begin_future.set_result(b"tx123")

        tasklet.set_result("I tried, momma.")

        _datastore_api.commit.assert_called_once_with(b"tx123", retries=0)
        commit_future.set_result(None)

        assert future.result() == "I tried, momma."
예제 #2
0
    def test_propagation_allowed_not_yet_in_transaction(_datastore_api):
        def callback():
            return "I tried, momma."

        begin_future = tasklets.Future("begin transaction")
        _datastore_api.begin_transaction.return_value = begin_future

        commit_future = tasklets.Future("commit transaction")
        _datastore_api.commit.return_value = commit_future

        with mock.patch(
            "google.cloud.ndb._transaction.transaction_async_",
            side_effect=_transaction.transaction_async_,
        ) as transaction_async_:
            future = _transaction.transaction_async(
                callback,
                join=False,
                propagation=context_module.TransactionOptions.ALLOWED,
            )

        _datastore_api.begin_transaction.assert_called_once_with(False, retries=0)
        begin_future.set_result(b"tx123")

        _datastore_api.commit.assert_called_once_with(b"tx123", retries=0)
        commit_future.set_result(None)

        assert future.result() == "I tried, momma."

        transaction_async_.assert_called_once_with(
            callback,
            3,
            False,
            True,
            True,
            None,
        )
    def test_success(_datastore_api):
        on_commit_callback = mock.Mock()

        def callback():
            context_module.get_context().call_on_commit(on_commit_callback)
            return "I tried, momma."

        begin_future = tasklets.Future("begin transaction")
        _datastore_api.begin_transaction.return_value = begin_future

        commit_future = tasklets.Future("commit transaction")
        _datastore_api.commit.return_value = commit_future

        future = _transaction.transaction_async(callback)

        _datastore_api.begin_transaction.assert_called_once_with(False,
                                                                 retries=0)
        begin_future.set_result(b"tx123")

        _datastore_api.commit.assert_called_once_with(b"tx123", retries=0)
        commit_future.set_result(None)

        assert future.result() == "I tried, momma."
        on_commit_callback.assert_called_once_with()
예제 #4
0
 def test_invalid_propagation():
     with pytest.raises(ValueError):
         _transaction.transaction_async(
             None,
             propagation=99,
         )