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__ = "[email protected] (Joe Gregorio)" 19 20import json 21import os 22import 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 33 34LOAD_FEED = """{ 35 "items": [ 36 { 37 "longVal": 42, 38 "kind": "zoo#loadValue", 39 "enumVal": "A String", 40 "anyVal": "", # Anything will do. 41 "nullVal": None, 42 "stringVal": "A String", 43 "doubleVal": 3.14, 44 "booleanVal": True or False, # True or False. 45 }, 46 ], 47 "kind": "zoo#loadFeed", 48}""" 49 50 51class SchemasTest(unittest.TestCase): 52 def setUp(self): 53 f = open(datafile("zoo.json")) 54 discovery = f.read() 55 f.close() 56 discovery = json.loads(discovery) 57 self.sc = Schemas(discovery) 58 59 def test_basic_formatting(self): 60 self.assertEqual( 61 sorted(LOAD_FEED.splitlines()), 62 sorted(self.sc.prettyPrintByName("LoadFeed").splitlines()), 63 ) 64 65 def test_empty_edge_case(self): 66 self.assertTrue("Unknown type" in self.sc.prettyPrintSchema({})) 67 68 def test_simple_object(self): 69 self.assertEqual({}, eval(self.sc.prettyPrintSchema({"type": "object"}))) 70 71 def test_string(self): 72 self.assertEqual( 73 type(""), type(eval(self.sc.prettyPrintSchema({"type": "string"}))) 74 ) 75 76 def test_integer(self): 77 self.assertEqual( 78 type(20), type(eval(self.sc.prettyPrintSchema({"type": "integer"}))) 79 ) 80 81 def test_number(self): 82 self.assertEqual( 83 type(1.2), type(eval(self.sc.prettyPrintSchema({"type": "number"}))) 84 ) 85 86 def test_boolean(self): 87 self.assertEqual( 88 type(True), type(eval(self.sc.prettyPrintSchema({"type": "boolean"}))) 89 ) 90 91 def test_string_default(self): 92 self.assertEqual( 93 "foo", eval(self.sc.prettyPrintSchema({"type": "string", "default": "foo"})) 94 ) 95 96 def test_integer_default(self): 97 self.assertEqual( 98 20, eval(self.sc.prettyPrintSchema({"type": "integer", "default": 20})) 99 ) 100 101 def test_number_default(self): 102 self.assertEqual( 103 1.2, eval(self.sc.prettyPrintSchema({"type": "number", "default": 1.2})) 104 ) 105 106 def test_boolean_default(self): 107 self.assertEqual( 108 False, 109 eval(self.sc.prettyPrintSchema({"type": "boolean", "default": False})), 110 ) 111 112 def test_null(self): 113 self.assertEqual(None, eval(self.sc.prettyPrintSchema({"type": "null"}))) 114 115 def test_any(self): 116 self.assertEqual("", eval(self.sc.prettyPrintSchema({"type": "any"}))) 117 118 def test_array(self): 119 self.assertEqual( 120 [{}], 121 eval( 122 self.sc.prettyPrintSchema( 123 {"type": "array", "items": {"type": "object"}} 124 ) 125 ), 126 ) 127 128 def test_nested_references(self): 129 feed = { 130 "items": [ 131 { 132 "photo": { 133 "hash": "A String", 134 "hashAlgorithm": "A String", 135 "filename": "A String", 136 "type": "A String", 137 "size": 42, 138 }, 139 "kind": "zoo#animal", 140 "etag": "A String", 141 "name": "A String", 142 } 143 ], 144 "kind": "zoo#animalFeed", 145 "etag": "A String", 146 } 147 148 self.assertEqual(feed, eval(self.sc.prettyPrintByName("AnimalFeed"))) 149 150 def test_additional_properties(self): 151 items = { 152 "animals": { 153 "a_key": { 154 "photo": { 155 "hash": "A String", 156 "hashAlgorithm": "A String", 157 "filename": "A String", 158 "type": "A String", 159 "size": 42, 160 }, 161 "kind": "zoo#animal", 162 "etag": "A String", 163 "name": "A String", 164 } 165 }, 166 "kind": "zoo#animalMap", 167 "etag": "A String", 168 } 169 170 self.assertEqual(items, eval(self.sc.prettyPrintByName("AnimalMap"))) 171 172 def test_unknown_name(self): 173 self.assertRaises(KeyError, self.sc.prettyPrintByName, "UknownSchemaThing") 174 175 176if __name__ == "__main__": 177 unittest.main() 178