1# Copyright 2014 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"""Unit tests for googleapiclient.schema.""" 16from __future__ import absolute_import 17 18__author__ = 'jcgregorio@google.com (Joe Gregorio)' 19 20import json 21import os 22import unittest2 as unittest 23 24from googleapiclient.schema import Schemas 25 26 27DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') 28 29 30def datafile(filename): 31 return os.path.join(DATA_DIR, filename) 32 33LOAD_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", 42 "doubleVal": 3.14, 43 "booleanVal": True or False, # True or False. 44 }, 45 ], 46 "kind": "zoo#loadFeed", 47 }""" 48 49class SchemasTest(unittest.TestCase): 50 def setUp(self): 51 f = open(datafile('zoo.json')) 52 discovery = f.read() 53 f.close() 54 discovery = json.loads(discovery) 55 self.sc = Schemas(discovery) 56 57 def test_basic_formatting(self): 58 self.assertEqual(sorted(LOAD_FEED.splitlines()), 59 sorted(self.sc.prettyPrintByName('LoadFeed').splitlines())) 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 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 152 def test_unknown_name(self): 153 self.assertRaises(KeyError, 154 self.sc.prettyPrintByName, 'UknownSchemaThing') 155 156 157if __name__ == '__main__': 158 unittest.main() 159