|
| 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 TestClient(unittest2.TestCase): |
| 19 | + |
| 20 | + def _getTargetClass(self): |
| 21 | + from gcloud.dns.client import Client |
| 22 | + return Client |
| 23 | + |
| 24 | + def _makeOne(self, *args, **kw): |
| 25 | + return self._getTargetClass()(*args, **kw) |
| 26 | + |
| 27 | + def test_ctor(self): |
| 28 | + from gcloud.dns.connection import Connection |
| 29 | + PROJECT = 'PROJECT' |
| 30 | + creds = _Credentials() |
| 31 | + http = object() |
| 32 | + client = self._makeOne(project=PROJECT, credentials=creds, http=http) |
| 33 | + self.assertTrue(isinstance(client.connection, Connection)) |
| 34 | + self.assertTrue(client.connection.credentials is creds) |
| 35 | + self.assertTrue(client.connection.http is http) |
| 36 | + |
| 37 | + def test_quotas_defaults(self): |
| 38 | + PROJECT = 'PROJECT' |
| 39 | + PATH = 'projects/%s' % PROJECT |
| 40 | + MANAGED_ZONES = 1234 |
| 41 | + RRS_PER_RRSET = 23 |
| 42 | + RRSETS_PER_ZONE = 345 |
| 43 | + RRSET_ADDITIONS = 456 |
| 44 | + RRSET_DELETIONS = 567 |
| 45 | + TOTAL_SIZE = 67890 |
| 46 | + DATA = { |
| 47 | + 'quota': { |
| 48 | + 'managedZones': str(MANAGED_ZONES), |
| 49 | + 'resourceRecordsPerRrset': str(RRS_PER_RRSET), |
| 50 | + 'rrsetsPerManagedZone': str(RRSETS_PER_ZONE), |
| 51 | + 'rrsetAdditionsPerChange': str(RRSET_ADDITIONS), |
| 52 | + 'rrsetDeletionsPerChange': str(RRSET_DELETIONS), |
| 53 | + 'totalRrdataSizePerChange': str(TOTAL_SIZE), |
| 54 | + } |
| 55 | + } |
| 56 | + CONVERTED = dict([(key, int(value)) |
| 57 | + for key, value in DATA['quota'].items()]) |
| 58 | + creds = _Credentials() |
| 59 | + client = self._makeOne(PROJECT, creds) |
| 60 | + conn = client.connection = _Connection(DATA) |
| 61 | + |
| 62 | + quotas = client.quotas() |
| 63 | + |
| 64 | + self.assertEqual(quotas, CONVERTED) |
| 65 | + |
| 66 | + self.assertEqual(len(conn._requested), 1) |
| 67 | + req = conn._requested[0] |
| 68 | + self.assertEqual(req['method'], 'GET') |
| 69 | + self.assertEqual(req['path'], '/%s' % PATH) |
| 70 | + |
| 71 | + |
| 72 | +class _Credentials(object): |
| 73 | + |
| 74 | + _scopes = None |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def create_scoped_required(): |
| 78 | + return True |
| 79 | + |
| 80 | + def create_scoped(self, scope): |
| 81 | + self._scopes = scope |
| 82 | + return self |
| 83 | + |
| 84 | + |
| 85 | +class _Connection(object): |
| 86 | + |
| 87 | + def __init__(self, *responses): |
| 88 | + self._responses = responses |
| 89 | + self._requested = [] |
| 90 | + |
| 91 | + def api_request(self, **kw): |
| 92 | + self._requested.append(kw) |
| 93 | + response, self._responses = self._responses[0], self._responses[1:] |
| 94 | + return response |
0 commit comments