Example #1
0
    def test_write(self):
        # Setup Expected Response
        stream_id = "streamId-315624902"
        stream_token = b"122"
        expected_response = {
            "stream_id": stream_id,
            "stream_token": stream_token
        }
        expected_response = firestore_pb2.WriteResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[iter([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]")
        request = {"database": database}
        request = firestore_pb2.WriteRequest(**request)
        requests = [request]

        response = client.write(requests)
        resources = list(response)
        assert len(resources) == 1
        assert expected_response == resources[0]

        assert len(channel.requests) == 1
        actual_requests = channel.requests[0][1]
        assert len(actual_requests) == 1
        actual_request = list(actual_requests)[0]
        assert request == actual_request
Example #2
0
    def test_write(self):
        # Setup Expected Response
        stream_id = 'streamId-315624902'
        stream_token = b'122'
        expected_response = {
            'stream_id': stream_id,
            'stream_token': stream_token
        }
        expected_response = firestore_pb2.WriteResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[iter([expected_response])])
        client = firestore_client.FirestoreClient(channel=channel)

        # Setup Request
        database = client.database_root_path('[PROJECT]', '[DATABASE]')
        request = {'database': database}
        request = firestore_pb2.WriteRequest(**request)
        requests = [request]

        response = client.write(requests)
        resources = list(response)
        assert len(resources) == 1
        assert expected_response == resources[0]

        assert len(channel.requests) == 1
        actual_requests = channel.requests[0][1]
        assert len(actual_requests) == 1
        actual_request = list(actual_requests)[0]
        assert request == actual_request
Example #3
0
    def test_write(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = firestore_client.FirestoreClient()

        # Mock request
        database = client.database_root_path('[PROJECT]', '[DATABASE]')
        request = {'database': database}
        requests = [request]

        # Mock response
        stream_id = 'streamId-315624902'
        stream_token = b'122'
        expected_response = {
            'stream_id': stream_id,
            'stream_token': stream_token
        }
        expected_response = firestore_pb2.WriteResponse(**expected_response)
        grpc_stub.Write.return_value = iter([expected_response])

        response = client.write(requests)
        resources = list(response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response, resources[0])

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