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 |
| 62 class FakeTemplateDataSource(object): |
| 63 def get(self, key): |
| 64 return 'handlebar %s' % key |
| 65 |
48 class APIDataSourceTest(unittest.TestCase): | 66 class APIDataSourceTest(unittest.TestCase): |
49 def setUp(self): | 67 def setUp(self): |
50 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 68 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 69 self._compiled_fs_factory = CompiledFileSystem.Factory( |
| 70 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA), |
| 71 ObjectStoreCreator.ForTest()) |
| 72 self._json_cache = self._compiled_fs_factory.Create( |
| 73 lambda _, json: json_parse.Parse(json), |
| 74 APIDataSourceTest, |
| 75 'test') |
51 | 76 |
52 def _ReadLocalFile(self, filename): | 77 def _ReadLocalFile(self, filename): |
53 with open(os.path.join(self._base_path, filename), 'r') as f: | 78 with open(os.path.join(self._base_path, filename), 'r') as f: |
54 return f.read() | 79 return f.read() |
55 | 80 |
56 def _CreateRefResolver(self, filename): | 81 def _CreateRefResolver(self, filename): |
57 data_source = FakeAPIAndListDataSource( | 82 data_source = FakeAPIAndListDataSource( |
58 self._LoadJSON(filename)) | 83 self._LoadJSON(filename)) |
59 return ReferenceResolver.Factory(data_source, | 84 return ReferenceResolver.Factory(data_source, |
60 data_source, | 85 data_source, |
61 ObjectStoreCreator.ForTest()).Create() | 86 ObjectStoreCreator.ForTest()).Create() |
62 | 87 |
63 def _LoadJSON(self, filename): | 88 def _LoadJSON(self, filename): |
64 return json.loads(self._ReadLocalFile(filename)) | 89 return json.loads(self._ReadLocalFile(filename)) |
65 | 90 |
66 def testCreateId(self): | 91 def testCreateId(self): |
67 data_source = FakeAPIAndListDataSource( | 92 data_source = FakeAPIAndListDataSource( |
68 self._LoadJSON('test_file_data_source.json')) | 93 self._LoadJSON('test_file_data_source.json')) |
69 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 94 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
70 self._CreateRefResolver('test_file_data_source.json'), | 95 self._CreateRefResolver('test_file_data_source.json'), |
71 False).ToDict() | 96 False, |
| 97 FakeAvailabilityFinder(), |
| 98 self._json_cache, |
| 99 FakeTemplateDataSource()).ToDict() |
72 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 100 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
73 self.assertEquals('property-TypeA-b', | 101 self.assertEquals('property-TypeA-b', |
74 dict_['types'][0]['properties'][0]['id']) | 102 dict_['types'][0]['properties'][0]['id']) |
75 self.assertEquals('method-get', dict_['functions'][0]['id']) | 103 self.assertEquals('method-get', dict_['functions'][0]['id']) |
76 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 104 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
77 | 105 |
78 # TODO(kalman): re-enable this when we have a rebase option. | 106 # TODO(kalman): re-enable this when we have a rebase option. |
79 def DISABLED_testToDict(self): | 107 def DISABLED_testToDict(self): |
80 filename = 'test_file.json' | 108 filename = 'test_file.json' |
81 expected_json = self._LoadJSON('expected_' + filename) | 109 expected_json = self._LoadJSON('expected_' + filename) |
82 data_source = FakeAPIAndListDataSource( | 110 data_source = FakeAPIAndListDataSource( |
83 self._LoadJSON('test_file_data_source.json')) | 111 self._LoadJSON('test_file_data_source.json')) |
84 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 112 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
85 self._CreateRefResolver('test_file_data_source.json'), | 113 self._CreateRefResolver('test_file_data_source.json'), |
86 False).ToDict() | 114 False, |
| 115 FakeAvailabilityFinder(), |
| 116 self._json_cache, |
| 117 FakeTemplateDataSource()).ToDict() |
87 self.assertEquals(expected_json, dict_) | 118 self.assertEquals(expected_json, dict_) |
88 | 119 |
89 def testFormatValue(self): | 120 def testFormatValue(self): |
90 self.assertEquals('1,234,567', _FormatValue(1234567)) | 121 self.assertEquals('1,234,567', _FormatValue(1234567)) |
91 self.assertEquals('67', _FormatValue(67)) | 122 self.assertEquals('67', _FormatValue(67)) |
92 self.assertEquals('234,567', _FormatValue(234567)) | 123 self.assertEquals('234,567', _FormatValue(234567)) |
93 | 124 |
94 def testFormatDescription(self): | 125 def testFormatDescription(self): |
95 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 126 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
96 self._CreateRefResolver('ref_test_data_source.json'), | 127 self._CreateRefResolver('ref_test_data_source.json'), |
97 False).ToDict() | 128 False, |
| 129 FakeAvailabilityFinder(), |
| 130 self._json_cache, |
| 131 FakeTemplateDataSource()).ToDict() |
98 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 132 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
99 _GetType(dict_, 'type1')['description']) | 133 _GetType(dict_, 'type1')['description']) |
100 self.assertEquals( | 134 self.assertEquals( |
101 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 135 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
102 _MakeLink('ref_test.html#type-type2', 'type2')), | 136 _MakeLink('ref_test.html#type-type2', 'type2')), |
103 _GetType(dict_, 'type2')['description']) | 137 _GetType(dict_, 'type2')['description']) |
104 self.assertEquals( | 138 self.assertEquals( |
105 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 139 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
106 _MakeLink('ref_test.html#type-type2', 'type2')), | 140 _MakeLink('ref_test.html#type-type2', 'type2')), |
107 _GetType(dict_, 'type3')['description']) | 141 _GetType(dict_, 'type3')['description']) |
108 | 142 |
109 def testRemoveNoDocs(self): | 143 def testRemoveNoDocs(self): |
110 d = self._LoadJSON('nodoc_test.json') | 144 d = self._LoadJSON('nodoc_test.json') |
111 _RemoveNoDocs(d) | 145 _RemoveNoDocs(d) |
112 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 146 self.assertEquals(self._LoadJSON('expected_nodoc.json'), d) |
| 147 |
| 148 def testGetIntroList(self): |
| 149 model = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 150 self._CreateRefResolver('test_file_data_source.json'), |
| 151 False, |
| 152 FakeAvailabilityFinder(), |
| 153 self._json_cache, |
| 154 FakeTemplateDataSource()) |
| 155 expected_list = [ |
| 156 { 'title': 'Description', |
| 157 'content': [ |
| 158 { 'text': 'a test api' } |
| 159 ] |
| 160 }, |
| 161 { 'title': 'Availability', |
| 162 'content': [ |
| 163 { 'partial': 'handlebar intro_tables/trunk_message.html', |
| 164 'version': 'trunk' |
| 165 } |
| 166 ] |
| 167 }, |
| 168 { 'title': 'Permissions', |
| 169 'content': [ |
| 170 { 'perm': 'tester', |
| 171 'text': '"thing1", "thing2"' |
| 172 }, |
| 173 { 'text': 'is an API for testing things.' } |
| 174 ] |
| 175 }, |
| 176 { 'title': 'Learn More', |
| 177 'content': [ |
| 178 { 'link': 'https://tester.test.com/welcome.html', |
| 179 'text': 'Welcome!' |
| 180 } |
| 181 ] |
| 182 } |
| 183 ] |
| 184 self.assertEquals(json.dumps(model._GetIntroTableList()), |
| 185 json.dumps(expected_list)) |
113 | 186 |
114 def testInlineDocs(self): | 187 def testInlineDocs(self): |
115 schema = { | 188 schema = { |
116 "namespace": "storage", | 189 "namespace": "storage", |
117 "properties": { | 190 "properties": { |
118 "key2": { | 191 "key2": { |
119 "description": "second key", | 192 "description": "second key", |
120 "$ref": "Key" | 193 "$ref": "Key" |
121 }, | 194 }, |
122 "key1": { | 195 "key1": { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 "marker": True, | 237 "marker": True, |
165 "type": "string" | 238 "type": "string" |
166 }, | 239 }, |
167 "type": "array", | 240 "type": "array", |
168 "id": "KeyList", | 241 "id": "KeyList", |
169 "description": "A list of keys" | 242 "description": "A list of keys" |
170 } | 243 } |
171 ] | 244 ] |
172 } | 245 } |
173 | 246 |
174 inlined_schema = copy.deepcopy(schema) | 247 inlined_schema = deepcopy(schema) |
175 _InlineDocs(inlined_schema) | 248 _InlineDocs(inlined_schema) |
176 self.assertEqual(expected_schema, inlined_schema) | 249 self.assertEqual(expected_schema, inlined_schema) |
177 | 250 |
178 def testDetectInline(self): | 251 def testDetectInline(self): |
179 schema = { | 252 schema = { |
180 "types": [ | 253 "types": [ |
181 { | 254 { |
182 "id": "Key", | 255 "id": "Key", |
183 "items": { | 256 "items": { |
184 "$ref": "Value" | 257 "$ref": "Value" |
(...skipping 16 matching lines...) Expand all Loading... |
201 } | 274 } |
202 ] | 275 ] |
203 } | 276 } |
204 | 277 |
205 _DetectInlineableTypes(schema) | 278 _DetectInlineableTypes(schema) |
206 _InlineDocs(schema) | 279 _InlineDocs(schema) |
207 self.assertEqual(expected_schema, schema) | 280 self.assertEqual(expected_schema, schema) |
208 | 281 |
209 if __name__ == '__main__': | 282 if __name__ == '__main__': |
210 unittest.main() | 283 unittest.main() |
OLD | NEW |