def test_get_multi_hit_multiple_keys_same_project(self): from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore.key import Key kind = "Kind" id1 = 1234 id2 = 2345 # Make a found entity pb to be returned from mock backend. entity_pb1 = _make_entity_pb(self.PROJECT, kind, id1) entity_pb2 = _make_entity_pb(self.PROJECT, kind, id2) # Make a connection to return the entity pbs. creds = _make_credentials() client = self._make_one(credentials=creds) lookup_response = _make_lookup_response(results=[entity_pb1, entity_pb2]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api key1 = Key(kind, id1, project=self.PROJECT) key2 = Key(kind, id2, project=self.PROJECT) retrieved1, retrieved2 = client.get_multi([key1, key2]) # Check values match. self.assertEqual(retrieved1.key.path, key1.path) self.assertEqual(dict(retrieved1), {}) self.assertEqual(retrieved2.key.path, key2.path) self.assertEqual(dict(retrieved2), {}) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( self.PROJECT, [key1.to_protobuf(), key2.to_protobuf()], read_options=read_options, )
def test_get_multi_hit_multiple_keys_same_project(self): from google.cloud.proto.datastore.v1 import datastore_pb2 from google.cloud.datastore.key import Key kind = 'Kind' id1 = 1234 id2 = 2345 # Make a found entity pb to be returned from mock backend. entity_pb1 = _make_entity_pb(self.PROJECT, kind, id1) entity_pb2 = _make_entity_pb(self.PROJECT, kind, id2) # Make a connection to return the entity pbs. creds = _make_credentials() client = self._make_one(credentials=creds) lookup_response = _make_lookup_response( results=[entity_pb1, entity_pb2]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api key1 = Key(kind, id1, project=self.PROJECT) key2 = Key(kind, id2, project=self.PROJECT) retrieved1, retrieved2 = client.get_multi([key1, key2]) # Check values match. self.assertEqual(retrieved1.key.path, key1.path) self.assertEqual(dict(retrieved1), {}) self.assertEqual(retrieved2.key.path, key2.path) self.assertEqual(dict(retrieved2), {}) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( self.PROJECT, read_options, [key1.to_protobuf(), key2.to_protobuf()])
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore_v1.proto 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, [key2_pb], read_options=read_options, ) ds_api.lookup.assert_any_call( self.PROJECT, [key1_pb, key2_pb], read_options=read_options, )
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore_v1.proto 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, [key2_pb], read_options=read_options, ) ds_api.lookup.assert_any_call( self.PROJECT, [key1_pb, key2_pb], read_options=read_options, )
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from google.cloud.datastore._generated 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 client._connection._add_lookup_result([entity1_pb], deferred=[key2_pb]) client._connection._add_lookup_result([entity2_pb]) 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) cw = client._connection._lookup_cw self.assertEqual(len(cw), 2) ds_id, k_pbs, eventual, tid = cw[0] self.assertEqual(ds_id, self.PROJECT) self.assertEqual(len(k_pbs), 2) self.assertEqual(key1_pb, k_pbs[0]) self.assertEqual(key2_pb, k_pbs[1]) self.assertFalse(eventual) self.assertIsNone(tid) ds_id, k_pbs, eventual, tid = cw[1] self.assertEqual(ds_id, self.PROJECT) self.assertEqual(len(k_pbs), 1) self.assertEqual(key2_pb, k_pbs[0]) self.assertFalse(eventual) self.assertIsNone(tid)
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from google.cloud.datastore._generated 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 = object() client = self._makeOne(credentials=creds) # mock up two separate requests client.connection._add_lookup_result([entity1_pb], deferred=[key2_pb]) client.connection._add_lookup_result([entity2_pb]) 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) cw = client.connection._lookup_cw self.assertEqual(len(cw), 2) ds_id, k_pbs, eventual, tid = cw[0] self.assertEqual(ds_id, self.PROJECT) self.assertEqual(len(k_pbs), 2) self.assertEqual(key1_pb, k_pbs[0]) self.assertEqual(key2_pb, k_pbs[1]) self.assertFalse(eventual) self.assertIsNone(tid) ds_id, k_pbs, eventual, tid = cw[1] self.assertEqual(ds_id, self.PROJECT) self.assertEqual(len(k_pbs), 1) self.assertEqual(key2_pb, k_pbs[0]) self.assertFalse(eventual) self.assertIsNone(tid)
def test_get_multi_miss_w_deferred(self): from google.cloud.datastore.key import Key key = Key('Kind', 1234, project=self.PROJECT) # Set deferred entity on mock connection. creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result(deferred=[key.to_protobuf()]) deferred = [] entities = client.get_multi([key], deferred=deferred) self.assertEqual(entities, []) self.assertEqual([def_key.to_protobuf() for def_key in deferred], [key.to_protobuf()])
def test_get_multi_miss_w_deferred(self): from google.cloud.datastore.key import Key key = Key('Kind', 1234, project=self.PROJECT) # Set deferred entity on mock connection. creds = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result(deferred=[key.to_protobuf()]) deferred = [] entities = client.get_multi([key], deferred=deferred) self.assertEqual(entities, []) self.assertEqual([def_key.to_protobuf() for def_key in deferred], [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_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_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_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_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_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_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_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_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_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 = object() client = self._makeOne(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_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_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_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 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 test_ancestor(self): from google.cloud.datastore.key import Key from google.cloud.datastore_v1.types import query as query_pb2 ancestor = Key("Ancestor", 123, project="PROJECT") pb = self._call_fut(_Query(ancestor=ancestor)) cfilter = pb.filter.composite_filter self.assertEqual(cfilter.op, query_pb2.CompositeFilter.Operator.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, "__key__") ancestor_pb = ancestor.to_protobuf() self.assertEqual(pfilter.value.key_value, ancestor_pb)
def test_ancestor(self): from google.cloud.datastore.key import Key from google.cloud.datastore._generated import query_pb2 ancestor = Key('Ancestor', 123, project='PROJECT') pb = self._callFUT(_Query(ancestor=ancestor)) cfilter = pb.filter.composite_filter self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, '__key__') ancestor_pb = ancestor.to_protobuf() self.assertEqual(pfilter.value.key_value, ancestor_pb)
def test_ancestor(self): from google.cloud.datastore.key import Key from google.cloud.datastore_v1.proto import query_pb2 ancestor = Key("Ancestor", 123, project="PROJECT") pb = self._call_fut(_Query(ancestor=ancestor)) cfilter = pb.filter.composite_filter self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, "__key__") ancestor_pb = ancestor.to_protobuf() self.assertEqual(pfilter.value.key_value, ancestor_pb)
def test_filter_key(self): from google.cloud.datastore.key import Key from google.cloud.datastore_v1.types import query as query_pb2 key = Key("Kind", 123, project="PROJECT") query = _Query(filters=[("__key__", "=", key)]) query.OPERATORS = {"=": query_pb2.PropertyFilter.Operator.EQUAL} pb = self._call_fut(query) cfilter = pb.filter.composite_filter self.assertEqual(cfilter.op, query_pb2.CompositeFilter.Operator.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, "__key__") key_pb = key.to_protobuf() self.assertEqual(pfilter.value.key_value, key_pb)
def test_filter_key(self): from google.cloud.datastore.key import Key from google.cloud.datastore_v1.proto import query_pb2 key = Key("Kind", 123, project="PROJECT") query = _Query(filters=[("__key__", "=", key)]) query.OPERATORS = {"=": query_pb2.PropertyFilter.EQUAL} pb = self._call_fut(query) cfilter = pb.filter.composite_filter self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, "__key__") key_pb = key.to_protobuf() self.assertEqual(pfilter.value.key_value, key_pb)
def test_get_multi_miss(self): from google.cloud.proto.datastore.v1 import datastore_pb2 from google.cloud.datastore.key import Key creds = _make_credentials() client = self._make_one(credentials=creds) ds_api = _make_datastore_api() client._datastore_api_internal = ds_api key = Key('Kind', 1234, project=self.PROJECT) results = client.get_multi([key]) self.assertEqual(results, []) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with(self.PROJECT, read_options, [key.to_protobuf()])
def test_get_multi_miss(self): from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore.key import Key creds = _make_credentials() client = self._make_one(credentials=creds) ds_api = _make_datastore_api() client._datastore_api_internal = ds_api key = Key("Kind", 1234, project=self.PROJECT) results = client.get_multi([key]) self.assertEqual(results, []) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( self.PROJECT, [key.to_protobuf()], read_options=read_options )
def test_filter_key(self): from google.cloud.datastore.key import Key from google.cloud.datastore._generated import query_pb2 key = Key('Kind', 123, project='PROJECT') query = _Query(filters=[('__key__', '=', key)]) query.OPERATORS = { '=': query_pb2.PropertyFilter.EQUAL, } pb = self._callFUT(query) cfilter = pb.filter.composite_filter self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, '__key__') key_pb = key.to_protobuf() self.assertEqual(pfilter.value.key_value, key_pb)
def test_entity_w_key(self): from google.cloud.datastore.entity import Entity from google.cloud.datastore.key import Key name = "foo" value = u"Foo" pb = self._makePB() key = Key("KIND", 123, project="PROJECT") entity = Entity(key=key) entity[name] = value self._call_fut(pb, entity) entity_pb = pb.entity_value self.assertEqual(entity_pb.key, key.to_protobuf()._pb) prop_dict = dict(entity_pb.properties.items()) self.assertEqual(len(prop_dict), 1) self.assertEqual(list(prop_dict.keys()), [name]) self.assertEqual(prop_dict[name].string_value, value)
def test_entity_w_key(self): from google.cloud.datastore.entity import Entity from google.cloud.datastore.helpers import _property_tuples from google.cloud.datastore.key import Key name = "foo" value = u"Foo" pb = self._makePB() key = Key("KIND", 123, project="PROJECT") entity = Entity(key=key) entity[name] = value self._call_fut(pb, entity) entity_pb = pb.entity_value self.assertEqual(entity_pb.key, key.to_protobuf()) prop_dict = dict(_property_tuples(entity_pb)) self.assertEqual(len(prop_dict), 1) self.assertEqual(list(prop_dict.keys()), [name]) self.assertEqual(prop_dict[name].string_value, value)
def test_entity_w_key(self): from google.cloud.datastore.entity import Entity from google.cloud.datastore.helpers import _property_tuples from google.cloud.datastore.key import Key name = 'foo' value = u'Foo' pb = self._makePB() key = Key('KIND', 123, project='PROJECT') entity = Entity(key=key) entity[name] = value self._call_fut(pb, entity) entity_pb = pb.entity_value self.assertEqual(entity_pb.key, key.to_protobuf()) prop_dict = dict(_property_tuples(entity_pb)) self.assertEqual(len(prop_dict), 1) self.assertEqual(list(prop_dict.keys()), [name]) self.assertEqual(prop_dict[name].string_value, value)
def test_get_multi_hit_w_transaction(self): from google.cloud.datastore_v1.proto 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, [key.to_protobuf()], read_options=read_options)
def test_get_multi_hit_w_transaction(self): from google.cloud.datastore_v1.proto 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, [key.to_protobuf()], read_options=read_options )