def test_get_multi_diff_datasets_w_prefix(self): from gcloud.datastore.key import Key DATASET_ID1 = 'e~DATASET' DATASET_ID2 = 's~DATASET-ALT' key1 = Key('KIND', 1234, dataset_id=DATASET_ID1) key2 = Key('KIND', 1234, dataset_id=DATASET_ID2) creds = object() client = self._makeOne(credentials=creds) with self.assertRaises(ValueError): client.get_multi([key1, key2])
def test_get_multi_diff_projects_w_prefix(self): from gcloud.datastore.key import Key PROJECT1 = 'e~PROJECT' PROJECT2 = 's~PROJECT-ALT' 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_get_multi_hit_multiple_keys_different_dataset(self): from gcloud.datastore.key import Key DATASET_ID1 = 'DATASET' DATASET_ID2 = 'DATASET-ALT' # Make sure our IDs are actually different. self.assertNotEqual(DATASET_ID1, DATASET_ID2) key1 = Key('KIND', 1234, dataset_id=DATASET_ID1) key2 = Key('KIND', 1234, dataset_id=DATASET_ID2) client = self._makeOne(connection=object()) with self.assertRaises(ValueError): client.get_multi([key1, key2])
def test_get_multi_hit_w_transaction(self): from gcloud.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 = object() client = self._makeOne(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.assertFalse(new_key is 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_get_multi_hit(self): from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection 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.DATASET_ID, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. connection = _Connection(entity_pb) client = self._makeOne(connection=connection) key = Key(KIND, ID, dataset_id=self.DATASET_ID) result, = client.get_multi([key]) new_key = result.key # Check the returned value is as expected. self.assertFalse(new_key is key) self.assertEqual(new_key.dataset_id, self.DATASET_ID) self.assertEqual(new_key.path, PATH) self.assertEqual(list(result), ['foo']) self.assertEqual(result['foo'], 'Foo')
def test_get_multi_hit_multiple_keys_same_dataset(self): from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection KIND = 'Kind' ID1 = 1234 ID2 = 2345 # Make a found entity pb to be returned from mock backend. entity_pb1 = _make_entity_pb(self.DATASET_ID, KIND, ID1) entity_pb2 = _make_entity_pb(self.DATASET_ID, KIND, ID2) # Make a connection to return the entity pbs. connection = _Connection(entity_pb1, entity_pb2) client = self._makeOne(connection=connection) key1 = Key(KIND, ID1, dataset_id=self.DATASET_ID) key2 = Key(KIND, ID2, dataset_id=self.DATASET_ID) 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), {})
def test_get_multi_diff_prefixes(self): from gcloud.datastore.key import Key PROJECT1 = 'PROJECT' PROJECT2 = 'e~PROJECT' PROJECT3 = 's~PROJECT' KIND = 'Kind' ID1 = 1234 ID2 = 2345 ID3 = 3456 # Make found entity pbs to be returned from mock backend. entity_pb1 = _make_entity_pb(PROJECT1, KIND, ID1) entity_pb2 = _make_entity_pb(PROJECT2, KIND, ID2) entity_pb3 = _make_entity_pb(PROJECT3, KIND, ID3) creds = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result([entity_pb1, entity_pb2, entity_pb3]) key1 = Key(KIND, ID1, project=PROJECT1) key2 = Key(KIND, ID2, project=PROJECT2) key3 = Key(KIND, ID3, project=PROJECT3) retrieved_all = client.get_multi([key1, key2, key3]) retrieved1, retrieved2, retrieved3 = retrieved_all # Check values & keys match. self.assertEqual(retrieved1.key.path, key1.path) self.assertEqual(retrieved2.key.path, key2.path) self.assertEqual(retrieved3.key.path, key3.path)
def test_get_multi_hit(self): from gcloud.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._makeOne(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.assertFalse(new_key is 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_miss_w_missing(self): from gcloud.datastore._generated import entity_pb2 from gcloud.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.dataset_id = self.PROJECT path_element = missed.key.path_element.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_hit_multiple_keys_same_project(self): from gcloud.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 = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result([entity_pb1, entity_pb2]) 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), {})
def test_get_multi_diff_prefixes(self): from gcloud.datastore.key import Key PROJECT1 = 'PROJECT' PROJECT2 = 'e~PROJECT' PROJECT3 = 's~PROJECT' KIND = 'Kind' ID1 = 1234 ID2 = 2345 ID3 = 3456 # Make found entity pbs to be returned from mock backend. entity_pb1 = _make_entity_pb(PROJECT1, KIND, ID1) entity_pb2 = _make_entity_pb(PROJECT2, KIND, ID2) entity_pb3 = _make_entity_pb(PROJECT3, KIND, ID3) creds = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result( [entity_pb1, entity_pb2, entity_pb3]) key1 = Key(KIND, ID1, project=PROJECT1) key2 = Key(KIND, ID2, project=PROJECT2) key3 = Key(KIND, ID3, project=PROJECT3) retrieved_all = client.get_multi([key1, key2, key3]) retrieved1, retrieved2, retrieved3 = retrieved_all # Check values & keys match. self.assertEqual(retrieved1.key.path, key1.path) self.assertEqual(retrieved2.key.path, key2.path) self.assertEqual(retrieved3.key.path, key3.path)
def test_get_multi_max_loops(self): from gcloud._testing import _Monkey from gcloud.datastore import client as _MUT from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection KIND = 'Kind' ID = 1234 # Make a found entity pb to be returned from mock backend. entity_pb = _make_entity_pb(self.DATASET_ID, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. connection = _Connection(entity_pb) client = self._makeOne(connection=connection) key = Key(KIND, ID, dataset_id=self.DATASET_ID) deferred = [] missing = [] with _Monkey(_MUT, _MAX_LOOPS=-1): 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_hit_multiple_keys_same_project(self): from gcloud.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 = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result([entity_pb1, entity_pb2]) 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), {})
def test_get_multi_max_loops(self): from gcloud._testing import _Monkey from gcloud.datastore import client as _MUT from gcloud.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 = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result([entity_pb]) key = Key(KIND, ID, project=self.PROJECT) deferred = [] missing = [] with _Monkey(_MUT, _MAX_LOOPS=-1): 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_miss_w_missing(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection KIND = 'Kind' ID = 1234 # Make a missing entity pb to be returned from mock backend. missed = datastore_pb.Entity() missed.key.partition_id.dataset_id = self.DATASET_ID path_element = missed.key.path_element.add() path_element.kind = KIND path_element.id = ID # Set missing entity on mock connection. connection = _Connection() connection._missing = [missed] client = self._makeOne(connection=connection) key = Key(KIND, ID, dataset_id=self.DATASET_ID) 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_max_loops(self): from gcloud._testing import _Monkey from gcloud.datastore import client as _MUT from gcloud.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 = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result([entity_pb]) key = Key(KIND, ID, project=self.PROJECT) deferred = [] missing = [] with _Monkey(_MUT, _MAX_LOOPS=-1): 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_hit_multiple_keys_different_project(self): from gcloud.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_get_multi_hit_multiple_keys_different_project(self): from gcloud.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_get_multi_miss(self): from gcloud.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_get_multi_miss(self): from gcloud.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_get_multi_miss(self): from gcloud.datastore.key import Key creds = object() client = self._makeOne(credentials=creds) client.connection._add_lookup_result() key = Key('Kind', 1234, dataset_id=self.DATASET_ID) results = client.get_multi([key]) self.assertEqual(results, [])
def test_get_multi_miss(self): from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection connection = _Connection() client = self._makeOne(connection=connection) key = Key('Kind', 1234, dataset_id=self.DATASET_ID) results = client.get_multi([key]) self.assertEqual(results, [])
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from gcloud.datastore._generated import entity_pb2 from gcloud.datastore.entity import Entity from gcloud.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.assertTrue(isinstance(found[0], Entity)) self.assertEqual(found[0].key.path, key1.path) self.assertEqual(found[0].key.project, key1.project) self.assertTrue(isinstance(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.assertTrue(tid is None) 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.assertTrue(tid is None)
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from gcloud.datastore._generated import entity_pb2 from gcloud.datastore.entity import Entity from gcloud.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.assertTrue(isinstance(found[0], Entity)) self.assertEqual(found[0].key.path, key1.path) self.assertEqual(found[0].key.project, key1.project) self.assertTrue(isinstance(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.assertTrue(tid is None) 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.assertTrue(tid is None)
def test_get_multi_miss_w_deferred(self): from gcloud.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_get_multi_miss_w_deferred(self): from gcloud.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_get_multi_miss_w_deferred(self): from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection key = Key('Kind', 1234, dataset_id=self.DATASET_ID) # Set deferred entity on mock connection. connection = _Connection() connection._deferred = [key.to_protobuf()] client = self._makeOne(connection=connection) 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_no_keys(self): creds = object() client = self._makeOne(credentials=creds) results = client.get_multi([]) self.assertEqual(results, [])
def test_get_multi_w_deferred_from_backend_but_not_passed(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb from gcloud.datastore.connection import Connection from gcloud.datastore.key import Key from gcloud.datastore import test_connection # Shortening name, import line above was too long. cmp_key_after_req = test_connection._compare_key_pb_after_request key1 = Key('Kind', dataset_id=self.DATASET_ID) key2 = Key('Kind', 2345, dataset_id=self.DATASET_ID) key_pb1 = key1.to_protobuf() key_pb2 = key2.to_protobuf() # Build mock first response. rsp_pb1 = datastore_pb.LookupResponse() entity1 = datastore_pb.Entity() entity1.key.CopyFrom(key_pb1) # Add the entity to the "found" part of the response. rsp_pb1.found.add(entity=entity1) # Add the second key to the "deferred" part of the response. rsp_pb1.deferred.add().CopyFrom(key_pb2) # Build mock second response. rsp_pb2 = datastore_pb.LookupResponse() # Add in entity that was deferred. entity2 = datastore_pb.Entity() entity2.key.CopyFrom(key_pb2) rsp_pb2.found.add(entity=entity2) connection = Connection() client = self._makeOne(connection=connection) # Add mock http object to connection with response from above. http = connection._http = _HttpMultiple( ({'status': '200'}, rsp_pb1.SerializeToString()), ({'status': '200'}, rsp_pb2.SerializeToString()), ) 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.assertEqual(found[0].key.path, key1.path) self.assertEqual(found[0].key.dataset_id, key1.dataset_id) self.assertEqual(found[1].key.path, key2.path) self.assertEqual(found[1].key.dataset_id, key2.dataset_id) # Check that our http object was called correctly. cw = http._called_with rq_class = datastore_pb.LookupRequest request = rq_class() self.assertEqual(len(cw), 2) # Make URI to check for requests. URI = '/'.join([ connection.api_base_url, 'datastore', connection.API_VERSION, 'datasets', self.DATASET_ID, 'lookup', ]) # Make sure the first called with argument checks out. self._verifyProtobufCall(cw[0], URI, connection) request.ParseFromString(cw[0]['body']) keys = list(request.key) self.assertEqual(len(keys), 2) cmp_key_after_req(self, key_pb1, keys[0]) cmp_key_after_req(self, key_pb2, keys[1]) # Make sure the second called with argument checks out. self._verifyProtobufCall(cw[1], URI, connection) request.ParseFromString(cw[1]['body']) keys = list(request.key) self.assertEqual(len(keys), 1) cmp_key_after_req(self, key_pb2, keys[0])
def test_get_multi_no_keys(self): connection = object() client = self._makeOne(connection=connection) results = client.get_multi([]) self.assertEqual(results, [])