Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 1 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 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 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 15 | """Unit tests for googleapiclient.schema.""" |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 16 | from __future__ import absolute_import |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 17 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 18 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 19 | |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 20 | import json |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 21 | import os |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 22 | import unittest2 as unittest |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 23 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 24 | from googleapiclient.schema import Schemas |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 25 | |
| 26 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 27 | DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def datafile(filename): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 31 | return os.path.join(DATA_DIR, filename) |
| 32 | |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 33 | |
| 34 | LOAD_FEED = """{ |
Anthonios Partheniou | b1b0c83 | 2020-12-14 14:24:19 -0500 | [diff] [blame] | 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 | }""" |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 49 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 50 | |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 51 | class SchemasTest(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 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) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 58 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 59 | def test_basic_formatting(self): |
| 60 | self.assertEqual( |
| 61 | sorted(LOAD_FEED.splitlines()), |
| 62 | sorted(self.sc.prettyPrintByName("LoadFeed").splitlines()), |
| 63 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 64 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 65 | def test_empty_edge_case(self): |
| 66 | self.assertTrue("Unknown type" in self.sc.prettyPrintSchema({})) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 67 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 68 | def test_simple_object(self): |
| 69 | self.assertEqual({}, eval(self.sc.prettyPrintSchema({"type": "object"}))) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 70 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 71 | def test_string(self): |
| 72 | self.assertEqual( |
| 73 | type(""), type(eval(self.sc.prettyPrintSchema({"type": "string"}))) |
| 74 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 75 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 76 | def test_integer(self): |
| 77 | self.assertEqual( |
| 78 | type(20), type(eval(self.sc.prettyPrintSchema({"type": "integer"}))) |
| 79 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 80 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 81 | def test_number(self): |
| 82 | self.assertEqual( |
| 83 | type(1.2), type(eval(self.sc.prettyPrintSchema({"type": "number"}))) |
| 84 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 85 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 86 | def test_boolean(self): |
| 87 | self.assertEqual( |
| 88 | type(True), type(eval(self.sc.prettyPrintSchema({"type": "boolean"}))) |
| 89 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 90 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 91 | def test_string_default(self): |
| 92 | self.assertEqual( |
| 93 | "foo", eval(self.sc.prettyPrintSchema({"type": "string", "default": "foo"})) |
| 94 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 95 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 96 | def test_integer_default(self): |
| 97 | self.assertEqual( |
| 98 | 20, eval(self.sc.prettyPrintSchema({"type": "integer", "default": 20})) |
| 99 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 100 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 101 | def test_number_default(self): |
| 102 | self.assertEqual( |
| 103 | 1.2, eval(self.sc.prettyPrintSchema({"type": "number", "default": 1.2})) |
| 104 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 105 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 106 | def test_boolean_default(self): |
| 107 | self.assertEqual( |
| 108 | False, |
| 109 | eval(self.sc.prettyPrintSchema({"type": "boolean", "default": False})), |
| 110 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 111 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 112 | def test_null(self): |
| 113 | self.assertEqual(None, eval(self.sc.prettyPrintSchema({"type": "null"}))) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 114 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 115 | def test_any(self): |
| 116 | self.assertEqual("", eval(self.sc.prettyPrintSchema({"type": "any"}))) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 117 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 118 | def test_array(self): |
| 119 | self.assertEqual( |
| 120 | [{}], |
| 121 | eval( |
| 122 | self.sc.prettyPrintSchema( |
| 123 | {"type": "array", "items": {"type": "object"}} |
| 124 | ) |
| 125 | ), |
| 126 | ) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 127 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 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 | } |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 147 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 148 | self.assertEqual(feed, eval(self.sc.prettyPrintByName("AnimalFeed"))) |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 149 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 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 | } |
Joe Gregorio | 7f371e1 | 2012-07-12 16:04:33 -0400 | [diff] [blame] | 169 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 170 | self.assertEqual(items, eval(self.sc.prettyPrintByName("AnimalMap"))) |
Joe Gregorio | 7f371e1 | 2012-07-12 16:04:33 -0400 | [diff] [blame] | 171 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 172 | def test_unknown_name(self): |
| 173 | self.assertRaises(KeyError, self.sc.prettyPrintByName, "UknownSchemaThing") |
Joe Gregorio | 2b78128 | 2011-12-08 12:00:25 -0500 | [diff] [blame] | 174 | |
| 175 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 176 | if __name__ == "__main__": |
| 177 | unittest.main() |