示例#1
0
    def test_lookup_multiple_keys_w_deferred_from_backend_but_not_passed(self):
        from gcloud.datastore.connection import datastore_pb
        from gcloud.datastore.key import Key

        DATASET_ID = 'DATASET'
        key_pb1 = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf()
        key_pb2 = Key(path=[{'kind': 'Kind', 'id': 2345}]).to_protobuf()
        rsp_pb1 = datastore_pb.LookupResponse()
        entity1 = datastore_pb.Entity()
        entity1.key.CopyFrom(key_pb1)
        rsp_pb1.found.add(entity=entity1)
        rsp_pb1.deferred.add().CopyFrom(key_pb2)
        rsp_pb2 = datastore_pb.LookupResponse()
        entity2 = datastore_pb.Entity()
        entity2.key.CopyFrom(key_pb2)
        rsp_pb2.found.add(entity=entity2)
        conn = self._makeOne()
        URI = '/'.join([
            conn.API_BASE_URL,
            'datastore',
            conn.API_VERSION,
            'datasets',
            DATASET_ID,
            'lookup',
        ])
        http = conn._http = HttpMultiple(
            ({
                'status': '200'
            }, rsp_pb1.SerializeToString()),
            ({
                'status': '200'
            }, rsp_pb2.SerializeToString()),
        )
        found = conn.lookup(DATASET_ID, [key_pb1, key_pb2])
        self.assertEqual(len(found), 2)
        self.assertEqual(found[0].key.path_element[0].kind, 'Kind')
        self.assertEqual(found[0].key.path_element[0].id, 1234)
        self.assertEqual(found[1].key.path_element[0].kind, 'Kind')
        self.assertEqual(found[1].key.path_element[0].id, 2345)
        cw = http._called_with
        rq_class = datastore_pb.LookupRequest
        request = rq_class()
        self.assertEqual(len(cw), 2)

        self._verifyProtobufCall(cw[0], URI, conn)
        request.ParseFromString(cw[0]['body'])
        keys = list(request.key)
        self.assertEqual(len(keys), 2)
        self.assertEqual(keys[0], key_pb1)
        self.assertEqual(keys[1], key_pb2)

        self._verifyProtobufCall(cw[1], URI, conn)
        request.ParseFromString(cw[1]['body'])
        keys = list(request.key)
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0], key_pb2)
示例#2
0
    def test_run_query_w_namespace_nonempty_result(self):
        from gcloud.datastore.connection import datastore_pb
        from gcloud.datastore.query import Query

        DATASET_ID = 'DATASET'
        KIND = 'Kind'
        entity = datastore_pb.Entity()
        q_pb = Query(KIND, DATASET_ID).to_protobuf()
        rsp_pb = datastore_pb.RunQueryResponse()
        rsp_pb.batch.entity_result.add(entity=entity)
        rsp_pb.batch.entity_result_type = 1  # FULL
        rsp_pb.batch.more_results = 3  # NO_MORE_RESULTS
        conn = self._makeOne()
        URI = '/'.join([
            conn.API_BASE_URL,
            'datastore',
            conn.API_VERSION,
            'datasets',
            DATASET_ID,
            'runQuery',
        ])
        http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
        pbs = conn.run_query(DATASET_ID, q_pb, 'NS')[0]
        self.assertEqual(len(pbs), 1)
        cw = http._called_with
        self._verifyProtobufCall(cw, URI, conn)
        rq_class = datastore_pb.RunQueryRequest
        request = rq_class()
        request.ParseFromString(cw['body'])
        self.assertEqual(request.partition_id.namespace, 'NS')
        self.assertEqual(request.query, q_pb)
示例#3
0
    def test_lookup_single_key_nonempty_response(self):
        from gcloud.datastore.connection import datastore_pb
        from gcloud.datastore.key import Key

        DATASET_ID = 'DATASET'
        key_pb = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf()
        rsp_pb = datastore_pb.LookupResponse()
        entity = datastore_pb.Entity()
        entity.key.CopyFrom(key_pb)
        rsp_pb.found.add(entity=entity)
        conn = self._makeOne()
        URI = '/'.join([
            conn.API_BASE_URL,
            'datastore',
            conn.API_VERSION,
            'datasets',
            DATASET_ID,
            'lookup',
        ])
        http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
        found = conn.lookup(DATASET_ID, key_pb)
        self.assertEqual(found.key.path_element[0].kind, 'Kind')
        self.assertEqual(found.key.path_element[0].id, 1234)
        cw = http._called_with
        self._verifyProtobufCall(cw, URI, conn)
        rq_class = datastore_pb.LookupRequest
        request = rq_class()
        request.ParseFromString(cw['body'])
        keys = list(request.key)
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0], key_pb)
示例#4
0
    def test_lookup_single_key_nonempty_response(self):
        from gcloud.datastore import _datastore_v1_pb2 as datastore_pb

        DATASET_ID = 'DATASET'
        key_pb = self._make_key_pb(DATASET_ID)
        rsp_pb = datastore_pb.LookupResponse()
        entity = datastore_pb.Entity()
        entity.key.CopyFrom(key_pb)
        rsp_pb.found.add(entity=entity)
        conn = self._makeOne()
        URI = '/'.join([
            conn.api_base_url,
            'datastore',
            conn.API_VERSION,
            'datasets',
            DATASET_ID,
            'lookup',
        ])
        http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
        (found, ), missing, deferred = conn.lookup(DATASET_ID, [key_pb])
        self.assertEqual(len(missing), 0)
        self.assertEqual(len(deferred), 0)
        self.assertEqual(found.key.path_element[0].kind, 'Kind')
        self.assertEqual(found.key.path_element[0].id, 1234)
        cw = http._called_with
        self._verifyProtobufCall(cw, URI, conn)
        rq_class = datastore_pb.LookupRequest
        request = rq_class()
        request.ParseFromString(cw['body'])
        keys = list(request.key)
        self.assertEqual(len(keys), 1)
        _compare_key_pb_after_request(self, key_pb, keys[0])
示例#5
0
 def test_get_entities_miss_w_missing(self):
     from gcloud.datastore.connection import datastore_pb
     from gcloud.datastore.key import Key
     DATASET_ID = 'DATASET'
     KIND = 'Kind'
     ID = 1234
     PATH = [{'kind': KIND, 'id': ID}]
     missed = datastore_pb.Entity()
     missed.key.partition_id.dataset_id = DATASET_ID
     path_element = missed.key.path_element.add()
     path_element.kind = KIND
     path_element.id = ID
     connection = _Connection()
     connection._missing = [missed]
     dataset = self._makeOne(DATASET_ID, connection)
     key = Key(path=PATH, dataset_id=DATASET_ID)
     missing = []
     entities = dataset.get_entities([key], missing=missing)
     self.assertEqual(entities, [])
     self.assertEqual([missed.key().to_protobuf() for missed in missing],
                      [key.to_protobuf()])
示例#6
0
 def test_get_entity_path(self):
     from gcloud.datastore.connection import datastore_pb
     DATASET_ID = 'DATASET'
     KIND = 'Kind'
     ID = 1234
     PATH = [{'kind': KIND, 'id': ID}]
     entity_pb = datastore_pb.Entity()
     entity_pb.key.partition_id.dataset_id = DATASET_ID
     path_element = entity_pb.key.path_element.add()
     path_element.kind = KIND
     path_element.id = ID
     prop = entity_pb.property.add()
     prop.name = 'foo'
     prop.value.string_value = 'Foo'
     connection = _Connection(entity_pb)
     dataset = self._makeOne(DATASET_ID, connection)
     result = dataset.get_entity([KIND, ID])
     key = result.key()
     self.assertEqual(key._dataset_id, DATASET_ID)
     self.assertEqual(key.path(), PATH)
     self.assertEqual(list(result), ['foo'])
     self.assertEqual(result['foo'], 'Foo')