| 1 |
# Copyright 2014 Google Inc. All Rights Reserved. |
| 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 googleapiclient.schema.""" |
| 16 |
from __future__ import absolute_import |
| 17 |
|
| 18 |
__author__ = "[email protected] (Joe Gregorio)" |
| 19 |
|
| 20 |
import json |
| 21 |
import os |
| 22 |
import unittest |
| 23 |
|
| 24 |
from googleapiclient.schema import Schemas |
| 25 |
|
| 26 |
DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
| 27 |
|
| 28 |
|
| 29 |
def datafile(filename): |
| 30 |
return os.path.join(DATA_DIR, filename) |
| 31 |
|
| 32 |
|
| 33 |
LOAD_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.14, |
| 43 |
"booleanVal": True or False, # True or False. |
| 44 |
}, |
| 45 |
], |
| 46 |
"kind": "zoo#loadFeed", |
| 47 |
}""" |
| 48 |
|
| 49 |
|
| 50 |
class SchemasTest(unittest.TestCase): |
| 51 |
def setUp(self): |
| 52 |
f = open(datafile("zoo.json")) |
| 53 |
discovery = f.read() |
| 54 |
f.close() |
| 55 |
discovery = json.loads(discovery) |
| 56 |
self.sc = Schemas(discovery) |
| 57 |
|
| 58 |
def test_basic_formatting(self): |
| 59 |
self.assertEqual( |
| 60 |
sorted(LOAD_FEED.splitlines()), |
| 61 |
sorted(self.sc.prettyPrintByName("LoadFeed").splitlines()), |
| 62 |
) |
| 63 |
|
| 64 |
def test_empty_edge_case(self): |
| 65 |
self.assertTrue("Unknown type" in self.sc.prettyPrintSchema({})) |
| 66 |
|
| 67 |
def test_simple_object(self): |
| 68 |
self.assertEqual({}, eval(self.sc.prettyPrintSchema({"type": "object"}))) |
| 69 |
|
| 70 |
def test_string(self): |
| 71 |
self.assertEqual( |
| 72 |
type(""), type(eval(self.sc.prettyPrintSchema({"type": "string"}))) |
| 73 |
) |
| 74 |
|
| 75 |
def test_integer(self): |
| 76 |
self.assertEqual( |
| 77 |
type(20), type(eval(self.sc.prettyPrintSchema({"type": "integer"}))) |
| 78 |
) |
| 79 |
|
| 80 |
def test_number(self): |
| 81 |
self.assertEqual( |
| 82 |
type(1.2), type(eval(self.sc.prettyPrintSchema({"type": "number"}))) |
| 83 |
) |
| 84 |
|
| 85 |
def test_boolean(self): |
| 86 |
self.assertEqual( |
| 87 |
type(True), type(eval(self.sc.prettyPrintSchema({"type": "boolean"}))) |
| 88 |
) |
| 89 |
|
| 90 |
def test_string_default(self): |
| 91 |
self.assertEqual( |
| 92 |
"foo", eval(self.sc.prettyPrintSchema({"type": "string", "default": "foo"})) |
| 93 |
) |
| 94 |
|
| 95 |
def test_integer_default(self): |
| 96 |
self.assertEqual( |
| 97 |
20, eval(self.sc.prettyPrintSchema({"type": "integer", "default": 20})) |
| 98 |
) |
| 99 |
|
| 100 |
def test_number_default(self): |
| 101 |
self.assertEqual( |
| 102 |
1.2, eval(self.sc.prettyPrintSchema({"type": "number", "default": 1.2})) |
| 103 |
) |
| 104 |
|
| 105 |
def test_boolean_default(self): |
| 106 |
self.assertEqual( |
| 107 |
False, |
| 108 |
eval(self.sc.prettyPrintSchema({"type": "boolean", "default": False})), |
| 109 |
) |
| 110 |
|
| 111 |
def test_null(self): |
| 112 |
self.assertEqual(None, eval(self.sc.prettyPrintSchema({"type": "null"}))) |
| 113 |
|
| 114 |
def test_any(self): |
| 115 |
self.assertEqual("", eval(self.sc.prettyPrintSchema({"type": "any"}))) |
| 116 |
|
| 117 |
def test_array(self): |
| 118 |
self.assertEqual( |
| 119 |
[{}], |
| 120 |
eval( |
| 121 |
self.sc.prettyPrintSchema( |
| 122 |
{"type": "array", "items": {"type": "object"}} |
| 123 |
) |
| 124 |
), |
| 125 |
) |
| 126 |
|
| 127 |
def test_nested_references(self): |
| 128 |
feed = { |
| 129 |
"items": [ |
| 130 |
{ |
| 131 |
"photo": { |
| 132 |
"hash": "A String", |
| 133 |
"hashAlgorithm": "A String", |
| 134 |
"filename": "A String", |
| 135 |
"type": "A String", |
| 136 |
"size": 42, |
| 137 |
}, |
| 138 |
"kind": "zoo#animal", |
| 139 |
"etag": "A String", |
| 140 |
"name": "A String", |
| 141 |
} |
| 142 |
], |
| 143 |
"kind": "zoo#animalFeed", |
| 144 |
"etag": "A String", |
| 145 |
} |
| 146 |
|
| 147 |
self.assertEqual(feed, eval(self.sc.prettyPrintByName("AnimalFeed"))) |
| 148 |
|
| 149 |
def test_additional_properties(self): |
| 150 |
items = { |
| 151 |
"animals": { |
| 152 |
"a_key": { |
| 153 |
"photo": { |
| 154 |
"hash": "A String", |
| 155 |
"hashAlgorithm": "A String", |
| 156 |
"filename": "A String", |
| 157 |
"type": "A String", |
| 158 |
"size": 42, |
| 159 |
}, |
| 160 |
"kind": "zoo#animal", |
| 161 |
"etag": "A String", |
| 162 |
"name": "A String", |
| 163 |
} |
| 164 |
}, |
| 165 |
"kind": "zoo#animalMap", |
| 166 |
"etag": "A String", |
| 167 |
} |
| 168 |
|
| 169 |
self.assertEqual(items, eval(self.sc.prettyPrintByName("AnimalMap"))) |
| 170 |
|
| 171 |
def test_unknown_name(self): |
| 172 |
self.assertRaises(KeyError, self.sc.prettyPrintByName, "UknownSchemaThing") |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
unittest.main() |