def test_add_filter___key__valid_key(self): from google.cloud.datastore.key import Key query = self._make_one(self._make_client()) key = Key("Foo", project=self._PROJECT) query.add_filter("__key__", "=", key) self.assertEqual(query.filters, [("__key__", "=", key)])
def test_get_multi_miss_w_deferred(self): from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore.key import Key key = Key('Kind', 1234, project=self.PROJECT) key_pb = key.to_protobuf() # Set deferred entity on mock connection. creds = _make_credentials() client = self._make_one(credentials=creds) lookup_response = _make_lookup_response(deferred=[key_pb]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api deferred = [] entities = client.get_multi([key], deferred=deferred) self.assertEqual(entities, []) self.assertEqual( [def_key.to_protobuf() for def_key in deferred], [key_pb]) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( self.PROJECT, [key_pb], read_options=read_options, )
def test_ancestor_deleter_w_key(self): from google.cloud.datastore.key import Key key = Key("KIND", 123, project=self._PROJECT) query = self._make_one(client=self._make_client(), ancestor=key) del query.ancestor self.assertIsNone(query.ancestor)
def test_get_multi_max_loops(self): from google.cloud.datastore.key import Key KIND = 'Kind' ID = 1234 # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result([entity_pb]) key = Key(KIND, ID, project=self.PROJECT) deferred = [] missing = [] patch = mock.patch('google.cloud.datastore.client._MAX_LOOPS', new=-1) with patch: result = client.get_multi([key], missing=missing, deferred=deferred) # Make sure we have no results, even though the connection has been # set up as in `test_hit` to return a single result. self.assertEqual(result, []) self.assertEqual(missing, []) self.assertEqual(deferred, [])
def test_get_multi_max_loops(self): from google.cloud.datastore.key import Key kind = "Kind" id_ = 1234 # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, kind, id_, "foo", "Foo") # Make a connection to return the entity pb. creds = _make_credentials() client = self._make_one(credentials=creds) lookup_response = _make_lookup_response(results=[entity_pb]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api key = Key(kind, id_, project=self.PROJECT) deferred = [] missing = [] patch = mock.patch("google.cloud.datastore.client._MAX_LOOPS", new=-1) with patch: result = client.get_multi([key], missing=missing, deferred=deferred) # Make sure we have no results, even though the connection has been # set up as in `test_hit` to return a single result. self.assertEqual(result, []) self.assertEqual(missing, []) self.assertEqual(deferred, []) ds_api.lookup.assert_not_called()
def test_get_multi_miss_w_missing(self): from google.cloud.datastore._generated import entity_pb2 from google.cloud.datastore.key import Key KIND = 'Kind' ID = 1234 # Make a missing entity pb to be returned from mock backend. missed = entity_pb2.Entity() missed.key.partition_id.project_id = self.PROJECT path_element = missed.key.path.add() path_element.kind = KIND path_element.id = ID creds = _make_credentials() client = self._make_one(credentials=creds) # Set missing entity on mock connection. client._connection._add_lookup_result(missing=[missed]) key = Key(KIND, ID, project=self.PROJECT) missing = [] entities = client.get_multi([key], missing=missing) self.assertEqual(entities, []) self.assertEqual([missed.key.to_protobuf() for missed in missing], [key.to_protobuf()])
def test_get_multi_hit_w_transaction(self): from google.cloud.datastore.key import Key TXN_ID = '123' KIND = 'Kind' ID = 1234 PATH = [{'kind': KIND, 'id': ID}] # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result([entity_pb]) key = Key(KIND, ID, project=self.PROJECT) txn = client.transaction() txn._id = TXN_ID result, = client.get_multi([key], transaction=txn) new_key = result.key # Check the returned value is as expected. self.assertIsNot(new_key, key) self.assertEqual(new_key.project, self.PROJECT) self.assertEqual(new_key.path, PATH) self.assertEqual(list(result), ['foo']) self.assertEqual(result['foo'], 'Foo') cw = client._connection._lookup_cw self.assertEqual(len(cw), 1) _, _, _, transaction_id = cw[0] self.assertEqual(transaction_id, TXN_ID)
def test_key(self): from google.cloud.datastore.key import Key key = Key('PATH', 1234, project='PROJECT') name, value = self._call_fut(key) self.assertEqual(name, 'key_value') self.assertEqual(value, key.to_protobuf())
def test_key(self): from google.cloud.datastore.key import Key key = Key("PATH", 1234, project="PROJECT") name, value = self._call_fut(key) self.assertEqual(name, "key_value") self.assertEqual(value, key.to_protobuf())
def test_add_filter___key__valid_key(self): from google.cloud.datastore.key import Key query = self._make_one(self._makeClient()) key = Key('Foo', project=self._PROJECT) query.add_filter('__key__', '=', key) self.assertEqual(query.filters, [('__key__', '=', key)])
def test_get_multi_hit(self): from google.cloud.datastore.key import Key KIND = 'Kind' ID = 1234 PATH = [{'kind': KIND, 'id': ID}] # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. creds = object() client = self._make_one(credentials=creds) client._connection._add_lookup_result([entity_pb]) key = Key(KIND, ID, project=self.PROJECT) result, = client.get_multi([key]) new_key = result.key # Check the returned value is as expected. self.assertIsNot(new_key, key) self.assertEqual(new_key.project, self.PROJECT) self.assertEqual(new_key.path, PATH) self.assertEqual(list(result), ['foo']) self.assertEqual(result['foo'], 'Foo')
def test_get_multi_hit_w_transaction(self): from google.cloud.proto.datastore.v1 import datastore_pb2 from google.cloud.datastore.key import Key txn_id = b'123' kind = 'Kind' id_ = 1234 path = [{'kind': kind, 'id': id_}] # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, kind, id_, 'foo', 'Foo') # Make a connection to return the entity pb. creds = _make_credentials() client = self._make_one(credentials=creds) lookup_response = _make_lookup_response(results=[entity_pb]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api key = Key(kind, id_, project=self.PROJECT) txn = client.transaction() txn._id = txn_id result, = client.get_multi([key], transaction=txn) new_key = result.key # Check the returned value is as expected. self.assertIsNot(new_key, key) self.assertEqual(new_key.project, self.PROJECT) self.assertEqual(new_key.path, path) self.assertEqual(list(result), ['foo']) self.assertEqual(result['foo'], 'Foo') read_options = datastore_pb2.ReadOptions(transaction=txn_id) ds_api.lookup.assert_called_once_with(self.PROJECT, read_options, [key.to_protobuf()])
def test_filter___key__not_equal_operator(self): from google.cloud.datastore.key import Key key = Key("Foo", project=self._PROJECT) query = self._make_one(self._make_client()) query.add_filter("__key__", "<", key) self.assertEqual(query.filters, [("__key__", "<", key)])
def _make_key_pb(self, project, id_=1234): from google.cloud.datastore.key import Key path_args = ('Kind', ) if id_ is not None: path_args += (id_, ) return Key(*path_args, project=project).to_protobuf()
def test_get_multi_hit(self): from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore.key import Key kind = "Kind" id_ = 1234 path = [{"kind": kind, "id": id_}] # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.PROJECT, kind, id_, "foo", "Foo") # Make a connection to return the entity pb. creds = _make_credentials() client = self._make_one(credentials=creds) lookup_response = _make_lookup_response(results=[entity_pb]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api key = Key(kind, id_, project=self.PROJECT) result, = client.get_multi([key]) new_key = result.key # Check the returned value is as expected. self.assertIsNot(new_key, key) self.assertEqual(new_key.project, self.PROJECT) self.assertEqual(new_key.path, path) self.assertEqual(list(result), ["foo"]) self.assertEqual(result["foo"], "Foo") read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with(self.PROJECT, [key.to_protobuf()], read_options=read_options)
def test_ctor_explicit(self): from google.cloud.datastore.key import Key _PROJECT = "OTHER_PROJECT" _KIND = "KIND" _NAMESPACE = "OTHER_NAMESPACE" client = self._make_client() ancestor = Key("ANCESTOR", 123, project=_PROJECT) FILTERS = [("foo", "=", "Qux"), ("bar", "<", 17)] PROJECTION = ["foo", "bar", "baz"] ORDER = ["foo", "bar"] DISTINCT_ON = ["foo"] query = self._make_one( client, kind=_KIND, project=_PROJECT, namespace=_NAMESPACE, ancestor=ancestor, filters=FILTERS, projection=PROJECTION, order=ORDER, distinct_on=DISTINCT_ON, ) self.assertIs(query._client, client) self.assertEqual(query.project, _PROJECT) self.assertEqual(query.kind, _KIND) self.assertEqual(query.namespace, _NAMESPACE) self.assertEqual(query.ancestor.path, ancestor.path) self.assertEqual(query.filters, FILTERS) self.assertEqual(query.projection, PROJECTION) self.assertEqual(query.order, ORDER) self.assertEqual(query.distinct_on, DISTINCT_ON)
def test_get_multi_miss_w_missing(self): from google.cloud.datastore_v1.proto import entity_pb2 from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore.key import Key KIND = "Kind" ID = 1234 # Make a missing entity pb to be returned from mock backend. missed = entity_pb2.Entity() missed.key.partition_id.project_id = self.PROJECT path_element = missed.key.path.add() path_element.kind = KIND path_element.id = ID creds = _make_credentials() client = self._make_one(credentials=creds) # Set missing entity on mock connection. lookup_response = _make_lookup_response(missing=[missed]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api key = Key(KIND, ID, project=self.PROJECT) missing = [] entities = client.get_multi([key], missing=missing) self.assertEqual(entities, []) key_pb = key.to_protobuf() self.assertEqual([missed.key.to_protobuf() for missed in missing], [key_pb]) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with(self.PROJECT, [key_pb], read_options=read_options)
def test_ctor_explicit(self): from google.cloud.datastore.key import Key _PROJECT = 'OTHER_PROJECT' _KIND = 'KIND' _NAMESPACE = 'OTHER_NAMESPACE' client = self._makeClient() ancestor = Key('ANCESTOR', 123, project=_PROJECT) FILTERS = [('foo', '=', 'Qux'), ('bar', '<', 17)] PROJECTION = ['foo', 'bar', 'baz'] ORDER = ['foo', 'bar'] DISTINCT_ON = ['foo'] query = self._make_one( client, kind=_KIND, project=_PROJECT, namespace=_NAMESPACE, ancestor=ancestor, filters=FILTERS, projection=PROJECTION, order=ORDER, distinct_on=DISTINCT_ON, ) self.assertIs(query._client, client) self.assertEqual(query.project, _PROJECT) self.assertEqual(query.kind, _KIND) self.assertEqual(query.namespace, _NAMESPACE) self.assertEqual(query.ancestor.path, ancestor.path) self.assertEqual(query.filters, FILTERS) self.assertEqual(query.projection, PROJECTION) self.assertEqual(query.order, ORDER) self.assertEqual(query.distinct_on, DISTINCT_ON)
def key_from_protobuf(pb): """Factory method for creating a key based on a protobuf. The protobuf should be one returned from the Cloud Datastore Protobuf API. :type pb: :class:`.entity_pb2.Key` :param pb: The Protobuf representing the key. :rtype: :class:`google.cloud.datastore.key.Key` :returns: a new `Key` instance """ path_args = [] for element in pb.path: path_args.append(element.kind) if element.id: # Simple field (int64) path_args.append(element.id) # This is safe: we expect proto objects returned will only have # one of `name` or `id` set. if element.name: # Simple field (string) path_args.append(element.name) project = None if pb.partition_id.project_id: # Simple field (string) project = pb.partition_id.project_id namespace = None if pb.partition_id.namespace_id: # Simple field (string) namespace = pb.partition_id.namespace_id return Key(*path_args, namespace=namespace, project=project)
def test___eq_____ne___w_non_entity(self): from google.cloud.datastore.key import Key key = Key(_KIND, _ID, project=_PROJECT) entity = self._make_one(key=key) self.assertFalse(entity == object()) self.assertTrue(entity != object())
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from google.cloud.proto.datastore.v1 import datastore_pb2 from google.cloud.proto.datastore.v1 import entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.key import Key key1 = Key('Kind', project=self.PROJECT) key1_pb = key1.to_protobuf() key2 = Key('Kind', 2345, project=self.PROJECT) key2_pb = key2.to_protobuf() entity1_pb = entity_pb2.Entity() entity1_pb.key.CopyFrom(key1_pb) entity2_pb = entity_pb2.Entity() entity2_pb.key.CopyFrom(key2_pb) creds = _make_credentials() client = self._make_one(credentials=creds) # Mock up two separate requests. Using an iterable as side_effect # allows multiple return values. lookup_response1 = _make_lookup_response(results=[entity1_pb], deferred=[key2_pb]) lookup_response2 = _make_lookup_response(results=[entity2_pb]) ds_api = _make_datastore_api() ds_api.lookup = mock.Mock( side_effect=[lookup_response1, lookup_response2], spec=[]) client._datastore_api_internal = ds_api missing = [] found = client.get_multi([key1, key2], missing=missing) self.assertEqual(len(found), 2) self.assertEqual(len(missing), 0) # Check the actual contents on the response. self.assertIsInstance(found[0], Entity) self.assertEqual(found[0].key.path, key1.path) self.assertEqual(found[0].key.project, key1.project) self.assertIsInstance(found[1], Entity) self.assertEqual(found[1].key.path, key2.path) self.assertEqual(found[1].key.project, key2.project) self.assertEqual(ds_api.lookup.call_count, 2) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_any_call(self.PROJECT, read_options, [key2_pb]) ds_api.lookup.assert_any_call(self.PROJECT, read_options, [key1_pb, key2_pb])
def test_key(self): from google.cloud.datastore.key import Key pb = self._makePB() key = Key('KIND', 1234, project='PROJECT') self._call_fut(pb, key) value = pb.key_value self.assertEqual(value, key.to_protobuf())
def get_by_therapist(therapist_key): therapist = Key('Therapist', therapist_key) client = datastore.Client() query = client.query(kind='Service') query.add_filter('therapist', '=', therapist) return list(query.fetch())
def test_get_multi_hit_multiple_keys_different_project(self): from google.cloud.datastore.key import Key PROJECT1 = 'PROJECT' PROJECT2 = 'PROJECT-ALT' # Make sure our IDs are actually different. self.assertNotEqual(PROJECT1, PROJECT2) key1 = Key('KIND', 1234, project=PROJECT1) key2 = Key('KIND', 1234, project=PROJECT2) creds = object() client = self._makeOne(credentials=creds) with self.assertRaises(ValueError): client.get_multi([key1, key2])
def test_ancestor_setter_w_key(self): from google.cloud.datastore.key import Key _NAME = u'NAME' key = Key('KIND', 123, project=self._PROJECT) query = self._makeOne(self._makeClient()) query.add_filter('name', '=', _NAME) query.ancestor = key self.assertEqual(query.ancestor.path, key.path)
def test_key(self): from google.cloud.proto.datastore.v1 import entity_pb2 from google.cloud.datastore.key import Key pb = entity_pb2.Value() expected = Key('KIND', 1234, project='PROJECT').to_protobuf() pb.key_value.CopyFrom(expected) found = self._call_fut(pb) self.assertEqual(found.to_protobuf(), expected)
def test_get_multi_miss(self): from google.cloud.datastore.key import Key creds = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result() key = Key('Kind', 1234, project=self.PROJECT) results = client.get_multi([key]) self.assertEqual(results, [])
def test_key_filter_explicit(self): from google.cloud.datastore.key import Key client = self._makeClient() query = self._makeOne(client) self.assertEqual(query.filters, []) key = Key('Kind', 1234, project='project') query.key_filter(key, operator='>') self.assertEqual(query.filters, [('__key__', '>', key)])
def test_get_multi_w_missing_non_empty(self): from google.cloud.datastore.key import Key creds = _make_credentials() client = self._make_one(credentials=creds) key = Key('Kind', 1234, project=self.PROJECT) missing = ['this', 'list', 'is', 'not', 'empty'] self.assertRaises(ValueError, client.get_multi, [key], missing=missing)
def test_key_filter_defaults(self): from google.cloud.datastore.key import Key client = self._make_client() query = self._make_one(client) self.assertEqual(query.filters, []) key = Key('Kind', 1234, project='project') query.key_filter(key) self.assertEqual(query.filters, [('__key__', '=', key)])