示例#1
0
    def test_begin_transaction(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        project = 'PROJECT'
        transaction = b'TRANSACTION'
        rsp_pb = datastore_pb2.BeginTransactionResponse()
        rsp_pb.transaction = transaction

        # Create mock HTTP and client with response.
        http = Http({'status': '200'}, rsp_pb.SerializeToString())
        client = mock.Mock(_http=http, spec=['_http'])

        # Make request.
        conn = self._make_one(client)
        response = conn.begin_transaction(project)

        # Check the result and verify the callers.
        self.assertEqual(response, rsp_pb)
        uri = '/'.join([
            conn.api_base_url,
            conn.API_VERSION,
            'projects',
            project + ':beginTransaction',
        ])
        cw = http._called_with
        self._verify_protobuf_call(cw, uri, conn)
        request = datastore_pb2.BeginTransactionRequest()
        request.ParseFromString(cw['body'])
        # The RPC-over-HTTP request does not set the project in the request.
        self.assertEqual(request.project_id, u'')
示例#2
0
    def test_begin_transaction(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        project = 'PROJECT'
        transaction = b'TRANSACTION'
        rsp_pb = datastore_pb2.BeginTransactionResponse()
        rsp_pb.transaction = transaction

        # Create mock HTTP and client with response.
        http = _make_requests_session(
            [_make_response(content=rsp_pb.SerializeToString())])
        client = mock.Mock(_http=http,
                           _base_url='test.invalid',
                           spec=['_http', '_base_url'])

        # Make request.
        ds_api = self._make_one(client)
        response = ds_api.begin_transaction(project)

        # Check the result and verify the callers.
        self.assertEqual(response, rsp_pb)

        uri = _build_expected_url(client._base_url, project,
                                  'beginTransaction')
        request = _verify_protobuf_call(
            http, uri, datastore_pb2.BeginTransactionRequest())
        # The RPC-over-HTTP request does not set the project in the request.
        self.assertEqual(request.project_id, u'')
    def test_begin_transaction(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = datastore_client.DatastoreClient()

        # Mock request
        project_id = 'projectId-1969970175'

        # Mock response
        transaction = b'-34'
        expected_response = datastore_pb2.BeginTransactionResponse(
            transaction=transaction)
        grpc_stub.BeginTransaction.return_value = expected_response

        response = client.begin_transaction(project_id)
        self.assertEqual(expected_response, response)

        grpc_stub.BeginTransaction.assert_called_once()
        args, kwargs = grpc_stub.BeginTransaction.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = datastore_pb2.BeginTransactionRequest(
            project_id=project_id)
        self.assertEqual(expected_request, actual_request)
示例#4
0
    def test_begin_transaction(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        PROJECT = 'PROJECT'
        TRANSACTION = b'TRANSACTION'
        rsp_pb = datastore_pb2.BeginTransactionResponse()
        rsp_pb.transaction = TRANSACTION
        http = Http({'status': '200'}, rsp_pb.SerializeToString())
        client = mock.Mock(_http=http, spec=['_http'])
        conn = self._make_one(client)
        URI = '/'.join([
            conn.api_base_url,
            conn.API_VERSION,
            'projects',
            PROJECT + ':beginTransaction',
        ])
        self.assertEqual(conn.begin_transaction(PROJECT), TRANSACTION)
        cw = http._called_with
        self._verify_protobuf_call(cw, URI, conn)
        rq_class = datastore_pb2.BeginTransactionRequest
        request = rq_class()
        request.ParseFromString(cw['body'])
示例#5
0
    def test_it(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        http = object()
        project = 'projectOK'
        method = 'beginTransaction'
        base_url = 'test.invalid'
        request_pb = datastore_pb2.BeginTransactionRequest(project_id=project)

        response_pb = datastore_pb2.BeginTransactionResponse(
            transaction=b'7830rmc')
        patch = mock.patch('google.cloud.datastore._http._request',
                           return_value=response_pb.SerializeToString())
        with patch as mock_request:
            result = self._call_fut(http, project, method, base_url,
                                    request_pb,
                                    datastore_pb2.BeginTransactionResponse)
            self.assertEqual(result, response_pb)

            mock_request.assert_called_once_with(
                http, project, method, request_pb.SerializeToString(),
                base_url)