blob: eb41adf7e07ba28dd3ac3428ca49fcfc952efafd [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
18__author__ = 'jcgregorio@google.com (Joe Gregorio)'
19
Craig Citro6ae34d72014-08-18 23:10:09 -070020import json
Joe Gregorio2b781282011-12-08 12:00:25 -050021import os
22import unittest
23import StringIO
24
John Asmuth864311d2014-04-24 15:46:08 -040025from googleapiclient.schema import Schemas
Joe Gregorio2b781282011-12-08 12:00:25 -050026
27
28DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
29
30
31def datafile(filename):
32 return os.path.join(DATA_DIR, filename)
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",
Joe Gregorio549230c2012-01-11 10:38:05 -050043 "doubleVal": 3.14,
Joe Gregorio2b781282011-12-08 12:00:25 -050044 "booleanVal": True or False, # True or False.
45 },
46 ],
47 "kind": "zoo#loadFeed",
48 }"""
49
50class SchemasTest(unittest.TestCase):
51 def setUp(self):
INADA Naokid898a372015-03-04 03:52:46 +090052 f = open(datafile('zoo.json'))
Joe Gregorio2b781282011-12-08 12:00:25 -050053 discovery = f.read()
54 f.close()
Craig Citro6ae34d72014-08-18 23:10:09 -070055 discovery = json.loads(discovery)
Joe Gregorio2b781282011-12-08 12:00:25 -050056 self.sc = Schemas(discovery)
57
58 def test_basic_formatting(self):
Joe Gregoriod688b6c2012-08-21 15:48:49 -040059 self.assertEqual(sorted(LOAD_FEED.splitlines()),
60 sorted(self.sc.prettyPrintByName('LoadFeed').splitlines()))
Joe Gregorio2b781282011-12-08 12:00:25 -050061
62 def test_empty_edge_case(self):
63 self.assertTrue('Unknown type' in self.sc.prettyPrintSchema({}))
64
65 def test_simple_object(self):
66 self.assertEqual({}, eval(self.sc.prettyPrintSchema({'type': 'object'})))
67
68 def test_string(self):
69 self.assertEqual(type(""), type(eval(self.sc.prettyPrintSchema({'type':
70 'string'}))))
71
72 def test_integer(self):
73 self.assertEqual(type(20), type(eval(self.sc.prettyPrintSchema({'type':
74 'integer'}))))
75
76 def test_number(self):
77 self.assertEqual(type(1.2), type(eval(self.sc.prettyPrintSchema({'type':
78 'number'}))))
79
80 def test_boolean(self):
81 self.assertEqual(type(True), type(eval(self.sc.prettyPrintSchema({'type':
82 'boolean'}))))
83
84 def test_string_default(self):
85 self.assertEqual('foo', eval(self.sc.prettyPrintSchema({'type':
86 'string', 'default': 'foo'})))
87
88 def test_integer_default(self):
89 self.assertEqual(20, eval(self.sc.prettyPrintSchema({'type':
90 'integer', 'default': 20})))
91
92 def test_number_default(self):
93 self.assertEqual(1.2, eval(self.sc.prettyPrintSchema({'type':
94 'number', 'default': 1.2})))
95
96 def test_boolean_default(self):
97 self.assertEqual(False, eval(self.sc.prettyPrintSchema({'type':
98 'boolean', 'default': False})))
99
100 def test_null(self):
101 self.assertEqual(None, eval(self.sc.prettyPrintSchema({'type': 'null'})))
102
103 def test_any(self):
104 self.assertEqual('', eval(self.sc.prettyPrintSchema({'type': 'any'})))
105
106 def test_array(self):
107 self.assertEqual([{}], eval(self.sc.prettyPrintSchema({'type': 'array',
108 'items': {'type': 'object'}})))
109
110 def test_nested_references(self):
111 feed = {
112 'items': [ {
113 'photo': {
114 'hash': 'A String',
115 'hashAlgorithm': 'A String',
116 'filename': 'A String',
117 'type': 'A String',
118 'size': 42
119 },
120 'kind': 'zoo#animal',
121 'etag': 'A String',
122 'name': 'A String'
123 }
124 ],
125 'kind': 'zoo#animalFeed',
126 'etag': 'A String'
127 }
128
129 self.assertEqual(feed, eval(self.sc.prettyPrintByName('AnimalFeed')))
130
Joe Gregorio7f371e12012-07-12 16:04:33 -0400131 def test_additional_properties(self):
132 items = {
133 'animals': {
134 'a_key': {
135 'photo': {
136 'hash': 'A String',
137 'hashAlgorithm': 'A String',
138 'filename': 'A String',
139 'type': 'A String',
140 'size': 42
141 },
142 'kind': 'zoo#animal',
143 'etag': 'A String',
144 'name': 'A String'
145 }
146 },
147 'kind': 'zoo#animalMap',
148 'etag': 'A String'
149 }
150
151 self.assertEqual(items, eval(self.sc.prettyPrintByName('AnimalMap')))
152
Joe Gregorio2b781282011-12-08 12:00:25 -0500153 def test_unknown_name(self):
154 self.assertRaises(KeyError,
155 self.sc.prettyPrintByName, 'UknownSchemaThing')
156
157
158if __name__ == '__main__':
159 unittest.main()