本文整理汇总了Python中google.cloud.datastore.key.Key类的典型用法代码示例。如果您正苦于以下问题:Python Key类的具体用法?Python Key怎么用?Python Key使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Key类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_multi_miss_w_deferred
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,
)
示例2: test_get_multi_miss_w_missing
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
)
示例3: test_get_multi_hit_multiple_keys_same_project
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,
)
示例4: test_get_multi_miss_w_missing
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()])
示例5: test_key
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())
示例6: test_get_multi_hit
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,
)
示例7: test_get_multi_w_deferred_from_backend_but_not_passed
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,
)
示例8: test_get_multi_w_deferred_from_backend_but_not_passed
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)
示例9: test_ancestor
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)
示例10: test_ancestor
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)
示例11: test_get_multi_miss_w_deferred
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()])
示例12: test_filter_key
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)
示例13: test_get_multi_miss
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
)
示例14: test_filter_key
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)
示例15: test_entity_w_key
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)