Пример #1
0
    def test_constructor_defaults(self):
        context = context_module.Context("client")
        assert context.client == "client"
        assert isinstance(context.eventloop, _eventloop.EventLoop)
        assert context.batches == {}
        assert context.transaction is None

        node1, pid1, sequence_no1 = context.id.split("-")
        node2, pid2, sequence_no2 = context_module.Context("client").id.split(
            "-")
        assert node1 == node2
        assert pid1 == pid2
        assert int(sequence_no2) - int(sequence_no1) == 1
Пример #2
0
def in_context(monkeypatch):
    """
    This fixture will create a context that won't connect to the datastore.
    At the same time it will mock context used in the code to do nothing
    to prevent double context creation.
    """
    ndb_client = mock.Mock(
        project="testing",
        namespace=None,
        stub=mock.Mock(spec=()),
        spec=("project", "namespace", "stub"),
    )
    context = context_module.Context(ndb_client).use()
    context.__enter__()

    class MockContext:
        @contextmanager
        def context(self):
            yield None

    def client():
        return MockContext()

    monkeypatch.setattr(ndb, 'Client', client)

    yield

    context.__exit__(None, None, None)
Пример #3
0
 def test_constructor_defaults(self, make_stub):
     context = context_module.Context("client")
     assert context.client == "client"
     assert context.stub is make_stub.return_value
     make_stub.assert_called_once_with("client")
     assert isinstance(context.eventloop, _eventloop.EventLoop)
     assert context.batches == {}
     assert context.transaction is None
Пример #4
0
 def _make_one(self, **kwargs):
     client = mock.Mock(
         namespace=None,
         project="testing",
         spec=("namespace", "project"),
         stub=mock.Mock(spec=()),
     )
     return context_module.Context(client, **kwargs)
Пример #5
0
def context():
    client = mock.Mock(project="testing",
                       namespace=None,
                       spec=("project", "namespace"))
    context = context_module.Context(client,
                                     stub=mock.Mock(spec=()),
                                     eventloop=TestingEventLoop())
    return context
Пример #6
0
 def test_insecure_channel(datastore_pb2_grpc, grpc):
     channel = grpc.insecure_channel.return_value
     client = mock.Mock(
         secure=False, host="thehost", spec=("secure", "host")
     )
     context = context_module.Context(client)
     with context.use():
         stub = _api.stub()
     assert stub is datastore_pb2_grpc.DatastoreStub.return_value
     datastore_pb2_grpc.DatastoreStub.assert_called_once_with(channel)
     grpc.insecure_channel.assert_called_once_with("thehost")
Пример #7
0
 def test_constructor_overrides(self):
     context = context_module.Context(
         client="client",
         eventloop="eventloop",
         batches="batches",
         transaction="transaction",
     )
     assert context.client == "client"
     assert context.eventloop == "eventloop"
     assert context.batches == "batches"
     assert context.transaction == "transaction"
Пример #8
0
def context():
    client = mock.Mock(project="testing",
                       namespace=None,
                       spec=("project", "namespace"))
    context = context_module.Context(
        client,
        stub=mock.Mock(spec=()),
        eventloop=TestingEventLoop(),
        datastore_policy=True,
        legacy_data=False,
    )
    return context
Пример #9
0
 def test_it():
     client = mock.Mock(
         _credentials="creds",
         secure=True,
         host="thehost",
         stub=object(),
         spec=("_credentials", "secure", "host", "stub"),
         client_info=client_info.ClientInfo(
             user_agent="google-cloud-ndb/{}".format(__version__)),
     )
     context = context_module.Context(client)
     with context.use():
         assert _api.stub() is client.stub
Пример #10
0
 def test_secure_channel(datastore_pb2_grpc, _helpers):
     channel = _helpers.make_secure_channel.return_value
     client = mock.Mock(
         _credentials="creds",
         secure=True,
         host="thehost",
         spec=("_credentials", "secure", "host"),
     )
     context = context_module.Context(client)
     with context.use():
         stub = _api.stub()
         assert _api.stub() is stub  # one stub per context
     assert stub is datastore_pb2_grpc.DatastoreStub.return_value
     datastore_pb2_grpc.DatastoreStub.assert_called_once_with(channel)
     _helpers.make_secure_channel.assert_called_once_with(
         "creds", _http.DEFAULT_USER_AGENT, "thehost")
Пример #11
0
 def test_secure_channel(datastore_pb2_grpc, _helpers):
     channel = _helpers.make_secure_channel.return_value
     client = mock.Mock(
         _credentials="creds",
         secure=True,
         host="thehost",
         spec=("_credentials", "secure", "host"),
         client_info=client_info.ClientInfo(
             user_agent="google-cloud-ndb/{}".format(__version__)
         ),
     )
     context = context_module.Context(client)
     with context.use():
         stub = _api.stub()
         assert _api.stub() is stub  # one stub per context
     assert stub is datastore_pb2_grpc.DatastoreStub.return_value
     datastore_pb2_grpc.DatastoreStub.assert_called_once_with(channel)
     _helpers.make_secure_channel.assert_called_once_with(
         "creds", client.client_info.to_user_agent(), "thehost"
     )
Пример #12
0
    def context(self, cache_policy=None):
        """Establish a context for a set of NDB calls.

        This method provides a context manager which establishes the runtime
        state for using NDB.

        For example:

        .. code-block:: python

            from google.cloud import ndb

            client = ndb.Client()
            with client.context():
                # Use NDB for some stuff
                pass

        Use of a context is required--NDB can only be used inside a running
        context. The context is used to manage the connection to Google Cloud
        Datastore, an event loop for asynchronous API calls, runtime caching
        policy, and other essential runtime state.

        Code within an asynchronous context should be single threaded.
        Internally, a :class:`threading.local` instance is used to track the
        current event loop.

        In a web application, it is recommended that a single context be used
        per HTTP request. This can typically be accomplished in a middleware
        layer.

        Arguments:
            cache_policy (Optional[Callable[[key.Key], bool]]): The
                cache policy to use in this context. See:
                :meth:`~google.cloud.ndb.context.Context.set_cache_policy`.
        """
        context = context_module.Context(self, cache_policy=cache_policy)
        with context.use():
            yield context

        # Finish up any work left to do on the event loop
        context.eventloop.run()
Пример #13
0
 def test_constructor_defaults(self):
     context = context_module.Context("client")
     assert context.client == "client"
     assert isinstance(context.eventloop, _eventloop.EventLoop)
     assert context.batches == {}
     assert context.transaction is None
Пример #14
0
 def test_urlfetch(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.urlfetch()
Пример #15
0
 def test_clear_cache(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.clear_cache()
 def test_constructor():
     with pytest.raises(NotImplementedError):
         context.Context()
Пример #17
0
 def test_flush(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.flush()
Пример #18
0
 def test_in_transaction(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.in_transaction()
Пример #19
0
    def context(
        self,
        cache_policy=None,
        global_cache=None,
        global_cache_policy=None,
        global_cache_timeout_policy=None,
        legacy_data=True,
    ):
        """Establish a context for a set of NDB calls.

        This method provides a context manager which establishes the runtime
        state for using NDB.

        For example:

        .. code-block:: python

            from google.cloud import ndb

            client = ndb.Client()
            with client.context():
                # Use NDB for some stuff
                pass

        Use of a context is required--NDB can only be used inside a running
        context. The context is used to manage the connection to Google Cloud
        Datastore, an event loop for asynchronous API calls, runtime caching
        policy, and other essential runtime state.

        Code within an asynchronous context should be single threaded.
        Internally, a :class:`threading.local` instance is used to track the
        current event loop.

        In a web application, it is recommended that a single context be used
        per HTTP request. This can typically be accomplished in a middleware
        layer.

        Arguments:
            cache_policy (Optional[Callable[[key.Key], bool]]): The
                cache policy to use in this context. See:
                :meth:`~google.cloud.ndb.context.Context.set_cache_policy`.
            global_cache (Optional[global_cache.GlobalCache]):
                The global cache for this context. See:
                :class:`~google.cloud.ndb.global_cache.GlobalCache`.
            global_cache_policy (Optional[Callable[[key.Key], bool]]): The
                global cache policy to use in this context. See:
                :meth:`~google.cloud.ndb.context.Context.set_global_cache_policy`.
            global_cache_timeout_policy (Optional[Callable[[key.Key], int]]):
                The global cache timeout to use in this context. See:
                :meth:`~google.cloud.ndb.context.Context.set_global_cache_timeout_policy`.
            legacy_data (bool): Set to ``True`` (the default) to write data in
                a way that can be read by the legacy version of NDB.
        """
        context = context_module.get_context(False)
        if context is not None:
            raise RuntimeError("Context is already created for this thread.")

        context = context_module.Context(
            self,
            cache_policy=cache_policy,
            global_cache=global_cache,
            global_cache_policy=global_cache_policy,
            global_cache_timeout_policy=global_cache_timeout_policy,
            legacy_data=legacy_data,
        )
        with context.use():
            yield context

            # Finish up any work left to do on the event loop
            context.eventloop.run()
Пример #20
0
 def make_some():
     try:
         for _ in range(10000):
             context_module.Context("client")
     except Exception as error:  # pragma: NO COVER
         errors.append(error)
Пример #21
0
 def test_set_datastore_policy(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.set_datastore_policy(None)
Пример #22
0
 def test_set_memcache_timeout_policy(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.set_memcache_timeout_policy(None)
Пример #23
0
 def test_default_memcache_policy(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.default_memcache_policy(None)
Пример #24
0
 def test_memcache_incr(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.memcache_incr()
Пример #25
0
 def test_get_cache_policy(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.get_cache_policy()
Пример #26
0
 def _make_one(self):
     client = mock.Mock(spec=())
     stub = mock.Mock(spec=())
     return context_module.Context(client, stub=stub)
Пример #27
0
 def test_call_on_commit(self):
     context = context_module.Context()
     with pytest.raises(NotImplementedError):
         context.call_on_commit(None)