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

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

Issue 22824042: Docserver: SidenavDataSource refactor, transition to DataSourceRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tests, comments, cleanup Created 7 years, 3 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 2013 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 logging
8 import sys
9 import unittest 8 import unittest
10 9
11 from compiled_file_system import CompiledFileSystem 10 from compiled_file_system import CompiledFileSystem
12 from local_file_system import LocalFileSystem
13 from object_store_creator import ObjectStoreCreator 11 from object_store_creator import ObjectStoreCreator
14 from sidenav_data_source import SidenavDataSource 12 from servlet import Request
13 from sidenav_data_source import (
14 SidenavDataSource, _AddLevels, _AddSelected, _QualifyHrefs)
15 from test_file_system import TestFileSystem
16
17
18 def CaptureLogging(f):
not at google - send to devlin 2013/08/29 16:30:13 this looks useful. could you add it to test_util?
19 '''Call the function |f|, capturing any logging output generated. |f| must
20 take no arguments. Returns a list of LogRecords that were emitted.
21 '''
22 output = []
23 class Capture(object):
24 def filter(self, record):
25 output.append(record)
26
27 cf = Capture()
28 logging.getLogger('').addFilter(cf)
29 f()
30 logging.getLogger('').removeFilter(cf)
31
32 return output
33
34
35 class FakeServerInstance(object):
36 def __init__(self, file_system):
37 self.compiled_host_fs_factory = CompiledFileSystem.Factory(
38 file_system, ObjectStoreCreator.ForTest())
39 self.sidenav_json_base_path = ''
40
15 41
16 class SamplesDataSourceTest(unittest.TestCase): 42 class SamplesDataSourceTest(unittest.TestCase):
17 def setUp(self): 43 def testAddLevels(self):
18 self._json_path = 'docs/server2/test_data/sidenav_data_source' 44 sidenav_json = [{
19 self._compiled_fs_factory = CompiledFileSystem.Factory( 45 'title': 'H2',
20 LocalFileSystem.Create(), 46 'items': [{
21 ObjectStoreCreator.ForTest()) 47 'title': 'H3',
48 'items': [{ 'title': 'X1' }]
49 }]
50 }]
22 51
23 def _CheckLevels(self, items, level=2): 52 expected = [{
24 for item in items: 53 'level': 1,
25 self.assertEqual(level, item['level']) 54 'title': 'H2',
26 if 'items' in item: 55 'items': [{
27 self._CheckLevels(item['items'], level=level + 1) 56 'level': 2,
57 'title': 'H3',
58 'items': [{ 'level': 3, 'title': 'X1' }]
59 }]
60 }]
28 61
29 def _ReadLocalFile(self, filename): 62 _AddLevels(sidenav_json, 1)
30 with open(os.path.join(sys.path[0], 63 self.assertEqual(expected, sidenav_json)
31 'test_data',
32 'sidenav_data_source',
33 filename), 'r') as f:
34 return f.read()
35 64
36 def testLevels(self): 65 def testAddSelected(self):
37 sidenav_data_source = SidenavDataSource.Factory(self._compiled_fs_factory, 66 sidenav_json = [
38 self._json_path).Create('') 67 { 'href': '/AH2.html' },
39 sidenav_json = sidenav_data_source.get('test') 68 {
40 self._CheckLevels(sidenav_json) 69 'href': '/H2.html',
70 'items': [{
71 'href': '/H3.html'
72 }]
73 }
74 ]
41 75
42 def testSelected(self): 76 expected = [
43 sidenav_data_source = SidenavDataSource.Factory( 77 { 'href': '/AH2.html' },
44 self._compiled_fs_factory, 78 {
45 self._json_path).Create('www.b.com') 79 'child_selected': True,
46 sidenav_json = sidenav_data_source.get('test') 80 'href': '/H2.html',
47 # This will be prettier once JSON is loaded with an OrderedDict. 81 'items': [{
48 for item in sidenav_json: 82 'href': '/H3.html',
49 if item['title'] == 'Jim': 83 'selected': True
50 self.assertTrue(item.get('child_selected', False)) 84 }]
51 for next_item in item['items']: 85 }
52 if next_item['title'] == 'B': 86 ]
53 self.assertTrue(next_item.get('selected', False))
54 return
55 # If we didn't return already, we should fail.
56 self.fail()
57 87
58 def testAbsolutePath(self): 88 _AddSelected(sidenav_json, 'H3.html')
59 sidenav_data_source = SidenavDataSource.Factory( 89 self.assertEqual(expected, sidenav_json)
60 self._compiled_fs_factory, 90
61 self._json_path).Create('absolute_path/test.html') 91 def testQualifyHrefs(self):
62 sidenav_json = sidenav_data_source.get('absolute_path') 92 sidenav_json = [
63 self.assertEqual( 93 { 'href': '/qualified/H1.html' },
64 sidenav_json, 94 { 'href': 'https://qualified/X1.html' },
65 json.loads(self._ReadLocalFile('absolute_path_sidenav_expected.json'))) 95 {
96 'href': 'H2.html',
97 'items': [{
98 'href': 'H3.html'
99 }]
100 }
101 ]
102
103 expected = [
104 { 'href': '/qualified/H1.html' },
105 { 'href': 'https://qualified/X1.html' },
106 {
107 'href': '/H2.html',
108 'items': [{
109 'href': '/H3.html'
110 }]
111 }
112 ]
113
114 log_output = CaptureLogging(lambda: _QualifyHrefs(sidenav_json))
115
116 self.assertEqual(expected, sidenav_json)
117 self.assertEqual(2, len(log_output))
118
119 def testSidenavDataSource(self):
120 file_system = TestFileSystem({
121 'apps_sidenav.json': json.dumps([{
122 'title': 'H1',
123 'href': 'H1.html',
124 'items': [{
125 'title': 'H2',
126 'href': '/H2.html'
127 }]
128 }])
129 })
130
131 expected = [{
132 'level': 2,
133 'child_selected': True,
134 'title': 'H1',
135 'href': '/H1.html',
136 'items': [{
137 'level': 3,
138 'selected': True,
139 'title': 'H2',
140 'href': '/H2.html'
141 }]
142 }]
143
144 sidenav_data_source = SidenavDataSource(
145 FakeServerInstance(file_system), Request.ForTest('/H2.html'))
146
147 log_output = CaptureLogging(
148 lambda: self.assertEqual(expected, sidenav_data_source.get('apps')))
149
150 self.assertEqual(1, len(log_output))
151 self.assertTrue(
152 log_output[0].msg.startswith('Paths in sidenav must be qualified.'))
153
154 def testCron(self):
155 file_system = TestFileSystem({
156 'apps_sidenav.json': '[{ "title": "H1" }]' ,
157 'extensions_sidenav.json': '[{ "title": "H2" }]'
158 })
159
160 # Ensure Cron doesn't rely on request.
161 sidenav_data_source = SidenavDataSource(
162 FakeServerInstance(file_system), request=None)
163 sidenav_data_source.Cron()
164
165 # If Cron fails, apps_sidenav.json will not be cached, and the _cache_data
166 # access will fail.
167 # TODO(jshumway): Make a non hack version of this check.
168 sidenav_data_source._cache._file_object_store.Get(
169 '/apps_sidenav.json').Get()._cache_data
170
66 171
67 if __name__ == '__main__': 172 if __name__ == '__main__':
68 unittest.main() 173 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698