Beispiel #1
0
    def test__begin(self):
        from google.cloud.firestore_v1.gapic import firestore_client
        from google.cloud.firestore_v1.proto import firestore_pb2

        # Create a minimal fake GAPIC with a dummy result.
        firestore_api = mock.create_autospec(
            firestore_client.FirestoreClient, instance=True
        )
        txn_id = b"to-begin"
        response = firestore_pb2.BeginTransactionResponse(transaction=txn_id)
        firestore_api.begin_transaction.return_value = response

        # Attach the fake GAPIC to a real client.
        client = _make_client()
        client._firestore_api_internal = firestore_api

        # Actually make a transaction and ``begin()`` it.
        transaction = self._make_one(client)
        self.assertIsNone(transaction._id)

        ret_val = transaction._begin()
        self.assertIsNone(ret_val)
        self.assertEqual(transaction._id, txn_id)

        # Verify the called mock.
        firestore_api.begin_transaction.assert_called_once_with(
            client._database_string, options_=None, metadata=client._rpc_metadata
        )
Beispiel #2
0
def _make_transaction(txn_id, **txn_kwargs):
    from google.protobuf import empty_pb2
    from google.cloud.firestore_v1.gapic import firestore_client
    from google.cloud.firestore_v1.proto import firestore_pb2
    from google.cloud.firestore_v1.proto import write_pb2
    from google.cloud.firestore_v1.transaction import Transaction

    # Create a fake GAPIC ...
    firestore_api = mock.create_autospec(
        firestore_client.FirestoreClient, instance=True
    )
    # ... with a dummy ``BeginTransactionResponse`` result ...
    begin_response = firestore_pb2.BeginTransactionResponse(transaction=txn_id)
    firestore_api.begin_transaction.return_value = begin_response
    # ... and a dummy ``Rollback`` result ...
    firestore_api.rollback.return_value = empty_pb2.Empty()
    # ... and a dummy ``Commit`` result.
    commit_response = firestore_pb2.CommitResponse(
        write_results=[write_pb2.WriteResult()]
    )
    firestore_api.commit.return_value = commit_response

    # Attach the fake GAPIC to a real client.
    client = _make_client()
    client._firestore_api_internal = firestore_api

    return Transaction(client, **txn_kwargs)
    def test_begin_transaction(self):
        # Setup Expected Response
        transaction = b"-34"
        expected_response = {"transaction": transaction}
        expected_response = firestore_pb2.BeginTransactionResponse(
            **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 = firestore_client.FirestoreClient()

        # Setup Request
        database = client.database_root_path("[PROJECT]", "[DATABASE]")

        response = client.begin_transaction(database)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.BeginTransactionRequest(
            database=database)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request