Beispiel #1
0
    def test_as_context_mgr_w_error(self):
        from gcloud.storage.batch import _FutureDict
        from gcloud.storage.client import Client
        URL = 'http://example.com/api'
        http = _HTTP()
        connection = _Connection(http=http)
        project = 'PROJECT'
        credentials = _Credentials()
        client = Client(project=project, credentials=credentials)
        client._connection = connection

        self.assertEqual(list(client._batch_stack), [])

        target1 = _MockObject()
        target2 = _MockObject()
        target3 = _MockObject()
        try:
            with self._makeOne(client) as batch:
                self.assertEqual(list(client._batch_stack), [batch])
                batch._make_request('POST',
                                    URL, {
                                        'foo': 1,
                                        'bar': 2
                                    },
                                    target_object=target1)
                batch._make_request('PATCH',
                                    URL, {'bar': 3},
                                    target_object=target2)
                batch._make_request('DELETE', URL, target_object=target3)
                raise ValueError()
        except ValueError:
            pass

        self.assertEqual(list(client._batch_stack), [])
        self.assertEqual(len(http._requests), 0)
        self.assertEqual(len(batch._requests), 3)
        self.assertEqual(batch._target_objects, [target1, target2, target3])
        # Since the context manager fails, finish will not get called and
        # the _properties will still be futures.
        self.assertTrue(isinstance(target1._properties, _FutureDict))
        self.assertTrue(isinstance(target2._properties, _FutureDict))
        self.assertTrue(isinstance(target3._properties, _FutureDict))
Beispiel #2
0
    def test_as_context_mgr_w_error(self):
        from gcloud.storage.batch import _FutureDict
        from gcloud.storage.client import Client
        URL = 'http://example.com/api'
        http = _HTTP()
        connection = _Connection(http=http)
        project = 'PROJECT'
        credentials = _Credentials()
        client = Client(project=project, credentials=credentials)
        client._connection = connection

        self.assertEqual(list(client._batch_stack), [])

        target1 = _MockObject()
        target2 = _MockObject()
        target3 = _MockObject()
        try:
            with self._makeOne(client) as batch:
                self.assertEqual(list(client._batch_stack), [batch])
                batch._make_request('POST', URL, {'foo': 1, 'bar': 2},
                                    target_object=target1)
                batch._make_request('PATCH', URL, {'bar': 3},
                                    target_object=target2)
                batch._make_request('DELETE', URL, target_object=target3)
                raise ValueError()
        except ValueError:
            pass

        self.assertEqual(list(client._batch_stack), [])
        self.assertEqual(len(http._requests), 0)
        self.assertEqual(len(batch._requests), 3)
        self.assertEqual(batch._target_objects, [target1, target2, target3])
        # Since the context manager fails, finish will not get called and
        # the _properties will still be futures.
        self.assertTrue(isinstance(target1._properties, _FutureDict))
        self.assertTrue(isinstance(target2._properties, _FutureDict))
        self.assertTrue(isinstance(target3._properties, _FutureDict))