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 | |
24 _INTRO_TABLES_DATA = { | |
25 'docs': { | |
26 'templates': { | |
27 'json': { | |
28 'intro_tables.json': | |
29 '{"tester": [{"Permissions": "<code>tester</code>"}]}' | |
not at google - send to devlin
2013/04/30 18:34:19
what about Learn More? No Learn More? No Permissio
epeterson
2013/05/13 02:38:10
Done.
| |
30 } | |
31 } | |
32 } | |
33 } | |
22 | 34 |
23 def _MakeLink(href, text): | 35 def _MakeLink(href, text): |
24 return '<a href="%s">%s</a>' % (href, text) | 36 return '<a href="%s">%s</a>' % (href, text) |
25 | 37 |
26 def _GetType(dict_, name): | 38 def _GetType(dict_, name): |
27 for type_ in dict_['types']: | 39 for type_ in dict_['types']: |
28 if type_['name'] == name: | 40 if type_['name'] == name: |
29 return type_ | 41 return type_ |
30 | 42 |
43 class FakeAvailabilityDataSourceFactory(object): | |
44 def Create(self): | |
45 return {} | |
46 | |
47 def _FakeGetAvailability(self): | |
48 return 'Not Currently Available' | |
49 | |
31 class FakeSamplesDataSource(object): | 50 class FakeSamplesDataSource(object): |
32 def Create(self, request): | 51 def Create(self, request): |
33 return {} | 52 return {} |
34 | 53 |
35 class FakeAPIAndListDataSource(object): | 54 class FakeAPIAndListDataSource(object): |
36 def __init__(self, json_data): | 55 def __init__(self, json_data): |
37 self._json = json_data | 56 self._json = json_data |
38 | 57 |
39 def Create(self, *args, **kwargs): | 58 def Create(self, *args, **kwargs): |
40 return self | 59 return self |
41 | 60 |
42 def get(self, key): | 61 def get(self, key): |
43 if key not in self._json: | 62 if key not in self._json: |
44 raise FileNotFoundError(key) | 63 raise FileNotFoundError(key) |
45 return self._json[key] | 64 return self._json[key] |
46 | 65 |
47 def GetAllNames(self): | 66 def GetAllNames(self): |
48 return self._json.keys() | 67 return self._json.keys() |
49 | 68 |
50 class APIDataSourceTest(unittest.TestCase): | 69 class APIDataSourceTest(unittest.TestCase): |
51 def setUp(self): | 70 def setUp(self): |
52 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 71 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
72 _JSCModel._GetAvailability = _FakeGetAvailability | |
not at google - send to devlin
2013/04/30 18:34:19
this is what passing in a fake availability data s
epeterson
2013/05/13 02:38:10
Done.
| |
53 | 73 |
54 def _ReadLocalFile(self, filename): | 74 def _ReadLocalFile(self, filename): |
55 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: |
56 return f.read() | 76 return f.read() |
57 | 77 |
58 def _CreateRefResolver(self, filename): | 78 def _CreateRefResolver(self, filename): |
59 data_source = FakeAPIAndListDataSource( | 79 data_source = FakeAPIAndListDataSource( |
60 self._LoadJSON(filename)) | 80 self._LoadJSON(filename)) |
61 return ReferenceResolver.Factory(data_source, | 81 return ReferenceResolver.Factory(data_source, |
62 data_source, | 82 data_source, |
63 ObjectStoreCreator.TestFactory()).Create() | 83 ObjectStoreCreator.TestFactory()).Create() |
64 | 84 |
65 def _LoadJSON(self, filename): | 85 def _LoadJSON(self, filename): |
66 return json.loads(self._ReadLocalFile(filename)) | 86 return json.loads(self._ReadLocalFile(filename)) |
67 | 87 |
68 def testCreateId(self): | 88 def testCreateId(self): |
69 data_source = FakeAPIAndListDataSource( | 89 data_source = FakeAPIAndListDataSource( |
70 self._LoadJSON('test_file_data_source.json')) | 90 self._LoadJSON('test_file_data_source.json')) |
91 fake_avail_factory = FakeAvailabilityDataSourceFactory() | |
not at google - send to devlin
2013/04/30 18:34:19
inline
epeterson
2013/05/13 02:38:10
Done.
| |
71 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 92 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
72 self._CreateRefResolver('test_file_data_source.json'), | 93 self._CreateRefResolver('test_file_data_source.json'), |
73 False).ToDict() | 94 False, |
95 fake_avail_factory, | |
96 TestFileSystem(deepcopy(_INTRO_TABLES_DATA))).ToDict() | |
not at google - send to devlin
2013/04/30 18:34:19
save a reference in setUp?
epeterson
2013/05/13 02:38:10
Done.
| |
74 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 97 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
75 self.assertEquals('property-TypeA-b', | 98 self.assertEquals('property-TypeA-b', |
76 dict_['types'][0]['properties'][0]['id']) | 99 dict_['types'][0]['properties'][0]['id']) |
77 self.assertEquals('method-get', dict_['functions'][0]['id']) | 100 self.assertEquals('method-get', dict_['functions'][0]['id']) |
78 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 101 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
79 | 102 |
80 # TODO(kalman): re-enable this when we have a rebase option. | 103 # TODO(kalman): re-enable this when we have a rebase option. |
81 def DISABLED_testToDict(self): | 104 def DISABLED_testToDict(self): |
82 filename = 'test_file.json' | 105 filename = 'test_file.json' |
83 expected_json = self._LoadJSON('expected_' + filename) | 106 expected_json = self._LoadJSON('expected_' + filename) |
84 data_source = FakeAPIAndListDataSource( | 107 data_source = FakeAPIAndListDataSource( |
85 self._LoadJSON('test_file_data_source.json')) | 108 self._LoadJSON('test_file_data_source.json')) |
86 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 109 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
87 self._CreateRefResolver('test_file_data_source.json'), | 110 self._CreateRefResolver('test_file_data_source.json'), |
88 False).ToDict() | 111 False, |
112 fake_avail_factory, | |
not at google - send to devlin
2013/04/30 18:34:19
the only reason this works is because the test is
epeterson
2013/05/13 02:38:10
Done.
| |
113 TestFileSystem(deepcopy(_INTRO_TABLES_DATA))).ToDict() | |
89 self.assertEquals(expected_json, dict_) | 114 self.assertEquals(expected_json, dict_) |
90 | 115 |
91 def testFormatValue(self): | 116 def testFormatValue(self): |
92 self.assertEquals('1,234,567', _FormatValue(1234567)) | 117 self.assertEquals('1,234,567', _FormatValue(1234567)) |
93 self.assertEquals('67', _FormatValue(67)) | 118 self.assertEquals('67', _FormatValue(67)) |
94 self.assertEquals('234,567', _FormatValue(234567)) | 119 self.assertEquals('234,567', _FormatValue(234567)) |
95 | 120 |
96 def testFormatDescription(self): | 121 def testFormatDescription(self): |
122 fake_avail_factory = FakeAvailabilityDataSourceFactory() | |
97 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 123 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
98 self._CreateRefResolver('ref_test_data_source.json'), | 124 self._CreateRefResolver('ref_test_data_source.json'), |
99 False).ToDict() | 125 False, |
126 fake_avail_factory, | |
127 TestFileSystem(deepcopy(_INTRO_TABLES_DATA))).ToDict() | |
not at google - send to devlin
2013/04/30 18:34:19
ditto inline, safe reference comment
epeterson
2013/05/13 02:38:10
Done.
| |
100 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 128 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
101 _GetType(dict_, 'type1')['description']) | 129 _GetType(dict_, 'type1')['description']) |
102 self.assertEquals( | 130 self.assertEquals( |
103 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 131 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
104 _MakeLink('ref_test.html#type-type2', 'type2')), | 132 _MakeLink('ref_test.html#type-type2', 'type2')), |
105 _GetType(dict_, 'type2')['description']) | 133 _GetType(dict_, 'type2')['description']) |
106 self.assertEquals( | 134 self.assertEquals( |
107 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 135 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
108 _MakeLink('ref_test.html#type-type2', 'type2')), | 136 _MakeLink('ref_test.html#type-type2', 'type2')), |
109 _GetType(dict_, 'type3')['description']) | 137 _GetType(dict_, 'type3')['description']) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 "marker": True, | 194 "marker": True, |
167 "type": "string" | 195 "type": "string" |
168 }, | 196 }, |
169 "type": "array", | 197 "type": "array", |
170 "id": "KeyList", | 198 "id": "KeyList", |
171 "description": "A list of keys" | 199 "description": "A list of keys" |
172 } | 200 } |
173 ] | 201 ] |
174 } | 202 } |
175 | 203 |
176 inlined_schema = copy.deepcopy(schema) | 204 inlined_schema = deepcopy(schema) |
177 _InlineDocs(inlined_schema) | 205 _InlineDocs(inlined_schema) |
178 self.assertEqual(expected_schema, inlined_schema) | 206 self.assertEqual(expected_schema, inlined_schema) |
179 | 207 |
180 | 208 |
181 if __name__ == '__main__': | 209 if __name__ == '__main__': |
182 unittest.main() | 210 unittest.main() |
OLD | NEW |