| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import copy | 6 from copy import deepcopy |
| 7 import json | 7 import json |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import unittest | 10 import unittest |
| 11 | 11 |
| 12 from api_data_source import (_JSCModel, | 12 from api_data_source import (_JSCModel, |
| 13 _FormatValue, | 13 _FormatValue, |
| 14 _RemoveNoDocs, | 14 _RemoveNoDocs, |
| 15 _DetectInlineableTypes, | 15 _DetectInlineableTypes, |
| 16 _InlineDocs) | 16 _InlineDocs) |
| 17 from collections import namedtuple |
| 18 from compiled_file_system import CompiledFileSystem |
| 17 from file_system import FileNotFoundError | 19 from file_system import FileNotFoundError |
| 18 from object_store_creator import ObjectStoreCreator | 20 from object_store_creator import ObjectStoreCreator |
| 19 from reference_resolver import ReferenceResolver | 21 from reference_resolver import ReferenceResolver |
| 22 from test_data.canned_data import CANNED_TEST_FILE_SYSTEM_DATA |
| 23 from test_file_system import TestFileSystem |
| 24 import third_party.json_schema_compiler.json_parse as json_parse |
| 20 | 25 |
| 21 def _MakeLink(href, text): | 26 def _MakeLink(href, text): |
| 22 return '<a href="%s">%s</a>' % (href, text) | 27 return '<a href="%s">%s</a>' % (href, text) |
| 23 | 28 |
| 24 def _GetType(dict_, name): | 29 def _GetType(dict_, name): |
| 25 for type_ in dict_['types']: | 30 for type_ in dict_['types']: |
| 26 if type_['name'] == name: | 31 if type_['name'] == name: |
| 27 return type_ | 32 return type_ |
| 28 | 33 |
| 34 class FakeAvailabilityFinder(object): |
| 35 AvailabilityInfo = namedtuple('AvailabilityInfo', 'channel version') |
| 36 |
| 37 def GetApiAvailability(self, version): |
| 38 return FakeAvailabilityFinder.AvailabilityInfo('trunk', 'trunk') |
| 39 |
| 40 def StringifyAvailability(self, availability): |
| 41 return availability.channel |
| 42 |
| 29 class FakeSamplesDataSource(object): | 43 class FakeSamplesDataSource(object): |
| 30 def Create(self, request): | 44 def Create(self, request): |
| 31 return {} | 45 return {} |
| 32 | 46 |
| 33 class FakeAPIAndListDataSource(object): | 47 class FakeAPIAndListDataSource(object): |
| 34 def __init__(self, json_data): | 48 def __init__(self, json_data): |
| 35 self._json = json_data | 49 self._json = json_data |
| 36 | 50 |
| 37 def Create(self, *args, **kwargs): | 51 def Create(self, *args, **kwargs): |
| 38 return self | 52 return self |
| 39 | 53 |
| 40 def get(self, key): | 54 def get(self, key): |
| 41 if key not in self._json: | 55 if key not in self._json: |
| 42 raise FileNotFoundError(key) | 56 raise FileNotFoundError(key) |
| 43 return self._json[key] | 57 return self._json[key] |
| 44 | 58 |
| 45 def GetAllNames(self): | 59 def GetAllNames(self): |
| 46 return self._json.keys() | 60 return self._json.keys() |
| 47 | 61 |
| 48 class APIDataSourceTest(unittest.TestCase): | 62 class APIDataSourceTest(unittest.TestCase): |
| 49 def setUp(self): | 63 def setUp(self): |
| 50 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 64 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 65 self._compiled_fs_factory = CompiledFileSystem.Factory( |
| 66 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA), |
| 67 ObjectStoreCreator.ForTest()) |
| 68 self._json_cache = self._compiled_fs_factory.Create( |
| 69 lambda _, json: json_parse.Parse(json), |
| 70 APIDataSourceTest, |
| 71 'test') |
| 51 | 72 |
| 52 def _ReadLocalFile(self, filename): | 73 def _ReadLocalFile(self, filename): |
| 53 with open(os.path.join(self._base_path, filename), 'r') as f: | 74 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 54 return f.read() | 75 return f.read() |
| 55 | 76 |
| 56 def _CreateRefResolver(self, filename): | 77 def _CreateRefResolver(self, filename): |
| 57 data_source = FakeAPIAndListDataSource( | 78 data_source = FakeAPIAndListDataSource( |
| 58 self._LoadJSON(filename)) | 79 self._LoadJSON(filename)) |
| 59 return ReferenceResolver.Factory(data_source, | 80 return ReferenceResolver.Factory(data_source, |
| 60 data_source, | 81 data_source, |
| 61 ObjectStoreCreator.ForTest()).Create() | 82 ObjectStoreCreator.ForTest()).Create() |
| 62 | 83 |
| 63 def _LoadJSON(self, filename): | 84 def _LoadJSON(self, filename): |
| 64 return json.loads(self._ReadLocalFile(filename)) | 85 return json.loads(self._ReadLocalFile(filename)) |
| 65 | 86 |
| 66 def testCreateId(self): | 87 def testCreateId(self): |
| 67 data_source = FakeAPIAndListDataSource( | 88 data_source = FakeAPIAndListDataSource( |
| 68 self._LoadJSON('test_file_data_source.json')) | 89 self._LoadJSON('test_file_data_source.json')) |
| 69 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 90 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 70 self._CreateRefResolver('test_file_data_source.json'), | 91 self._CreateRefResolver('test_file_data_source.json'), |
| 71 False).ToDict() | 92 False, |
| 93 FakeAvailabilityFinder(), |
| 94 self._json_cache).ToDict() |
| 72 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 95 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
| 73 self.assertEquals('property-TypeA-b', | 96 self.assertEquals('property-TypeA-b', |
| 74 dict_['types'][0]['properties'][0]['id']) | 97 dict_['types'][0]['properties'][0]['id']) |
| 75 self.assertEquals('method-get', dict_['functions'][0]['id']) | 98 self.assertEquals('method-get', dict_['functions'][0]['id']) |
| 76 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 99 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
| 77 | 100 |
| 78 # TODO(kalman): re-enable this when we have a rebase option. | 101 # TODO(kalman): re-enable this when we have a rebase option. |
| 79 def DISABLED_testToDict(self): | 102 def DISABLED_testToDict(self): |
| 80 filename = 'test_file.json' | 103 filename = 'test_file.json' |
| 81 expected_json = self._LoadJSON('expected_' + filename) | 104 expected_json = self._LoadJSON('expected_' + filename) |
| 82 data_source = FakeAPIAndListDataSource( | 105 data_source = FakeAPIAndListDataSource( |
| 83 self._LoadJSON('test_file_data_source.json')) | 106 self._LoadJSON('test_file_data_source.json')) |
| 84 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 107 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
| 85 self._CreateRefResolver('test_file_data_source.json'), | 108 self._CreateRefResolver('test_file_data_source.json'), |
| 86 False).ToDict() | 109 False, |
| 110 FakeAvailabilityFinder(), |
| 111 self._json_cache).ToDict() |
| 87 self.assertEquals(expected_json, dict_) | 112 self.assertEquals(expected_json, dict_) |
| 88 | 113 |
| 89 def testFormatValue(self): | 114 def testFormatValue(self): |
| 90 self.assertEquals('1,234,567', _FormatValue(1234567)) | 115 self.assertEquals('1,234,567', _FormatValue(1234567)) |
| 91 self.assertEquals('67', _FormatValue(67)) | 116 self.assertEquals('67', _FormatValue(67)) |
| 92 self.assertEquals('234,567', _FormatValue(234567)) | 117 self.assertEquals('234,567', _FormatValue(234567)) |
| 93 | 118 |
| 94 def testFormatDescription(self): | 119 def testFormatDescription(self): |
| 95 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 120 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
| 96 self._CreateRefResolver('ref_test_data_source.json'), | 121 self._CreateRefResolver('ref_test_data_source.json'), |
| 97 False).ToDict() | 122 False, |
| 123 FakeAvailabilityFinder(), |
| 124 self._json_cache).ToDict() |
| 98 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 125 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
| 99 _GetType(dict_, 'type1')['description']) | 126 _GetType(dict_, 'type1')['description']) |
| 100 self.assertEquals( | 127 self.assertEquals( |
| 101 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 128 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
| 102 _MakeLink('ref_test.html#type-type2', 'type2')), | 129 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 103 _GetType(dict_, 'type2')['description']) | 130 _GetType(dict_, 'type2')['description']) |
| 104 self.assertEquals( | 131 self.assertEquals( |
| 105 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 132 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 106 _MakeLink('ref_test.html#type-type2', 'type2')), | 133 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 107 _GetType(dict_, 'type3')['description']) | 134 _GetType(dict_, 'type3')['description']) |
| 108 | 135 |
| 109 def testRemoveNoDocs(self): | 136 def testRemoveNoDocs(self): |
| 110 d = self._LoadJSON('nodoc_test.json') | 137 d = self._LoadJSON('nodoc_test.json') |
| 111 _RemoveNoDocs(d) | 138 _RemoveNoDocs(d) |
| 112 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 139 self.assertEquals(self._LoadJSON('expected_nodoc.json'), d) |
| 140 |
| 141 def testGetIntroList(self): |
| 142 model = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 143 self._CreateRefResolver('test_file_data_source.json'), |
| 144 False, |
| 145 FakeAvailabilityFinder(), |
| 146 self._json_cache) |
| 147 expected_list = [ |
| 148 { 'title': 'Description', |
| 149 'content': [ |
| 150 { 'text': 'a test api' } |
| 151 ] |
| 152 }, |
| 153 { 'title': 'Availability', |
| 154 'content': [ |
| 155 { 'message': True, |
| 156 'trunk': True, |
| 157 'version': None |
| 158 } |
| 159 ] |
| 160 }, |
| 161 { 'title': 'Permissions', |
| 162 'content': [ |
| 163 { 'perm': 'tester', |
| 164 'text': '"thing1", "thing2"' |
| 165 }, |
| 166 { 'text': 'is an API for testing things.' } |
| 167 ] |
| 168 }, |
| 169 { 'title': 'Learn More', |
| 170 'content': [ |
| 171 { 'link': 'https://tester.test.com/welcome.html', |
| 172 'text': 'Welcome!' |
| 173 } |
| 174 ] |
| 175 } |
| 176 ] |
| 177 self.assertEquals(json.dumps(model._GetIntroTableList()), |
| 178 json.dumps(expected_list)) |
| 113 | 179 |
| 114 def testInlineDocs(self): | 180 def testInlineDocs(self): |
| 115 schema = { | 181 schema = { |
| 116 "namespace": "storage", | 182 "namespace": "storage", |
| 117 "properties": { | 183 "properties": { |
| 118 "key2": { | 184 "key2": { |
| 119 "description": "second key", | 185 "description": "second key", |
| 120 "$ref": "Key" | 186 "$ref": "Key" |
| 121 }, | 187 }, |
| 122 "key1": { | 188 "key1": { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 "marker": True, | 230 "marker": True, |
| 165 "type": "string" | 231 "type": "string" |
| 166 }, | 232 }, |
| 167 "type": "array", | 233 "type": "array", |
| 168 "id": "KeyList", | 234 "id": "KeyList", |
| 169 "description": "A list of keys" | 235 "description": "A list of keys" |
| 170 } | 236 } |
| 171 ] | 237 ] |
| 172 } | 238 } |
| 173 | 239 |
| 174 inlined_schema = copy.deepcopy(schema) | 240 inlined_schema = deepcopy(schema) |
| 175 _InlineDocs(inlined_schema) | 241 _InlineDocs(inlined_schema) |
| 176 self.assertEqual(expected_schema, inlined_schema) | 242 self.assertEqual(expected_schema, inlined_schema) |
| 177 | 243 |
| 178 def testDetectInline(self): | 244 def testDetectInline(self): |
| 179 schema = { | 245 schema = { |
| 180 "types": [ | 246 "types": [ |
| 181 { | 247 { |
| 182 "id": "Key", | 248 "id": "Key", |
| 183 "items": { | 249 "items": { |
| 184 "$ref": "Value" | 250 "$ref": "Value" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 201 } | 267 } |
| 202 ] | 268 ] |
| 203 } | 269 } |
| 204 | 270 |
| 205 _DetectInlineableTypes(schema) | 271 _DetectInlineableTypes(schema) |
| 206 _InlineDocs(schema) | 272 _InlineDocs(schema) |
| 207 self.assertEqual(expected_schema, schema) | 273 self.assertEqual(expected_schema, schema) |
| 208 | 274 |
| 209 if __name__ == '__main__': | 275 if __name__ == '__main__': |
| 210 unittest.main() | 276 unittest.main() |
| OLD | NEW |