|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest2 |
| 16 | + |
| 17 | + |
| 18 | +class Test__BucketIterator(unittest2.TestCase): |
| 19 | + |
| 20 | + def _getTargetClass(self): |
| 21 | + from gcloud.storage.api import _BucketIterator |
| 22 | + return _BucketIterator |
| 23 | + |
| 24 | + def _makeOne(self, *args, **kw): |
| 25 | + return self._getTargetClass()(*args, **kw) |
| 26 | + |
| 27 | + def test_ctor(self): |
| 28 | + connection = object() |
| 29 | + iterator = self._makeOne(connection) |
| 30 | + self.assertTrue(iterator.connection is connection) |
| 31 | + self.assertEqual(iterator.path, '/b') |
| 32 | + self.assertEqual(iterator.page_number, 0) |
| 33 | + self.assertEqual(iterator.next_page_token, None) |
| 34 | + |
| 35 | + def test_get_items_from_response_empty(self): |
| 36 | + connection = object() |
| 37 | + iterator = self._makeOne(connection) |
| 38 | + self.assertEqual(list(iterator.get_items_from_response({})), []) |
| 39 | + |
| 40 | + def test_get_items_from_response_non_empty(self): |
| 41 | + from gcloud.storage.bucket import Bucket |
| 42 | + BLOB_NAME = 'blob-name' |
| 43 | + response = {'items': [{'name': BLOB_NAME}]} |
| 44 | + connection = object() |
| 45 | + iterator = self._makeOne(connection) |
| 46 | + buckets = list(iterator.get_items_from_response(response)) |
| 47 | + self.assertEqual(len(buckets), 1) |
| 48 | + bucket = buckets[0] |
| 49 | + self.assertTrue(isinstance(bucket, Bucket)) |
| 50 | + self.assertTrue(bucket.connection is connection) |
| 51 | + self.assertEqual(bucket.name, BLOB_NAME) |
| 52 | + |
| 53 | + |
| 54 | +class Test_get_all_buckets(unittest2.TestCase): |
| 55 | + |
| 56 | + def _callFUT(self, connection=None): |
| 57 | + from gcloud.storage.api import get_all_buckets |
| 58 | + return get_all_buckets(connection=connection) |
| 59 | + |
| 60 | + def test_empty(self): |
| 61 | + from gcloud.storage.connection import Connection |
| 62 | + PROJECT = 'project' |
| 63 | + conn = Connection(PROJECT) |
| 64 | + URI = '/'.join([ |
| 65 | + conn.API_BASE_URL, |
| 66 | + 'storage', |
| 67 | + conn.API_VERSION, |
| 68 | + 'b?project=%s' % PROJECT, |
| 69 | + ]) |
| 70 | + http = conn._http = Http( |
| 71 | + {'status': '200', 'content-type': 'application/json'}, |
| 72 | + '{}', |
| 73 | + ) |
| 74 | + buckets = list(self._callFUT(conn)) |
| 75 | + self.assertEqual(len(buckets), 0) |
| 76 | + self.assertEqual(http._called_with['method'], 'GET') |
| 77 | + self.assertEqual(http._called_with['uri'], URI) |
| 78 | + |
| 79 | + def _get_all_buckets_non_empty_helper(self, use_default=False): |
| 80 | + from gcloud.storage._testing import _monkey_defaults |
| 81 | + from gcloud.storage.connection import Connection |
| 82 | + PROJECT = 'project' |
| 83 | + BUCKET_NAME = 'bucket-name' |
| 84 | + conn = Connection(PROJECT) |
| 85 | + URI = '/'.join([ |
| 86 | + conn.API_BASE_URL, |
| 87 | + 'storage', |
| 88 | + conn.API_VERSION, |
| 89 | + 'b?project=%s' % PROJECT, |
| 90 | + ]) |
| 91 | + http = conn._http = Http( |
| 92 | + {'status': '200', 'content-type': 'application/json'}, |
| 93 | + '{"items": [{"name": "%s"}]}' % BUCKET_NAME, |
| 94 | + ) |
| 95 | + |
| 96 | + if use_default: |
| 97 | + with _monkey_defaults(connection=conn): |
| 98 | + buckets = list(self._callFUT()) |
| 99 | + else: |
| 100 | + buckets = list(self._callFUT(conn)) |
| 101 | + |
| 102 | + self.assertEqual(len(buckets), 1) |
| 103 | + self.assertEqual(buckets[0].name, BUCKET_NAME) |
| 104 | + self.assertEqual(http._called_with['method'], 'GET') |
| 105 | + self.assertEqual(http._called_with['uri'], URI) |
| 106 | + |
| 107 | + def test_non_empty(self): |
| 108 | + self._get_all_buckets_non_empty_helper(use_default=False) |
| 109 | + |
| 110 | + def test_non_use_default(self): |
| 111 | + self._get_all_buckets_non_empty_helper(use_default=True) |
| 112 | + |
| 113 | + |
| 114 | +class Http(object): |
| 115 | + |
| 116 | + _called_with = None |
| 117 | + |
| 118 | + def __init__(self, headers, content): |
| 119 | + from httplib2 import Response |
| 120 | + self._response = Response(headers) |
| 121 | + self._content = content |
| 122 | + |
| 123 | + def request(self, **kw): |
| 124 | + self._called_with = kw |
| 125 | + return self._response, self._content |
0 commit comments