blob: f5fd518f5b2510b9bfb66c528e0ee76f5516f835 [file] [log] [blame]
Craig Citro751b7fb2014-09-23 11:20:38 -07001# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio2b781282011-12-08 12:00:25 -05002#
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 Asmuth864311d2014-04-24 15:46:08 -040015"""Unit tests for googleapiclient.schema."""
INADA Naokid898a372015-03-04 03:52:46 +090016from __future__ import absolute_import
Joe Gregorio2b781282011-12-08 12:00:25 -050017
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070018__author__ = "jcgregorio@google.com (Joe Gregorio)"
Joe Gregorio2b781282011-12-08 12:00:25 -050019
Craig Citro6ae34d72014-08-18 23:10:09 -070020import json
Joe Gregorio2b781282011-12-08 12:00:25 -050021import os
Pat Ferate497a90f2015-03-09 09:52:54 -070022import unittest2 as unittest
Joe Gregorio2b781282011-12-08 12:00:25 -050023
John Asmuth864311d2014-04-24 15:46:08 -040024from googleapiclient.schema import Schemas
Joe Gregorio2b781282011-12-08 12:00:25 -050025
26
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070027DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
Joe Gregorio2b781282011-12-08 12:00:25 -050028
29
30def datafile(filename):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070031 return os.path.join(DATA_DIR, filename)
32
Joe Gregorio2b781282011-12-08 12:00:25 -050033
34LOAD_FEED = """{
Anthonios Parthenioub1b0c832020-12-14 14:24:19 -050035 "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 Gregorio2b781282011-12-08 12:00:25 -050049
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070050
Joe Gregorio2b781282011-12-08 12:00:25 -050051class SchemasTest(unittest.TestCase):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070052 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 Gregorio2b781282011-12-08 12:00:25 -050058
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070059 def test_basic_formatting(self):
60 self.assertEqual(
61 sorted(LOAD_FEED.splitlines()),
62 sorted(self.sc.prettyPrintByName("LoadFeed").splitlines()),
63 )
Joe Gregorio2b781282011-12-08 12:00:25 -050064
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070065 def test_empty_edge_case(self):
66 self.assertTrue("Unknown type" in self.sc.prettyPrintSchema({}))
Joe Gregorio2b781282011-12-08 12:00:25 -050067
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070068 def test_simple_object(self):
69 self.assertEqual({}, eval(self.sc.prettyPrintSchema({"type": "object"})))
Joe Gregorio2b781282011-12-08 12:00:25 -050070
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070071 def test_string(self):
72 self.assertEqual(
73 type(""), type(eval(self.sc.prettyPrintSchema({"type": "string"})))
74 )
Joe Gregorio2b781282011-12-08 12:00:25 -050075
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070076 def test_integer(self):
77 self.assertEqual(
78 type(20), type(eval(self.sc.prettyPrintSchema({"type": "integer"})))
79 )
Joe Gregorio2b781282011-12-08 12:00:25 -050080
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070081 def test_number(self):
82 self.assertEqual(
83 type(1.2), type(eval(self.sc.prettyPrintSchema({"type": "number"})))
84 )
Joe Gregorio2b781282011-12-08 12:00:25 -050085
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070086 def test_boolean(self):
87 self.assertEqual(
88 type(True), type(eval(self.sc.prettyPrintSchema({"type": "boolean"})))
89 )
Joe Gregorio2b781282011-12-08 12:00:25 -050090
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070091 def test_string_default(self):
92 self.assertEqual(
93 "foo", eval(self.sc.prettyPrintSchema({"type": "string", "default": "foo"}))
94 )
Joe Gregorio2b781282011-12-08 12:00:25 -050095
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070096 def test_integer_default(self):
97 self.assertEqual(
98 20, eval(self.sc.prettyPrintSchema({"type": "integer", "default": 20}))
99 )
Joe Gregorio2b781282011-12-08 12:00:25 -0500100
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700101 def test_number_default(self):
102 self.assertEqual(
103 1.2, eval(self.sc.prettyPrintSchema({"type": "number", "default": 1.2}))
104 )
Joe Gregorio2b781282011-12-08 12:00:25 -0500105
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700106 def test_boolean_default(self):
107 self.assertEqual(
108 False,
109 eval(self.sc.prettyPrintSchema({"type": "boolean", "default": False})),
110 )
Joe Gregorio2b781282011-12-08 12:00:25 -0500111
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700112 def test_null(self):
113 self.assertEqual(None, eval(self.sc.prettyPrintSchema({"type": "null"})))
Joe Gregorio2b781282011-12-08 12:00:25 -0500114
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700115 def test_any(self):
116 self.assertEqual("", eval(self.sc.prettyPrintSchema({"type": "any"})))
Joe Gregorio2b781282011-12-08 12:00:25 -0500117
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700118 def test_array(self):
119 self.assertEqual(
120 [{}],
121 eval(
122 self.sc.prettyPrintSchema(
123 {"type": "array", "items": {"type": "object"}}
124 )
125 ),
126 )
Joe Gregorio2b781282011-12-08 12:00:25 -0500127
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700128 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 Gregorio2b781282011-12-08 12:00:25 -0500147
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700148 self.assertEqual(feed, eval(self.sc.prettyPrintByName("AnimalFeed")))
Joe Gregorio2b781282011-12-08 12:00:25 -0500149
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700150 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 Gregorio7f371e12012-07-12 16:04:33 -0400169
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700170 self.assertEqual(items, eval(self.sc.prettyPrintByName("AnimalMap")))
Joe Gregorio7f371e12012-07-12 16:04:33 -0400171
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700172 def test_unknown_name(self):
173 self.assertRaises(KeyError, self.sc.prettyPrintByName, "UknownSchemaThing")
Joe Gregorio2b781282011-12-08 12:00:25 -0500174
175
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700176if __name__ == "__main__":
177 unittest.main()