Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 1 | # Copyright 2011 Google Inc. |
| 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 | """Unit tests for apiclient.schema.""" |
| 16 | |
| 17 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 18 | |
| 19 | import os |
| 20 | import unittest |
| 21 | import StringIO |
| 22 | |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 23 | from apiclient.schema import Schemas |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 24 | from oauth2client.anyjson import simplejson |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 25 | |
| 26 | |
| 27 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 28 | |
| 29 | |
| 30 | def datafile(filename): |
| 31 | return os.path.join(DATA_DIR, filename) |
| 32 | |
| 33 | LOAD_FEED = """{ |
| 34 | "items": [ |
| 35 | { |
| 36 | "longVal": 42, |
| 37 | "kind": "zoo#loadValue", |
| 38 | "enumVal": "A String", |
| 39 | "anyVal": "", # Anything will do. |
| 40 | "nullVal": None, |
| 41 | "stringVal": "A String", |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 42 | "doubleVal": 3.14, |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 43 | "booleanVal": True or False, # True or False. |
| 44 | }, |
| 45 | ], |
| 46 | "kind": "zoo#loadFeed", |
| 47 | }""" |
| 48 | |
| 49 | class SchemasTest(unittest.TestCase): |
| 50 | def setUp(self): |
| 51 | f = file(datafile('zoo.json')) |
| 52 | discovery = f.read() |
| 53 | f.close() |
| 54 | discovery = simplejson.loads(discovery) |
| 55 | self.sc = Schemas(discovery) |
| 56 | |
| 57 | def test_basic_formatting(self): |
Joe Gregorio | d688b6c | 2012-08-21 15:48:49 -0400 | [diff] [blame] | 58 | self.assertEqual(sorted(LOAD_FEED.splitlines()), |
| 59 | sorted(self.sc.prettyPrintByName('LoadFeed').splitlines())) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 60 | |
| 61 | def test_empty_edge_case(self): |
| 62 | self.assertTrue('Unknown type' in self.sc.prettyPrintSchema({})) |
| 63 | |
| 64 | def test_simple_object(self): |
| 65 | self.assertEqual({}, eval(self.sc.prettyPrintSchema({'type': 'object'}))) |
| 66 | |
| 67 | def test_string(self): |
| 68 | self.assertEqual(type(""), type(eval(self.sc.prettyPrintSchema({'type': |
| 69 | 'string'})))) |
| 70 | |
| 71 | def test_integer(self): |
| 72 | self.assertEqual(type(20), type(eval(self.sc.prettyPrintSchema({'type': |
| 73 | 'integer'})))) |
| 74 | |
| 75 | def test_number(self): |
| 76 | self.assertEqual(type(1.2), type(eval(self.sc.prettyPrintSchema({'type': |
| 77 | 'number'})))) |
| 78 | |
| 79 | def test_boolean(self): |
| 80 | self.assertEqual(type(True), type(eval(self.sc.prettyPrintSchema({'type': |
| 81 | 'boolean'})))) |
| 82 | |
| 83 | def test_string_default(self): |
| 84 | self.assertEqual('foo', eval(self.sc.prettyPrintSchema({'type': |
| 85 | 'string', 'default': 'foo'}))) |
| 86 | |
| 87 | def test_integer_default(self): |
| 88 | self.assertEqual(20, eval(self.sc.prettyPrintSchema({'type': |
| 89 | 'integer', 'default': 20}))) |
| 90 | |
| 91 | def test_number_default(self): |
| 92 | self.assertEqual(1.2, eval(self.sc.prettyPrintSchema({'type': |
| 93 | 'number', 'default': 1.2}))) |
| 94 | |
| 95 | def test_boolean_default(self): |
| 96 | self.assertEqual(False, eval(self.sc.prettyPrintSchema({'type': |
| 97 | 'boolean', 'default': False}))) |
| 98 | |
| 99 | def test_null(self): |
| 100 | self.assertEqual(None, eval(self.sc.prettyPrintSchema({'type': 'null'}))) |
| 101 | |
| 102 | def test_any(self): |
| 103 | self.assertEqual('', eval(self.sc.prettyPrintSchema({'type': 'any'}))) |
| 104 | |
| 105 | def test_array(self): |
| 106 | self.assertEqual([{}], eval(self.sc.prettyPrintSchema({'type': 'array', |
| 107 | 'items': {'type': 'object'}}))) |
| 108 | |
| 109 | def test_nested_references(self): |
| 110 | feed = { |
| 111 | 'items': [ { |
| 112 | 'photo': { |
| 113 | 'hash': 'A String', |
| 114 | 'hashAlgorithm': 'A String', |
| 115 | 'filename': 'A String', |
| 116 | 'type': 'A String', |
| 117 | 'size': 42 |
| 118 | }, |
| 119 | 'kind': 'zoo#animal', |
| 120 | 'etag': 'A String', |
| 121 | 'name': 'A String' |
| 122 | } |
| 123 | ], |
| 124 | 'kind': 'zoo#animalFeed', |
| 125 | 'etag': 'A String' |
| 126 | } |
| 127 | |
| 128 | self.assertEqual(feed, eval(self.sc.prettyPrintByName('AnimalFeed'))) |
| 129 | |
Joe Gregorio | 7f371e1 | 2012-07-12 16:04:33 -0400 | [diff] [blame] | 130 | def test_additional_properties(self): |
| 131 | items = { |
| 132 | 'animals': { |
| 133 | 'a_key': { |
| 134 | 'photo': { |
| 135 | 'hash': 'A String', |
| 136 | 'hashAlgorithm': 'A String', |
| 137 | 'filename': 'A String', |
| 138 | 'type': 'A String', |
| 139 | 'size': 42 |
| 140 | }, |
| 141 | 'kind': 'zoo#animal', |
| 142 | 'etag': 'A String', |
| 143 | 'name': 'A String' |
| 144 | } |
| 145 | }, |
| 146 | 'kind': 'zoo#animalMap', |
| 147 | 'etag': 'A String' |
| 148 | } |
| 149 | |
| 150 | self.assertEqual(items, eval(self.sc.prettyPrintByName('AnimalMap'))) |
| 151 | |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 152 | def test_unknown_name(self): |
| 153 | self.assertRaises(KeyError, |
| 154 | self.sc.prettyPrintByName, 'UknownSchemaThing') |
| 155 | |
| 156 | |
| 157 | if __name__ == '__main__': |
| 158 | unittest.main() |