blob: 7430ac7e47f723ee9f2ca3e39b51440b519babb5 [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
15"""Unit tests for apiclient.schema."""
16
17__author__ = 'jcgregorio@google.com (Joe Gregorio)'
18
19import os
20import unittest
21import StringIO
22
23from apiclient.anyjson import simplejson
24from apiclient.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
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",
42 "doubleVal": 3.140000,
43 "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()
54 discovery = simplejson.loads(discovery)
55 self.sc = Schemas(discovery)
56
57 def test_basic_formatting(self):
58 self.assertEqual(LOAD_FEED, self.sc.prettyPrintByName('LoadFeed'))
59
60 def test_empty_edge_case(self):
61 self.assertTrue('Unknown type' in self.sc.prettyPrintSchema({}))
62
63 def test_simple_object(self):
64 self.assertEqual({}, eval(self.sc.prettyPrintSchema({'type': 'object'})))
65
66 def test_string(self):
67 self.assertEqual(type(""), type(eval(self.sc.prettyPrintSchema({'type':
68 'string'}))))
69
70 def test_integer(self):
71 self.assertEqual(type(20), type(eval(self.sc.prettyPrintSchema({'type':
72 'integer'}))))
73
74 def test_number(self):
75 self.assertEqual(type(1.2), type(eval(self.sc.prettyPrintSchema({'type':
76 'number'}))))
77
78 def test_boolean(self):
79 self.assertEqual(type(True), type(eval(self.sc.prettyPrintSchema({'type':
80 'boolean'}))))
81
82 def test_string_default(self):
83 self.assertEqual('foo', eval(self.sc.prettyPrintSchema({'type':
84 'string', 'default': 'foo'})))
85
86 def test_integer_default(self):
87 self.assertEqual(20, eval(self.sc.prettyPrintSchema({'type':
88 'integer', 'default': 20})))
89
90 def test_number_default(self):
91 self.assertEqual(1.2, eval(self.sc.prettyPrintSchema({'type':
92 'number', 'default': 1.2})))
93
94 def test_boolean_default(self):
95 self.assertEqual(False, eval(self.sc.prettyPrintSchema({'type':
96 'boolean', 'default': False})))
97
98 def test_null(self):
99 self.assertEqual(None, eval(self.sc.prettyPrintSchema({'type': 'null'})))
100
101 def test_any(self):
102 self.assertEqual('', eval(self.sc.prettyPrintSchema({'type': 'any'})))
103
104 def test_array(self):
105 self.assertEqual([{}], eval(self.sc.prettyPrintSchema({'type': 'array',
106 'items': {'type': 'object'}})))
107
108 def test_nested_references(self):
109 feed = {
110 'items': [ {
111 'photo': {
112 'hash': 'A String',
113 'hashAlgorithm': 'A String',
114 'filename': 'A String',
115 'type': 'A String',
116 'size': 42
117 },
118 'kind': 'zoo#animal',
119 'etag': 'A String',
120 'name': 'A String'
121 }
122 ],
123 'kind': 'zoo#animalFeed',
124 'etag': 'A String'
125 }
126
127 self.assertEqual(feed, eval(self.sc.prettyPrintByName('AnimalFeed')))
128
129 def test_unknown_name(self):
130 self.assertRaises(KeyError,
131 self.sc.prettyPrintByName, 'UknownSchemaThing')
132
133
134if __name__ == '__main__':
135 unittest.main()
136