Chromium Code Reviews| 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 (APIDataSource, | 12 from api_data_source import (APIDataSource, |
| 13 _JSCModel, | 13 _JSCModel, |
| 14 _FormatValue, | 14 _FormatValue, |
| 15 _RemoveNoDocs, | 15 _RemoveNoDocs, |
| 16 _InlineDocs) | 16 _InlineDocs) |
| 17 from compiled_file_system import CompiledFileSystem | 17 from compiled_file_system import CompiledFileSystem |
| 18 from file_system import FileNotFoundError | 18 from file_system import FileNotFoundError |
| 19 from local_file_system import LocalFileSystem | 19 from local_file_system import LocalFileSystem |
| 20 from object_store_creator import ObjectStoreCreator | 20 from object_store_creator import ObjectStoreCreator |
| 21 from reference_resolver import ReferenceResolver | 21 from reference_resolver import ReferenceResolver |
| 22 from test_file_system import TestFileSystem | |
| 23 import third_party.json_schema_compiler.json_parse as json_parse | |
| 22 | 24 |
| 23 def _MakeLink(href, text): | 25 def _MakeLink(href, text): |
| 24 return '<a href="%s">%s</a>' % (href, text) | 26 return '<a href="%s">%s</a>' % (href, text) |
| 25 | 27 |
| 26 def _GetType(dict_, name): | 28 def _GetType(dict_, name): |
| 27 for type_ in dict_['types']: | 29 for type_ in dict_['types']: |
| 28 if type_['name'] == name: | 30 if type_['name'] == name: |
| 29 return type_ | 31 return type_ |
| 30 | 32 |
| 33 class FakeAvailabilityFinder(object): | |
| 34 def GetApiAvailability(self, version): | |
| 35 return None | |
| 36 | |
| 31 class FakeSamplesDataSource(object): | 37 class FakeSamplesDataSource(object): |
| 32 def Create(self, request): | 38 def Create(self, request): |
| 33 return {} | 39 return {} |
| 34 | 40 |
| 35 class FakeAPIAndListDataSource(object): | 41 class FakeAPIAndListDataSource(object): |
| 36 def __init__(self, json_data): | 42 def __init__(self, json_data): |
| 37 self._json = json_data | 43 self._json = json_data |
| 38 | 44 |
| 39 def Create(self, *args, **kwargs): | 45 def Create(self, *args, **kwargs): |
| 40 return self | 46 return self |
| 41 | 47 |
| 42 def get(self, key): | 48 def get(self, key): |
| 43 if key not in self._json: | 49 if key not in self._json: |
| 44 raise FileNotFoundError(key) | 50 raise FileNotFoundError(key) |
| 45 return self._json[key] | 51 return self._json[key] |
| 46 | 52 |
| 47 def GetAllNames(self): | 53 def GetAllNames(self): |
| 48 return self._json.keys() | 54 return self._json.keys() |
| 49 | 55 |
| 50 class APIDataSourceTest(unittest.TestCase): | 56 class APIDataSourceTest(unittest.TestCase): |
| 51 def setUp(self): | 57 def setUp(self): |
| 52 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 58 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 59 self._compiled_fs_factory = CompiledFileSystem.Factory( | |
| 60 TestFileSystem(self._LoadJSON(os.path.join( | |
| 61 self._base_path, | |
| 62 'basic_docs_filesystem.json'))), | |
|
not at google - send to devlin
2013/06/05 00:24:09
define this in python somewhere, not a JSON file,
epeterson
2013/06/17 20:05:49
Done.
| |
| 63 ObjectStoreCreator.ForTest()) | |
| 64 def compiled_fs_parse(path, json): | |
| 65 return json_parse.Parse(json) | |
| 66 self._json_cache = self._compiled_fs_factory.Create(compiled_fs_parse, | |
| 67 APIDataSourceTest, | |
| 68 'test') | |
| 53 | 69 |
| 54 def _ReadLocalFile(self, filename): | 70 def _ReadLocalFile(self, filename): |
| 55 with open(os.path.join(self._base_path, filename), 'r') as f: | 71 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 56 return f.read() | 72 return f.read() |
| 57 | 73 |
| 58 def _CreateRefResolver(self, filename): | 74 def _CreateRefResolver(self, filename): |
| 59 data_source = FakeAPIAndListDataSource( | 75 data_source = FakeAPIAndListDataSource( |
| 60 self._LoadJSON(filename)) | 76 self._LoadJSON(filename)) |
| 61 return ReferenceResolver.Factory(data_source, | 77 return ReferenceResolver.Factory(data_source, |
| 62 data_source, | 78 data_source, |
| 63 ObjectStoreCreator.ForTest()).Create() | 79 ObjectStoreCreator.ForTest()).Create() |
| 64 | 80 |
| 65 def _LoadJSON(self, filename): | 81 def _LoadJSON(self, filename): |
| 66 return json.loads(self._ReadLocalFile(filename)) | 82 return json.loads(self._ReadLocalFile(filename)) |
| 67 | 83 |
| 68 def testCreateId(self): | 84 def testCreateId(self): |
| 69 data_source = FakeAPIAndListDataSource( | 85 data_source = FakeAPIAndListDataSource( |
| 70 self._LoadJSON('test_file_data_source.json')) | 86 self._LoadJSON('test_file_data_source.json')) |
| 71 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 87 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 72 self._CreateRefResolver('test_file_data_source.json'), | 88 self._CreateRefResolver('test_file_data_source.json'), |
| 73 False).ToDict() | 89 False, |
| 90 FakeAvailabilityFinder(), | |
| 91 self._json_cache).ToDict() | |
| 74 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 92 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
| 75 self.assertEquals('property-TypeA-b', | 93 self.assertEquals('property-TypeA-b', |
| 76 dict_['types'][0]['properties'][0]['id']) | 94 dict_['types'][0]['properties'][0]['id']) |
| 77 self.assertEquals('method-get', dict_['functions'][0]['id']) | 95 self.assertEquals('method-get', dict_['functions'][0]['id']) |
| 78 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 96 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
| 79 | 97 |
| 80 # TODO(kalman): re-enable this when we have a rebase option. | 98 # TODO(kalman): re-enable this when we have a rebase option. |
| 81 def DISABLED_testToDict(self): | 99 def DISABLED_testToDict(self): |
| 82 filename = 'test_file.json' | 100 filename = 'test_file.json' |
| 83 expected_json = self._LoadJSON('expected_' + filename) | 101 expected_json = self._LoadJSON('expected_' + filename) |
| 84 data_source = FakeAPIAndListDataSource( | 102 data_source = FakeAPIAndListDataSource( |
| 85 self._LoadJSON('test_file_data_source.json')) | 103 self._LoadJSON('test_file_data_source.json')) |
| 86 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 104 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
| 87 self._CreateRefResolver('test_file_data_source.json'), | 105 self._CreateRefResolver('test_file_data_source.json'), |
| 88 False).ToDict() | 106 False, |
| 107 FakeAvailabilityFinder(), | |
| 108 self._json_cache).ToDict() | |
| 89 self.assertEquals(expected_json, dict_) | 109 self.assertEquals(expected_json, dict_) |
| 90 | 110 |
| 91 def testFormatValue(self): | 111 def testFormatValue(self): |
| 92 self.assertEquals('1,234,567', _FormatValue(1234567)) | 112 self.assertEquals('1,234,567', _FormatValue(1234567)) |
| 93 self.assertEquals('67', _FormatValue(67)) | 113 self.assertEquals('67', _FormatValue(67)) |
| 94 self.assertEquals('234,567', _FormatValue(234567)) | 114 self.assertEquals('234,567', _FormatValue(234567)) |
| 95 | 115 |
| 96 def testFormatDescription(self): | 116 def testFormatDescription(self): |
| 97 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 117 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
| 98 self._CreateRefResolver('ref_test_data_source.json'), | 118 self._CreateRefResolver('ref_test_data_source.json'), |
| 99 False).ToDict() | 119 False, |
| 120 FakeAvailabilityFinder(), | |
| 121 self._json_cache).ToDict() | |
| 100 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 122 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
| 101 _GetType(dict_, 'type1')['description']) | 123 _GetType(dict_, 'type1')['description']) |
| 102 self.assertEquals( | 124 self.assertEquals( |
| 103 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 125 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
| 104 _MakeLink('ref_test.html#type-type2', 'type2')), | 126 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 105 _GetType(dict_, 'type2')['description']) | 127 _GetType(dict_, 'type2')['description']) |
| 106 self.assertEquals( | 128 self.assertEquals( |
| 107 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 129 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 108 _MakeLink('ref_test.html#type-type2', 'type2')), | 130 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 109 _GetType(dict_, 'type3')['description']) | 131 _GetType(dict_, 'type3')['description']) |
| 110 | 132 |
| 111 def testRemoveNoDocs(self): | 133 def testRemoveNoDocs(self): |
| 112 d = self._LoadJSON('nodoc_test.json') | 134 d = self._LoadJSON('nodoc_test.json') |
| 113 _RemoveNoDocs(d) | 135 _RemoveNoDocs(d) |
| 114 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 136 self.assertEquals(self._LoadJSON('expected_nodoc.json'), d) |
| 137 | |
| 138 def testGetPermissions(self): | |
| 139 model = _JSCModel(self._LoadJSON('test_file.json')[0], | |
| 140 self._CreateRefResolver('test_file_data_source.json'), | |
| 141 False, | |
| 142 FakeAvailabilityFinder(), | |
| 143 self._json_cache) | |
| 144 self.assertEquals(model._GetPermissions(), | |
| 145 self._LoadJSON('api_permissions_expected.json')) | |
| 146 | |
| 147 def testGetMoreLearning(self): | |
| 148 model = _JSCModel(self._LoadJSON('test_file.json')[0], | |
| 149 self._CreateRefResolver('test_file_data_source.json'), | |
| 150 False, | |
| 151 FakeAvailabilityFinder(), | |
| 152 self._json_cache) | |
| 153 self.assertEquals(model._GetMoreLearning(), | |
| 154 self._LoadJSON('api_learn_more_expected.json')) | |
|
not at google - send to devlin
2013/06/05 00:24:09
please just get this data from the canned file sys
epeterson
2013/06/17 20:05:49
Done, for the tests I added, at least. Should I go
not at google - send to devlin
2013/06/19 15:57:37
Same reply to the other comment.
| |
| 115 | 155 |
| 116 def testInlineDocs(self): | 156 def testInlineDocs(self): |
| 117 schema = { | 157 schema = { |
| 118 "namespace": "storage", | 158 "namespace": "storage", |
| 119 "properties": { | 159 "properties": { |
| 120 "key2": { | 160 "key2": { |
| 121 "description": "second key", | 161 "description": "second key", |
| 122 "$ref": "Key" | 162 "$ref": "Key" |
| 123 }, | 163 }, |
| 124 "key1": { | 164 "key1": { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 "marker": True, | 206 "marker": True, |
| 167 "type": "string" | 207 "type": "string" |
| 168 }, | 208 }, |
| 169 "type": "array", | 209 "type": "array", |
| 170 "id": "KeyList", | 210 "id": "KeyList", |
| 171 "description": "A list of keys" | 211 "description": "A list of keys" |
| 172 } | 212 } |
| 173 ] | 213 ] |
| 174 } | 214 } |
| 175 | 215 |
| 176 inlined_schema = copy.deepcopy(schema) | 216 inlined_schema = deepcopy(schema) |
| 177 _InlineDocs(inlined_schema) | 217 _InlineDocs(inlined_schema) |
| 178 self.assertEqual(expected_schema, inlined_schema) | 218 self.assertEqual(expected_schema, inlined_schema) |
| 179 | 219 |
| 180 | 220 |
| 181 if __name__ == '__main__': | 221 if __name__ == '__main__': |
| 182 unittest.main() | 222 unittest.main() |
| OLD | NEW |