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

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

Issue 228723005: Refactor APIDataSource to work with DataSourceRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tiny cleanup Created 6 years, 8 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 json 6 import json
7 import os 7 import os
8 import sys 8 import sys
9 import unittest 9 import unittest
10 10
(...skipping 24 matching lines...) Expand all
35 if type_['name'] == name: 35 if type_['name'] == name:
36 return type_ 36 return type_
37 37
38 38
39 class _FakeAvailabilityFinder(object): 39 class _FakeAvailabilityFinder(object):
40 40
41 def GetApiAvailability(self, version): 41 def GetApiAvailability(self, version):
42 return ChannelInfo('stable', '396', 5) 42 return ChannelInfo('stable', '396', 5)
43 43
44 44
45 class _FakeSamplesDataSource(object):
46
47 def Create(self, request):
48 return {}
49
50
51 # Sad irony :(
52 class _FakeAPIDataSource(object):
53
54 def __init__(self, json_data):
55 self._json = json_data
56
57 def Create(self, *args, **kwargs):
58 return self
59
60 def get(self, key, disable_refs=False):
61 if key not in self._json:
62 raise FileNotFoundError(key)
63 return self._json[key]
64
65
66 class _FakeNamespace(object):
67
68 def __init__(self):
69 self.documentation_options = {}
70
71
72 class _FakeAPIModels(object):
73
74 def __init__(self, names):
75 self._names = names
76
77 def GetNames(self):
78 return self._names
79
80 def GetModel(self, name):
81 return Future(value=_FakeNamespace())
82
83
84 class _FakeTemplateCache(object): 45 class _FakeTemplateCache(object):
85 46
86 def GetFromFile(self, key): 47 def GetFromFile(self, key):
87 return Future(value='handlebar %s' % key) 48 return Future(value='handlebar %s' % key)
88 49
89 50
90 class APIDataSourceTest(unittest.TestCase): 51 class APIDataSourceTest(unittest.TestCase):
91 52
92 def setUp(self): 53 def setUp(self):
93 self._base_path = Server2Path('test_data', 'test_json') 54 self._base_path = Server2Path('test_data', 'test_json')
(...skipping 19 matching lines...) Expand all
113 def _ReadLocalFile(self, filename): 74 def _ReadLocalFile(self, filename):
114 with open(os.path.join(self._base_path, filename), 'r') as f: 75 with open(os.path.join(self._base_path, filename), 'r') as f:
115 return f.read() 76 return f.read()
116 77
117 def _LoadJSON(self, filename): 78 def _LoadJSON(self, filename):
118 return json.loads(self._ReadLocalFile(filename)) 79 return json.loads(self._ReadLocalFile(filename))
119 80
120 def testCreateId(self): 81 def testCreateId(self):
121 dict_ = _JSCModel('tester', 82 dict_ = _JSCModel('tester',
122 self._api_models, 83 self._api_models,
123 False,
124 _FakeAvailabilityFinder(), 84 _FakeAvailabilityFinder(),
125 self._json_cache, 85 self._json_cache,
126 _FakeTemplateCache(), 86 _FakeTemplateCache(),
127 self._features_bundle, 87 self._features_bundle,
128 None).ToDict() 88 None).ToDict()
129 self.assertEquals('type-TypeA', dict_['types'][0]['id']) 89 self.assertEquals('type-TypeA', dict_['types'][0]['id'])
130 self.assertEquals('property-TypeA-b', 90 self.assertEquals('property-TypeA-b',
131 dict_['types'][0]['properties'][0]['id']) 91 dict_['types'][0]['properties'][0]['id'])
132 self.assertEquals('method-get', dict_['functions'][0]['id']) 92 self.assertEquals('method-get', dict_['functions'][0]['id'])
133 self.assertEquals('event-EventA', dict_['events'][0]['id']) 93 self.assertEquals('event-EventA', dict_['events'][0]['id'])
134 94
135 # TODO(kalman): re-enable this when we have a rebase option. 95 # TODO(kalman): re-enable this when we have a rebase option.
136 def DISABLED_testToDict(self): 96 def DISABLED_testToDict(self):
137 expected_json = self._LoadJSON('expected_tester.json') 97 expected_json = self._LoadJSON('expected_tester.json')
138 dict_ = _JSCModel('tester', 98 dict_ = _JSCModel('tester',
139 self._api_models, 99 self._api_models,
140 False,
141 _FakeAvailabilityFinder(), 100 _FakeAvailabilityFinder(),
142 self._json_cache, 101 self._json_cache,
143 _FakeTemplateCache(), 102 _FakeTemplateCache(),
144 self._features_bundle, 103 self._features_bundle,
145 None).ToDict() 104 None).ToDict()
146 self.assertEquals(expected_json, dict_) 105 self.assertEquals(expected_json, dict_)
147 106
148 def testFormatValue(self): 107 def testFormatValue(self):
149 self.assertEquals('1,234,567', _FormatValue(1234567)) 108 self.assertEquals('1,234,567', _FormatValue(1234567))
150 self.assertEquals('67', _FormatValue(67)) 109 self.assertEquals('67', _FormatValue(67))
151 self.assertEquals('234,567', _FormatValue(234567)) 110 self.assertEquals('234,567', _FormatValue(234567))
152 111
153 def testGetApiAvailability(self): 112 def testGetApiAvailability(self):
154 api_availabilities = { 113 api_availabilities = {
155 'bluetooth': ChannelInfo('dev', CANNED_BRANCHES[28], 28), 114 'bluetooth': ChannelInfo('dev', CANNED_BRANCHES[28], 28),
156 'contextMenus': ChannelInfo('trunk', CANNED_BRANCHES['trunk'], 'trunk'), 115 'contextMenus': ChannelInfo('trunk', CANNED_BRANCHES['trunk'], 'trunk'),
157 'jsonStableAPI': ChannelInfo('stable', CANNED_BRANCHES[20], 20), 116 'jsonStableAPI': ChannelInfo('stable', CANNED_BRANCHES[20], 20),
158 'idle': ChannelInfo('stable', CANNED_BRANCHES[5], 5), 117 'idle': ChannelInfo('stable', CANNED_BRANCHES[5], 5),
159 'input.ime': ChannelInfo('stable', CANNED_BRANCHES[18], 18), 118 'input.ime': ChannelInfo('stable', CANNED_BRANCHES[18], 18),
160 'tabs': ChannelInfo('stable', CANNED_BRANCHES[18], 18) 119 'tabs': ChannelInfo('stable', CANNED_BRANCHES[18], 18)
161 } 120 }
162 for api_name, availability in api_availabilities.iteritems(): 121 for api_name, availability in api_availabilities.iteritems():
163 model = _JSCModel(api_name, 122 model = _JSCModel(api_name,
164 self._avail_api_models, 123 self._avail_api_models,
165 True,
166 self._avail_finder, 124 self._avail_finder,
167 self._avail_json_cache, 125 self._avail_json_cache,
168 _FakeTemplateCache(), 126 _FakeTemplateCache(),
169 self._features_bundle, 127 self._features_bundle,
170 None) 128 None)
171 self.assertEquals(availability, model._GetApiAvailability()) 129 self.assertEquals(availability, model._GetApiAvailability())
172 130
173 def testGetIntroList(self): 131 def testGetIntroList(self):
174 model = _JSCModel('tester', 132 model = _JSCModel('tester',
175 self._api_models, 133 self._api_models,
176 False,
177 _FakeAvailabilityFinder(), 134 _FakeAvailabilityFinder(),
178 self._json_cache, 135 self._json_cache,
179 _FakeTemplateCache(), 136 _FakeTemplateCache(),
180 self._features_bundle, 137 self._features_bundle,
181 None) 138 None)
182 expected_list = [ 139 expected_list = [
183 { 'title': 'Description', 140 { 'title': 'Description',
184 'content': [ 141 'content': [
185 { 'text': 'a test api' } 142 { 'text': 'a test api' }
186 ] 143 ]
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 # Duplicates are an error. 195 # Duplicates are an error.
239 self.assertRaises(AssertionError, _GetEventByNameFromEvents, events) 196 self.assertRaises(AssertionError, _GetEventByNameFromEvents, events)
240 197
241 def _FakeLoadAddRulesSchema(self): 198 def _FakeLoadAddRulesSchema(self):
242 events = self._LoadJSON('add_rules_def_test.json') 199 events = self._LoadJSON('add_rules_def_test.json')
243 return _GetEventByNameFromEvents(events) 200 return _GetEventByNameFromEvents(events)
244 201
245 def testAddRules(self): 202 def testAddRules(self):
246 dict_ = _JSCModel('add_rules_tester', 203 dict_ = _JSCModel('add_rules_tester',
247 self._api_models, 204 self._api_models,
248 False,
249 _FakeAvailabilityFinder(), 205 _FakeAvailabilityFinder(),
250 self._json_cache, 206 self._json_cache,
251 _FakeTemplateCache(), 207 _FakeTemplateCache(),
252 self._features_bundle, 208 self._features_bundle,
253 self._FakeLoadAddRulesSchema).ToDict() 209 self._FakeLoadAddRulesSchema).ToDict()
254 210
255 # Check that the first event has the addRulesFunction defined. 211 # Check that the first event has the addRulesFunction defined.
256 self.assertEquals('add_rules_tester', dict_['name']) 212 self.assertEquals('add_rules_tester', dict_['name'])
257 self.assertEquals('rules', dict_['events'][0]['name']) 213 self.assertEquals('rules', dict_['events'][0]['name'])
258 self.assertEquals('notable_name_to_check_for', 214 self.assertEquals('notable_name_to_check_for',
259 dict_['events'][0]['byName']['addRules'][ 215 dict_['events'][0]['byName']['addRules'][
260 'parameters'][0]['name']) 216 'parameters'][0]['name'])
261 217
262 # Check that the second event has addListener defined. 218 # Check that the second event has addListener defined.
263 self.assertEquals('noRules', dict_['events'][1]['name']) 219 self.assertEquals('noRules', dict_['events'][1]['name'])
264 self.assertEquals('add_rules_tester', dict_['name']) 220 self.assertEquals('add_rules_tester', dict_['name'])
265 self.assertEquals('noRules', dict_['events'][1]['name']) 221 self.assertEquals('noRules', dict_['events'][1]['name'])
266 self.assertEquals('callback', 222 self.assertEquals('callback',
267 dict_['events'][0]['byName']['addListener'][ 223 dict_['events'][0]['byName']['addListener'][
268 'parameters'][0]['name']) 224 'parameters'][0]['name'])
269 225
270 if __name__ == '__main__': 226 if __name__ == '__main__':
271 unittest.main() 227 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698