| 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 from copy import deepcopy | 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 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 def _MakeLink(href, text): | 28 def _MakeLink(href, text): |
| 29 return '<a href="%s">%s</a>' % (href, text) | 29 return '<a href="%s">%s</a>' % (href, text) |
| 30 | 30 |
| 31 | 31 |
| 32 def _GetType(dict_, name): | 32 def _GetType(dict_, name): |
| 33 for type_ in dict_['types']: | 33 for type_ in dict_['types']: |
| 34 if type_['name'] == name: | 34 if type_['name'] == name: |
| 35 return type_ | 35 return type_ |
| 36 | 36 |
| 37 | 37 |
| 38 class FakeAvailabilityFinder(object): | 38 class _FakeAvailabilityFinder(object): |
| 39 | 39 |
| 40 def GetApiAvailability(self, version): | 40 def GetApiAvailability(self, version): |
| 41 return ChannelInfo('stable', '396', 5) | 41 return ChannelInfo('stable', '396', 5) |
| 42 | 42 |
| 43 | 43 |
| 44 class FakeSamplesDataSource(object): | 44 class _FakeSamplesDataSource(object): |
| 45 | 45 |
| 46 def Create(self, request): | 46 def Create(self, request): |
| 47 return {} | 47 return {} |
| 48 | 48 |
| 49 | 49 |
| 50 class FakeAPIAndListDataSource(object): | 50 # Sad irony :( |
| 51 class _FakeAPIDataSource(object): |
| 51 | 52 |
| 52 def __init__(self, json_data): | 53 def __init__(self, json_data): |
| 53 self._json = json_data | 54 self._json = json_data |
| 54 | 55 |
| 55 def Create(self, *args, **kwargs): | 56 def Create(self, *args, **kwargs): |
| 56 return self | 57 return self |
| 57 | 58 |
| 58 def get(self, key, disable_refs=False): | 59 def get(self, key, disable_refs=False): |
| 59 if key not in self._json: | 60 if key not in self._json: |
| 60 raise FileNotFoundError(key) | 61 raise FileNotFoundError(key) |
| 61 return self._json[key] | 62 return self._json[key] |
| 62 | 63 |
| 63 def GetAllNames(self): | 64 |
| 64 return self._json.keys() | 65 class _FakeAPIModels(object): |
| 66 |
| 67 def __init__(self, names): |
| 68 self._names = names |
| 69 |
| 70 def GetNames(self): |
| 71 return self._names |
| 65 | 72 |
| 66 | 73 |
| 67 class FakeTemplateCache(object): | 74 class _FakeTemplateCache(object): |
| 68 | 75 |
| 69 def GetFromFile(self, key): | 76 def GetFromFile(self, key): |
| 70 return Future(value='handlebar %s' % key) | 77 return Future(value='handlebar %s' % key) |
| 71 | 78 |
| 72 | 79 |
| 73 class APIDataSourceTest(unittest.TestCase): | 80 class APIDataSourceTest(unittest.TestCase): |
| 74 | 81 |
| 75 def setUp(self): | 82 def setUp(self): |
| 76 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 83 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 77 self._compiled_fs_factory = CompiledFileSystem.Factory( | 84 self._compiled_fs_factory = CompiledFileSystem.Factory( |
| 78 ObjectStoreCreator.ForTest()) | 85 ObjectStoreCreator.ForTest()) |
| 79 self._json_cache = self._compiled_fs_factory.ForJson( | 86 self._json_cache = self._compiled_fs_factory.ForJson( |
| 80 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)) | 87 TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)) |
| 81 | 88 |
| 82 def _ReadLocalFile(self, filename): | 89 def _ReadLocalFile(self, filename): |
| 83 with open(os.path.join(self._base_path, filename), 'r') as f: | 90 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 84 return f.read() | 91 return f.read() |
| 85 | 92 |
| 86 def _CreateRefResolver(self, filename): | 93 def _CreateRefResolver(self, filename): |
| 87 data_source = FakeAPIAndListDataSource( | 94 test_data = self._LoadJSON(filename) |
| 88 self._LoadJSON(filename)) | 95 return ReferenceResolver.Factory(_FakeAPIDataSource(test_data), |
| 89 return ReferenceResolver.Factory(data_source, | 96 _FakeAPIModels(test_data), |
| 90 data_source, | |
| 91 ObjectStoreCreator.ForTest()).Create() | 97 ObjectStoreCreator.ForTest()).Create() |
| 92 | 98 |
| 93 def _LoadJSON(self, filename): | 99 def _LoadJSON(self, filename): |
| 94 return json.loads(self._ReadLocalFile(filename)) | 100 return json.loads(self._ReadLocalFile(filename)) |
| 95 | 101 |
| 96 def testCreateId(self): | 102 def testCreateId(self): |
| 97 data_source = FakeAPIAndListDataSource( | |
| 98 self._LoadJSON('test_file_data_source.json')) | |
| 99 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 103 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 100 self._CreateRefResolver('test_file_data_source.json'), | 104 self._CreateRefResolver('test_file_data_source.json'), |
| 101 False, | 105 False, |
| 102 FakeAvailabilityFinder(), | 106 _FakeAvailabilityFinder(), |
| 103 TestBranchUtility.CreateWithCannedData(), | 107 TestBranchUtility.CreateWithCannedData(), |
| 104 self._json_cache, | 108 self._json_cache, |
| 105 FakeTemplateCache(), | 109 _FakeTemplateCache(), |
| 106 None).ToDict() | 110 None).ToDict() |
| 107 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 111 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
| 108 self.assertEquals('property-TypeA-b', | 112 self.assertEquals('property-TypeA-b', |
| 109 dict_['types'][0]['properties'][0]['id']) | 113 dict_['types'][0]['properties'][0]['id']) |
| 110 self.assertEquals('method-get', dict_['functions'][0]['id']) | 114 self.assertEquals('method-get', dict_['functions'][0]['id']) |
| 111 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 115 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
| 112 | 116 |
| 113 # TODO(kalman): re-enable this when we have a rebase option. | 117 # TODO(kalman): re-enable this when we have a rebase option. |
| 114 def DISABLED_testToDict(self): | 118 def DISABLED_testToDict(self): |
| 115 filename = 'test_file.json' | 119 filename = 'test_file.json' |
| 116 expected_json = self._LoadJSON('expected_' + filename) | 120 expected_json = self._LoadJSON('expected_' + filename) |
| 117 data_source = FakeAPIAndListDataSource( | |
| 118 self._LoadJSON('test_file_data_source.json')) | |
| 119 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 121 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
| 120 self._CreateRefResolver('test_file_data_source.json'), | 122 self._CreateRefResolver('test_file_data_source.json'), |
| 121 False, | 123 False, |
| 122 FakeAvailabilityFinder(), | 124 _FakeAvailabilityFinder(), |
| 123 TestBranchUtility.CreateWithCannedData(), | 125 TestBranchUtility.CreateWithCannedData(), |
| 124 self._json_cache, | 126 self._json_cache, |
| 125 FakeTemplateCache(), | 127 _FakeTemplateCache(), |
| 126 None).ToDict() | 128 None).ToDict() |
| 127 self.assertEquals(expected_json, dict_) | 129 self.assertEquals(expected_json, dict_) |
| 128 | 130 |
| 129 def testFormatValue(self): | 131 def testFormatValue(self): |
| 130 self.assertEquals('1,234,567', _FormatValue(1234567)) | 132 self.assertEquals('1,234,567', _FormatValue(1234567)) |
| 131 self.assertEquals('67', _FormatValue(67)) | 133 self.assertEquals('67', _FormatValue(67)) |
| 132 self.assertEquals('234,567', _FormatValue(234567)) | 134 self.assertEquals('234,567', _FormatValue(234567)) |
| 133 | 135 |
| 134 def testFormatDescription(self): | 136 def testFormatDescription(self): |
| 135 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 137 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
| 136 self._CreateRefResolver('ref_test_data_source.json'), | 138 self._CreateRefResolver('ref_test_data_source.json'), |
| 137 False, | 139 False, |
| 138 FakeAvailabilityFinder(), | 140 _FakeAvailabilityFinder(), |
| 139 TestBranchUtility.CreateWithCannedData(), | 141 TestBranchUtility.CreateWithCannedData(), |
| 140 self._json_cache, | 142 self._json_cache, |
| 141 FakeTemplateCache(), | 143 _FakeTemplateCache(), |
| 142 None).ToDict() | 144 None).ToDict() |
| 143 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 145 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
| 144 _GetType(dict_, 'type1')['description']) | 146 _GetType(dict_, 'type1')['description']) |
| 145 self.assertEquals( | 147 self.assertEquals( |
| 146 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 148 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
| 147 _MakeLink('ref_test.html#type-type2', 'type2')), | 149 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 148 _GetType(dict_, 'type2')['description']) | 150 _GetType(dict_, 'type2')['description']) |
| 149 self.assertEquals( | 151 self.assertEquals( |
| 150 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 152 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 151 _MakeLink('ref_test.html#type-type2', 'type2')), | 153 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 152 _GetType(dict_, 'type3')['description']) | 154 _GetType(dict_, 'type3')['description']) |
| 153 | 155 |
| 154 | 156 |
| 155 def testGetApiAvailability(self): | 157 def testGetApiAvailability(self): |
| 156 model = _JSCModel(self._LoadJSON('test_file.json')[0], | 158 model = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 157 self._CreateRefResolver('test_file_data_source.json'), | 159 self._CreateRefResolver('test_file_data_source.json'), |
| 158 False, | 160 False, |
| 159 FakeAvailabilityFinder(), | 161 _FakeAvailabilityFinder(), |
| 160 TestBranchUtility.CreateWithCannedData(), | 162 TestBranchUtility.CreateWithCannedData(), |
| 161 self._json_cache, | 163 self._json_cache, |
| 162 FakeTemplateCache(), | 164 _FakeTemplateCache(), |
| 163 None) | 165 None) |
| 164 # The model namespace is "tester". No predetermined availability is found, | 166 # The model namespace is "tester". No predetermined availability is found, |
| 165 # so the FakeAvailabilityFinder instance is used to find availability. | 167 # so the _FakeAvailabilityFinder instance is used to find availability. |
| 166 self.assertEqual(ChannelInfo('stable', '396', 5), | 168 self.assertEqual(ChannelInfo('stable', '396', 5), |
| 167 model._GetApiAvailability()) | 169 model._GetApiAvailability()) |
| 168 | 170 |
| 169 # These APIs have predetermined availabilities in the | 171 # These APIs have predetermined availabilities in the |
| 170 # api_availabilities.json file within CANNED_DATA. | 172 # api_availabilities.json file within CANNED_DATA. |
| 171 model._namespace.name = 'trunk_api' | 173 model._namespace.name = 'trunk_api' |
| 172 self.assertEqual(ChannelInfo('trunk', 'trunk', 'trunk'), | 174 self.assertEqual(ChannelInfo('trunk', 'trunk', 'trunk'), |
| 173 model._GetApiAvailability()) | 175 model._GetApiAvailability()) |
| 174 | 176 |
| 175 model._namespace.name = 'dev_api' | 177 model._namespace.name = 'dev_api' |
| 176 self.assertEqual(ChannelInfo('dev', '1500', 28), | 178 self.assertEqual(ChannelInfo('dev', '1500', 28), |
| 177 model._GetApiAvailability()) | 179 model._GetApiAvailability()) |
| 178 | 180 |
| 179 model._namespace.name = 'beta_api' | 181 model._namespace.name = 'beta_api' |
| 180 self.assertEqual(ChannelInfo('beta', '1453', 27), | 182 self.assertEqual(ChannelInfo('beta', '1453', 27), |
| 181 model._GetApiAvailability()) | 183 model._GetApiAvailability()) |
| 182 | 184 |
| 183 model._namespace.name = 'stable_api' | 185 model._namespace.name = 'stable_api' |
| 184 self.assertEqual(ChannelInfo('stable', '1132', 20), | 186 self.assertEqual(ChannelInfo('stable', '1132', 20), |
| 185 model._GetApiAvailability()) | 187 model._GetApiAvailability()) |
| 186 | 188 |
| 187 def testGetIntroList(self): | 189 def testGetIntroList(self): |
| 188 model = _JSCModel(self._LoadJSON('test_file.json')[0], | 190 model = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 189 self._CreateRefResolver('test_file_data_source.json'), | 191 self._CreateRefResolver('test_file_data_source.json'), |
| 190 False, | 192 False, |
| 191 FakeAvailabilityFinder(), | 193 _FakeAvailabilityFinder(), |
| 192 TestBranchUtility.CreateWithCannedData(), | 194 TestBranchUtility.CreateWithCannedData(), |
| 193 self._json_cache, | 195 self._json_cache, |
| 194 FakeTemplateCache(), | 196 _FakeTemplateCache(), |
| 195 None) | 197 None) |
| 196 expected_list = [ | 198 expected_list = [ |
| 197 { 'title': 'Description', | 199 { 'title': 'Description', |
| 198 'content': [ | 200 'content': [ |
| 199 { 'text': 'a test api' } | 201 { 'text': 'a test api' } |
| 200 ] | 202 ] |
| 201 }, | 203 }, |
| 202 { 'title': 'Availability', | 204 { 'title': 'Availability', |
| 203 'content': [ | 205 'content': [ |
| 204 { 'partial': 'handlebar docs/templates/private/' + | 206 { 'partial': 'handlebar docs/templates/private/' + |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 252 |
| 251 events['types'][0]['functions'].append(add_rules) | 253 events['types'][0]['functions'].append(add_rules) |
| 252 # Duplicates are an error. | 254 # Duplicates are an error. |
| 253 self.assertRaises(AssertionError, _GetEventByNameFromEvents, events) | 255 self.assertRaises(AssertionError, _GetEventByNameFromEvents, events) |
| 254 | 256 |
| 255 def _FakeLoadAddRulesSchema(self): | 257 def _FakeLoadAddRulesSchema(self): |
| 256 events = self._LoadJSON('add_rules_def_test.json') | 258 events = self._LoadJSON('add_rules_def_test.json') |
| 257 return _GetEventByNameFromEvents(events) | 259 return _GetEventByNameFromEvents(events) |
| 258 | 260 |
| 259 def testAddRules(self): | 261 def testAddRules(self): |
| 260 data_source = FakeAPIAndListDataSource( | |
| 261 self._LoadJSON('test_file_data_source.json')) | |
| 262 dict_ = _JSCModel(self._LoadJSON('add_rules_test.json')[0], | 262 dict_ = _JSCModel(self._LoadJSON('add_rules_test.json')[0], |
| 263 self._CreateRefResolver('test_file_data_source.json'), | 263 self._CreateRefResolver('test_file_data_source.json'), |
| 264 False, | 264 False, |
| 265 FakeAvailabilityFinder(), | 265 _FakeAvailabilityFinder(), |
| 266 TestBranchUtility.CreateWithCannedData(), | 266 TestBranchUtility.CreateWithCannedData(), |
| 267 self._json_cache, | 267 self._json_cache, |
| 268 FakeTemplateCache(), | 268 _FakeTemplateCache(), |
| 269 self._FakeLoadAddRulesSchema).ToDict() | 269 self._FakeLoadAddRulesSchema).ToDict() |
| 270 # Check that the first event has the addRulesFunction defined. | 270 # Check that the first event has the addRulesFunction defined. |
| 271 self.assertEquals('tester', dict_['name']) | 271 self.assertEquals('tester', dict_['name']) |
| 272 self.assertEquals('rules', dict_['events'][0]['name']) | 272 self.assertEquals('rules', dict_['events'][0]['name']) |
| 273 self.assertEquals('notable_name_to_check_for', | 273 self.assertEquals('notable_name_to_check_for', |
| 274 dict_['events'][0]['byName']['addRules'][ | 274 dict_['events'][0]['byName']['addRules'][ |
| 275 'parameters'][0]['name']) | 275 'parameters'][0]['name']) |
| 276 | 276 |
| 277 # Check that the second event has addListener defined. | 277 # Check that the second event has addListener defined. |
| 278 self.assertEquals('noRules', dict_['events'][1]['name']) | 278 self.assertEquals('noRules', dict_['events'][1]['name']) |
| 279 self.assertEquals('tester', dict_['name']) | 279 self.assertEquals('tester', dict_['name']) |
| 280 self.assertEquals('noRules', dict_['events'][1]['name']) | 280 self.assertEquals('noRules', dict_['events'][1]['name']) |
| 281 self.assertEquals('callback', | 281 self.assertEquals('callback', |
| 282 dict_['events'][0]['byName']['addListener'][ | 282 dict_['events'][0]['byName']['addListener'][ |
| 283 'parameters'][0]['name']) | 283 'parameters'][0]['name']) |
| 284 | 284 |
| 285 if __name__ == '__main__': | 285 if __name__ == '__main__': |
| 286 unittest.main() | 286 unittest.main() |
| OLD | NEW |