blob: ec6dd42506f72a3c45ec423a1d815d7b6b1d1aea [file] [log] [blame]
Joe Gregorio2b781282011-12-08 12:00:25 -05001# Copyright 2011 Google Inc.
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 Asmuth864311d2014-04-24 15:46:08 -040015"""Unit tests for googleapiclient.schema."""
Joe Gregorio2b781282011-12-08 12:00:25 -050016
17__author__ = 'jcgregorio@google.com (Joe Gregorio)'
18
Craig Citro6ae34d72014-08-18 23:10:09 -070019import json
Joe Gregorio2b781282011-12-08 12:00:25 -050020import os
21import unittest
22import StringIO
23
John Asmuth864311d2014-04-24 15:46:08 -040024from googleapiclient.schema import Schemas
Joe Gregorio2b781282011-12-08 12:00:25 -050025
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",
Joe Gregorio549230c2012-01-11 10:38:05 -050042 "doubleVal": 3.14,
Joe Gregorio2b781282011-12-08 12:00:25 -050043 "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 = file(datafile('zoo.json'))
52 discovery = f.read()
53 f.close()
Craig Citro6ae34d72014-08-18 23:10:09 -070054 discovery = json.loads(discovery)
Joe Gregorio2b781282011-12-08 12:00:25 -050055 self.sc = Schemas(discovery)
56
57 def test_basic_formatting(self):
Joe Gregoriod688b6c2012-08-21 15:48:49 -040058 self.assertEqual(sorted(LOAD_FEED.splitlines()),
59 sorted(self.sc.prettyPrintByName('LoadFeed').splitlines()))
Joe Gregorio2b781282011-12-08 12:00:25 -050060
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
Joe Gregorio7f371e12012-07-12 16:04:33 -0400130 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
Joe Gregorio2b781282011-12-08 12:00:25 -0500152 def test_unknown_name(self):
153 self.assertRaises(KeyError,
154 self.sc.prettyPrintByName, 'UknownSchemaThing')
155
156
157if __name__ == '__main__':
158 unittest.main()