Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Discovery document tests |
| 6 | |
| 7 | Unit tests for objects created from discovery documents. |
| 8 | """ |
| 9 | |
| 10 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 11 | |
| 12 | from apiclient.discovery import build |
| 13 | import httplib2 |
| 14 | import os |
| 15 | import unittest |
| 16 | |
| 17 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 18 | |
| 19 | class HttpMock(object): |
| 20 | |
| 21 | def __init__(self, filename, headers): |
| 22 | f = file(os.path.join(DATA_DIR, filename), 'r') |
| 23 | self.data = f.read() |
| 24 | f.close() |
| 25 | self.headers = headers |
| 26 | |
| 27 | def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): |
| 28 | return httplib2.Response(self.headers), self.data |
| 29 | |
| 30 | |
| 31 | class Discovery(unittest.TestCase): |
| 32 | def test_method_error_checking(self): |
| 33 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 34 | buzz = build('buzz', 'v1', self.http) |
| 35 | |
| 36 | # Missing required parameters |
| 37 | try: |
| 38 | buzz.activities().list() |
| 39 | self.fail() |
| 40 | except TypeError, e: |
| 41 | self.assertTrue('Missing' in str(e)) |
| 42 | |
| 43 | # Parameter doesn't match regex |
| 44 | try: |
| 45 | buzz.activities().list(scope='@self', userId='') |
| 46 | self.fail() |
| 47 | except TypeError, e: |
| 48 | self.assertTrue('does not match' in str(e)) |
| 49 | |
| 50 | # Parameter doesn't match regex |
| 51 | try: |
| 52 | buzz.activities().list(scope='not@', userId='foo') |
| 53 | self.fail() |
| 54 | except TypeError, e: |
| 55 | self.assertTrue('does not match' in str(e)) |
| 56 | |
| 57 | # Unexpected parameter |
| 58 | try: |
| 59 | buzz.activities().list(flubber=12) |
| 60 | self.fail() |
| 61 | except TypeError, e: |
| 62 | self.assertTrue('unexpected' in str(e)) |
| 63 | |
| 64 | def test_resources(self): |
| 65 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 66 | buzz = build('buzz', 'v1', self.http) |
| 67 | self.assertTrue(getattr(buzz, 'activities')) |
| 68 | self.assertTrue(getattr(buzz, 'search')) |
| 69 | self.assertTrue(getattr(buzz, 'feeds')) |
| 70 | self.assertTrue(getattr(buzz, 'photos')) |
| 71 | self.assertTrue(getattr(buzz, 'people')) |
| 72 | self.assertTrue(getattr(buzz, 'groups')) |
| 73 | self.assertTrue(getattr(buzz, 'comments')) |
| 74 | self.assertTrue(getattr(buzz, 'related')) |
| 75 | |
| 76 | |
| 77 | if __name__ == '__main__': |
| 78 | unittest.main() |