Example #1
0
    def test_begin_transaction(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = spanner_v1.SpannerClient()

        # Mock request
        session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]',
                                      '[SESSION]')
        options_ = {}

        # Mock response
        id_ = b'27'
        expected_response = {'id': id_}
        expected_response = transaction_pb2.Transaction(**expected_response)
        grpc_stub.BeginTransaction.return_value = expected_response

        response = client.begin_transaction(session, options_)
        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 = spanner_pb2.BeginTransactionRequest(
            session=session, options=options_)
        self.assertEqual(expected_request, actual_request)
Example #2
0
    def test_begin_transaction(self):
        # Setup Expected Response
        id_ = b"27"
        expected_response = {"id": id_}
        expected_response = transaction_pb2.Transaction(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_v1.SpannerClient()

        # Setup Request
        session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]",
                                      "[SESSION]")
        options_ = {}

        response = client.begin_transaction(session, options_)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = spanner_pb2.BeginTransactionRequest(
            session=session, options=options_)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Example #3
0
    def test_begin_transaction(self):
        # Setup Expected Response
        id_ = b'27'
        expected_response = {'id': id_}
        expected_response = transaction_pb2.Transaction(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = spanner_v1.SpannerClient(channel=channel)

        # Setup Request
        session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]',
                                      '[SESSION]')
        options_ = {}

        response = client.begin_transaction(session, options_)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = spanner_pb2.BeginTransactionRequest(
            session=session, options=options_)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request