Ejemplo n.º 1
0
 def test_connection_getter_with_batch(self):
     from gcloud.storage.batch import Batch
     PROJECT = 'PROJECT'
     CREDENTIALS = _Credentials()
     client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
     batch = Batch(client)
     client._push_batch(batch)
     self.assertTrue(client.connection is not client._connection)
     self.assertTrue(client.connection is batch)
     self.assertTrue(client.current_batch is batch)
Ejemplo n.º 2
0
    def test__push_batch_and__pop_batch(self):
        from gcloud.storage.batch import Batch

        PROJECT = 'PROJECT'
        CREDENTIALS = _Credentials()

        client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
        batch1 = Batch(client)
        batch2 = Batch(client)
        client._push_batch(batch1)
        self.assertEqual(list(client._batch_stack), [batch1])
        self.assertTrue(client.current_batch is batch1)
        client._push_batch(batch2)
        self.assertTrue(client.current_batch is batch2)
        # list(_LocalStack) returns in reverse order.
        self.assertEqual(list(client._batch_stack), [batch2, batch1])
        self.assertTrue(client._pop_batch() is batch2)
        self.assertEqual(list(client._batch_stack), [batch1])
        self.assertTrue(client._pop_batch() is batch1)
        self.assertEqual(list(client._batch_stack), [])
Ejemplo n.º 3
0
    def batch(self):
        """Factory constructor for batch object.

        .. note::
          This will not make an HTTP request; it simply instantiates
          a batch object owned by this client.

        :rtype: :class:`gcloud.storage.batch.Batch`
        :returns: The batch object created.
        """
        return Batch(client=self)
Ejemplo n.º 4
0
def _require_connection(connection=None):
    """Infer a connection from the environment, if not passed explicitly.

    :type connection: :class:`gcloud.storage.connection.Connection`
    :param connection: Optional.

    :rtype: :class:`gcloud.storage.connection.Connection`
    :returns: A connection based on the current environment.
    :raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
             cannot be inferred from the environment.
    """
    # NOTE: We use current Batch directly since it inherits from Connection.
    if connection is None:
        connection = Batch.current()

    if connection is None:
        connection = get_default_connection()

    if connection is None:
        raise EnvironmentError('Connection could not be inferred.')

    return connection
Ejemplo n.º 5
0
def _require_connection(connection=None):
    """Infer a connection from the environment, if not passed explicitly.

    :type connection: :class:`gcloud.storage.connection.Connection`
    :param connection: Optional.

    :rtype: :class:`gcloud.storage.connection.Connection`
    :returns: A connection based on the current environment.
    :raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
             cannot be inferred from the environment.
    """
    # NOTE: We use current Batch directly since it inherits from Connection.
    if connection is None:
        connection = Batch.current()

    if connection is None:
        connection = get_default_connection()

    if connection is None:
        raise EnvironmentError('Connection could not be inferred.')

    return connection