Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/common/extensions/docs/server2/api_data_source_test.py

Issue 12996003: Dynamically generate a heading for Extension Docs API pages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comments - Patch currently being broken up Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 compiled_file_system import CompiledFileSystem
17 from file_system import FileNotFoundError 18 from file_system import FileNotFoundError
18 from object_store_creator import ObjectStoreCreator 19 from object_store_creator import ObjectStoreCreator
19 from reference_resolver import ReferenceResolver 20 from reference_resolver import ReferenceResolver
21 from test_data.canned_data import CANNED_TEST_FILE_SYSTEM_DATA
22 from test_file_system import TestFileSystem
23 import third_party.json_schema_compiler.json_parse as json_parse
20 24
21 def _MakeLink(href, text): 25 def _MakeLink(href, text):
22 return '<a href="%s">%s</a>' % (href, text) 26 return '<a href="%s">%s</a>' % (href, text)
23 27
24 def _GetType(dict_, name): 28 def _GetType(dict_, name):
25 for type_ in dict_['types']: 29 for type_ in dict_['types']:
26 if type_['name'] == name: 30 if type_['name'] == name:
27 return type_ 31 return type_
28 32
33 class FakeAvailabilityFinder(object):
34 def GetApiAvailability(self, version):
35 return None
36
29 class FakeSamplesDataSource(object): 37 class FakeSamplesDataSource(object):
30 def Create(self, request): 38 def Create(self, request):
31 return {} 39 return {}
32 40
33 class FakeAPIAndListDataSource(object): 41 class FakeAPIAndListDataSource(object):
34 def __init__(self, json_data): 42 def __init__(self, json_data):
35 self._json = json_data 43 self._json = json_data
36 44
37 def Create(self, *args, **kwargs): 45 def Create(self, *args, **kwargs):
38 return self 46 return self
39 47
40 def get(self, key): 48 def get(self, key):
41 if key not in self._json: 49 if key not in self._json:
42 raise FileNotFoundError(key) 50 raise FileNotFoundError(key)
43 return self._json[key] 51 return self._json[key]
44 52
45 def GetAllNames(self): 53 def GetAllNames(self):
46 return self._json.keys() 54 return self._json.keys()
47 55
48 class APIDataSourceTest(unittest.TestCase): 56 class APIDataSourceTest(unittest.TestCase):
49 def setUp(self): 57 def setUp(self):
50 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(CANNED_TEST_FILE_SYSTEM_DATA),
61 ObjectStoreCreator.ForTest())
62 self._json_cache = self._compiled_fs_factory.Create(
63 lambda _, json: json_parse.Parse(json),
64 APIDataSourceTest,
65 'test')
51 66
52 def _ReadLocalFile(self, filename): 67 def _ReadLocalFile(self, filename):
53 with open(os.path.join(self._base_path, filename), 'r') as f: 68 with open(os.path.join(self._base_path, filename), 'r') as f:
54 return f.read() 69 return f.read()
55 70
56 def _CreateRefResolver(self, filename): 71 def _CreateRefResolver(self, filename):
57 data_source = FakeAPIAndListDataSource( 72 data_source = FakeAPIAndListDataSource(
58 self._LoadJSON(filename)) 73 self._LoadJSON(filename))
59 return ReferenceResolver.Factory(data_source, 74 return ReferenceResolver.Factory(data_source,
60 data_source, 75 data_source,
61 ObjectStoreCreator.ForTest()).Create() 76 ObjectStoreCreator.ForTest()).Create()
62 77
63 def _LoadJSON(self, filename): 78 def _LoadJSON(self, filename):
64 return json.loads(self._ReadLocalFile(filename)) 79 return json.loads(self._ReadLocalFile(filename))
65 80
66 def testCreateId(self): 81 def testCreateId(self):
67 data_source = FakeAPIAndListDataSource( 82 data_source = FakeAPIAndListDataSource(
68 self._LoadJSON('test_file_data_source.json')) 83 self._LoadJSON('test_file_data_source.json'))
69 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], 84 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0],
70 self._CreateRefResolver('test_file_data_source.json'), 85 self._CreateRefResolver('test_file_data_source.json'),
71 False).ToDict() 86 False,
87 FakeAvailabilityFinder(),
88 self._json_cache).ToDict()
72 self.assertEquals('type-TypeA', dict_['types'][0]['id']) 89 self.assertEquals('type-TypeA', dict_['types'][0]['id'])
73 self.assertEquals('property-TypeA-b', 90 self.assertEquals('property-TypeA-b',
74 dict_['types'][0]['properties'][0]['id']) 91 dict_['types'][0]['properties'][0]['id'])
75 self.assertEquals('method-get', dict_['functions'][0]['id']) 92 self.assertEquals('method-get', dict_['functions'][0]['id'])
76 self.assertEquals('event-EventA', dict_['events'][0]['id']) 93 self.assertEquals('event-EventA', dict_['events'][0]['id'])
77 94
78 # TODO(kalman): re-enable this when we have a rebase option. 95 # TODO(kalman): re-enable this when we have a rebase option.
79 def DISABLED_testToDict(self): 96 def DISABLED_testToDict(self):
80 filename = 'test_file.json' 97 filename = 'test_file.json'
81 expected_json = self._LoadJSON('expected_' + filename) 98 expected_json = self._LoadJSON('expected_' + filename)
82 data_source = FakeAPIAndListDataSource( 99 data_source = FakeAPIAndListDataSource(
83 self._LoadJSON('test_file_data_source.json')) 100 self._LoadJSON('test_file_data_source.json'))
84 dict_ = _JSCModel(self._LoadJSON(filename)[0], 101 dict_ = _JSCModel(self._LoadJSON(filename)[0],
85 self._CreateRefResolver('test_file_data_source.json'), 102 self._CreateRefResolver('test_file_data_source.json'),
86 False).ToDict() 103 False,
104 FakeAvailabilityFinder(),
105 self._json_cache).ToDict()
87 self.assertEquals(expected_json, dict_) 106 self.assertEquals(expected_json, dict_)
88 107
89 def testFormatValue(self): 108 def testFormatValue(self):
90 self.assertEquals('1,234,567', _FormatValue(1234567)) 109 self.assertEquals('1,234,567', _FormatValue(1234567))
91 self.assertEquals('67', _FormatValue(67)) 110 self.assertEquals('67', _FormatValue(67))
92 self.assertEquals('234,567', _FormatValue(234567)) 111 self.assertEquals('234,567', _FormatValue(234567))
93 112
94 def testFormatDescription(self): 113 def testFormatDescription(self):
95 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], 114 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0],
96 self._CreateRefResolver('ref_test_data_source.json'), 115 self._CreateRefResolver('ref_test_data_source.json'),
97 False).ToDict() 116 False,
117 FakeAvailabilityFinder(),
118 self._json_cache).ToDict()
98 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), 119 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'),
99 _GetType(dict_, 'type1')['description']) 120 _GetType(dict_, 'type1')['description'])
100 self.assertEquals( 121 self.assertEquals(
101 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), 122 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'),
102 _MakeLink('ref_test.html#type-type2', 'type2')), 123 _MakeLink('ref_test.html#type-type2', 'type2')),
103 _GetType(dict_, 'type2')['description']) 124 _GetType(dict_, 'type2')['description'])
104 self.assertEquals( 125 self.assertEquals(
105 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), 126 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'),
106 _MakeLink('ref_test.html#type-type2', 'type2')), 127 _MakeLink('ref_test.html#type-type2', 'type2')),
107 _GetType(dict_, 'type3')['description']) 128 _GetType(dict_, 'type3')['description'])
108 129
109 def testRemoveNoDocs(self): 130 def testRemoveNoDocs(self):
110 d = self._LoadJSON('nodoc_test.json') 131 d = self._LoadJSON('nodoc_test.json')
111 _RemoveNoDocs(d) 132 _RemoveNoDocs(d)
112 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) 133 self.assertEquals(self._LoadJSON('expected_nodoc.json'), d)
134
135 def testGetPermissions(self):
136 model = _JSCModel(self._LoadJSON('test_file.json')[0],
137 self._CreateRefResolver('test_file_data_source.json'),
138 False,
139 FakeAvailabilityFinder(),
140 self._json_cache)
141 self.assertEquals(model._GetPermissions(),
142 [
143 { "permission": "tester" },
144 { "extra": "is an API." }
145 ])
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 [{
155 'href': 'https://tester.test.com/welcome.html',
156 'content': 'Welcome!'
157 }])
113 158
114 def testInlineDocs(self): 159 def testInlineDocs(self):
115 schema = { 160 schema = {
116 "namespace": "storage", 161 "namespace": "storage",
117 "properties": { 162 "properties": {
118 "key2": { 163 "key2": {
119 "description": "second key", 164 "description": "second key",
120 "$ref": "Key" 165 "$ref": "Key"
121 }, 166 },
122 "key1": { 167 "key1": {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 "marker": True, 209 "marker": True,
165 "type": "string" 210 "type": "string"
166 }, 211 },
167 "type": "array", 212 "type": "array",
168 "id": "KeyList", 213 "id": "KeyList",
169 "description": "A list of keys" 214 "description": "A list of keys"
170 } 215 }
171 ] 216 ]
172 } 217 }
173 218
174 inlined_schema = copy.deepcopy(schema) 219 inlined_schema = deepcopy(schema)
175 _InlineDocs(inlined_schema) 220 _InlineDocs(inlined_schema)
176 self.assertEqual(expected_schema, inlined_schema) 221 self.assertEqual(expected_schema, inlined_schema)
177 222
178 def testDetectInline(self): 223 def testDetectInline(self):
179 schema = { 224 schema = {
180 "types": [ 225 "types": [
181 { 226 {
182 "id": "Key", 227 "id": "Key",
183 "items": { 228 "items": {
184 "$ref": "Value" 229 "$ref": "Value"
(...skipping 16 matching lines...) Expand all
201 } 246 }
202 ] 247 ]
203 } 248 }
204 249
205 _DetectInlineableTypes(schema) 250 _DetectInlineableTypes(schema)
206 _InlineDocs(schema) 251 _InlineDocs(schema)
207 self.assertEqual(expected_schema, schema) 252 self.assertEqual(expected_schema, schema)
208 253
209 if __name__ == '__main__': 254 if __name__ == '__main__':
210 unittest.main() 255 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698