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
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)
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
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)
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
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")
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"
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
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
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")
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" )
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()
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
def test_urlfetch(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.urlfetch()
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()
def test_flush(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.flush()
def test_in_transaction(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.in_transaction()
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()
def make_some(): try: for _ in range(10000): context_module.Context("client") except Exception as error: # pragma: NO COVER errors.append(error)
def test_set_datastore_policy(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.set_datastore_policy(None)
def test_set_memcache_timeout_policy(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.set_memcache_timeout_policy(None)
def test_default_memcache_policy(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.default_memcache_policy(None)
def test_memcache_incr(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.memcache_incr()
def test_get_cache_policy(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.get_cache_policy()
def _make_one(self): client = mock.Mock(spec=()) stub = mock.Mock(spec=()) return context_module.Context(client, stub=stub)
def test_call_on_commit(self): context = context_module.Context() with pytest.raises(NotImplementedError): context.call_on_commit(None)