OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 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 from cStringIO import StringIO | 6 from cStringIO import StringIO |
7 import json | 7 import json |
8 import unittest | 8 import unittest |
9 from zipfile import ZipFile | 9 from zipfile import ZipFile |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 'noextension': 'noextension content', | 69 'noextension': 'noextension content', |
70 'run.js': 'run.js content', | 70 'run.js': 'run.js content', |
71 'site.css': 'site.css content', | 71 'site.css': 'site.css content', |
72 'storage.html': 'storage.html content', | 72 'storage.html': 'storage.html content', |
73 'markdown.md': '\n'.join(text[0] for text in _MARKDOWN_CONTENT) | 73 'markdown.md': '\n'.join(text[0] for text in _MARKDOWN_CONTENT) |
74 } | 74 } |
75 | 75 |
76 | 76 |
77 class ContentProviderUnittest(unittest.TestCase): | 77 class ContentProviderUnittest(unittest.TestCase): |
78 def setUp(self): | 78 def setUp(self): |
| 79 self._test_file_system = TestFileSystem(_TEST_DATA) |
79 self._content_provider = self._CreateContentProvider() | 80 self._content_provider = self._CreateContentProvider() |
80 | 81 |
81 def _CreateContentProvider(self, supports_zip=False): | 82 def _CreateContentProvider(self, supports_zip=False): |
82 object_store_creator = ObjectStoreCreator.ForTest() | 83 object_store_creator = ObjectStoreCreator.ForTest() |
83 test_file_system = TestFileSystem(_TEST_DATA) | |
84 return ContentProvider( | 84 return ContentProvider( |
85 'foo', | 85 'foo', |
86 CompiledFileSystem.Factory(object_store_creator), | 86 CompiledFileSystem.Factory(object_store_creator), |
87 test_file_system, | 87 self._test_file_system, |
88 object_store_creator, | 88 object_store_creator, |
89 default_extensions=('.html', '.md'), | 89 default_extensions=('.html', '.md'), |
90 # TODO(kalman): Test supports_templates=False. | 90 # TODO(kalman): Test supports_templates=False. |
91 supports_templates=True, | 91 supports_templates=True, |
92 supports_zip=supports_zip) | 92 supports_zip=supports_zip) |
93 | 93 |
94 def _assertContent(self, content, content_type, content_and_type): | 94 def _assertContent(self, content, content_type, content_and_type): |
95 # Assert type so that str is differentiated from unicode. | 95 # Assert type so that str is differentiated from unicode. |
96 self.assertEqual(type(content), type(content_and_type.content)) | 96 self.assertEqual(type(content), type(content_and_type.content)) |
97 self.assertEqual(content, content_and_type.content) | 97 self.assertEqual(content, content_and_type.content) |
98 self.assertEqual(content_type, content_and_type.content_type) | 98 self.assertEqual(content_type, content_and_type.content_type) |
99 | 99 |
100 def _assertTemplateContent(self, content, path): | 100 def _assertTemplateContent(self, content, path, version): |
101 content_and_type = self._content_provider.GetContentAndType(path).Get() | 101 content_and_type = self._content_provider.GetContentAndType(path).Get() |
102 self.assertEqual(Handlebar, type(content_and_type.content)) | 102 self.assertEqual(Handlebar, type(content_and_type.content)) |
103 content_and_type.content = content_and_type.content.source | 103 content_and_type.content = content_and_type.content.source |
104 self._assertContent(content, 'text/html', content_and_type) | 104 self._assertContent(content, 'text/html', content_and_type) |
| 105 self.assertEqual(version, self._content_provider.GetVersion(path).Get()) |
105 | 106 |
106 def _assertMarkdownContent(self, content, path): | 107 def _assertMarkdownContent(self, content, path, version): |
107 content_and_type = self._content_provider.GetContentAndType(path).Get() | 108 content_and_type = self._content_provider.GetContentAndType(path).Get() |
108 content_and_type.content = content_and_type.content.source | 109 content_and_type.content = content_and_type.content.source |
109 self._assertContent(content, 'text/html', content_and_type) | 110 self._assertContent(content, 'text/html', content_and_type) |
| 111 self.assertEqual(version, self._content_provider.GetVersion(path).Get()) |
110 | 112 |
111 def testPlainText(self): | 113 def testPlainText(self): |
112 self._assertContent( | 114 self._assertContent( |
113 u'a.txt content', 'text/plain', | 115 u'a.txt content', 'text/plain', |
114 self._content_provider.GetContentAndType('dir/a.txt').Get()) | 116 self._content_provider.GetContentAndType('dir/a.txt').Get()) |
115 self._assertContent( | 117 self._assertContent( |
116 u'd.txt content', 'text/plain', | 118 u'd.txt content', 'text/plain', |
117 self._content_provider.GetContentAndType('dir/c/d.txt').Get()) | 119 self._content_provider.GetContentAndType('dir/c/d.txt').Get()) |
118 self._assertContent( | 120 self._assertContent( |
119 u'read.txt content', 'text/plain', | 121 u'read.txt content', 'text/plain', |
120 self._content_provider.GetContentAndType('read.txt').Get()) | 122 self._content_provider.GetContentAndType('read.txt').Get()) |
121 self._assertContent( | 123 self._assertContent( |
122 unicode(_REDIRECTS_JSON, 'utf-8'), 'application/json', | 124 unicode(_REDIRECTS_JSON, 'utf-8'), 'application/json', |
123 self._content_provider.GetContentAndType('redirects.json').Get()) | 125 self._content_provider.GetContentAndType('redirects.json').Get()) |
124 self._assertContent( | 126 self._assertContent( |
125 u'run.js content', 'application/javascript', | 127 u'run.js content', 'application/javascript', |
126 self._content_provider.GetContentAndType('run.js').Get()) | 128 self._content_provider.GetContentAndType('run.js').Get()) |
127 self._assertContent( | 129 self._assertContent( |
128 u'site.css content', 'text/css', | 130 u'site.css content', 'text/css', |
129 self._content_provider.GetContentAndType('site.css').Get()) | 131 self._content_provider.GetContentAndType('site.css').Get()) |
130 | 132 |
131 def testTemplate(self): | 133 def testTemplate(self): |
132 self._assertTemplateContent(u'storage.html content', 'storage.html') | 134 self._assertTemplateContent(u'storage.html content', 'storage.html', '0') |
| 135 self._test_file_system.IncrementStat('storage.html') |
| 136 self._assertTemplateContent(u'storage.html content', 'storage.html', '1') |
133 | 137 |
134 def testImage(self): | 138 def testImage(self): |
135 self._assertContent( | 139 self._assertContent( |
136 'img.png content', 'image/png', | 140 'img.png content', 'image/png', |
137 self._content_provider.GetContentAndType('img.png').Get()) | 141 self._content_provider.GetContentAndType('img.png').Get()) |
138 | 142 |
139 def testZipTopLevel(self): | 143 def testZipTopLevel(self): |
140 zip_content_provider = self._CreateContentProvider(supports_zip=True) | 144 zip_content_provider = self._CreateContentProvider(supports_zip=True) |
141 content_and_type = zip_content_provider.GetContentAndType('dir.zip').Get() | 145 content_and_type = zip_content_provider.GetContentAndType('dir.zip').Get() |
142 zipfile = ZipFile(StringIO(content_and_type.content)) | 146 zipfile = ZipFile(StringIO(content_and_type.content)) |
(...skipping 24 matching lines...) Expand all Loading... |
167 # corresponds to the canonical directory. | 171 # corresponds to the canonical directory. |
168 zip_content_provider = self._CreateContentProvider(supports_zip=True) | 172 zip_content_provider = self._CreateContentProvider(supports_zip=True) |
169 self.assertEqual( | 173 self.assertEqual( |
170 'dir.zip', | 174 'dir.zip', |
171 zip_content_provider.GetCanonicalPath('dir.zip')) | 175 zip_content_provider.GetCanonicalPath('dir.zip')) |
172 self.assertEqual( | 176 self.assertEqual( |
173 'dir.zip', | 177 'dir.zip', |
174 zip_content_provider.GetCanonicalPath('diR.zip')) | 178 zip_content_provider.GetCanonicalPath('diR.zip')) |
175 | 179 |
176 def testMarkdown(self): | 180 def testMarkdown(self): |
177 self._assertMarkdownContent( | 181 expected_content = '\n'.join(text[1] for text in _MARKDOWN_CONTENT) |
178 '\n'.join(text[1] for text in _MARKDOWN_CONTENT), | 182 self._assertMarkdownContent(expected_content, 'markdown', '0') |
179 'markdown') | 183 self._test_file_system.IncrementStat('markdown.md') |
| 184 self._assertMarkdownContent(expected_content, 'markdown', '1') |
180 | 185 |
181 def testNotFound(self): | 186 def testNotFound(self): |
182 self.assertRaises( | 187 self.assertRaises( |
183 FileNotFoundError, | 188 FileNotFoundError, |
184 self._content_provider.GetContentAndType('oops').Get) | 189 self._content_provider.GetContentAndType('oops').Get) |
185 | 190 |
186 def testIndexRedirect(self): | 191 def testIndexRedirect(self): |
187 self._assertTemplateContent(u'index.html content', '') | 192 self._assertTemplateContent(u'index.html content', '', '0') |
188 self._assertTemplateContent(u'index.html content 1', 'dir4') | 193 self._assertTemplateContent(u'index.html content 1', 'dir4', '0') |
189 self._assertTemplateContent(u'dir5.html content', 'dir5') | 194 self._assertTemplateContent(u'dir5.html content', 'dir5', '0') |
190 self._assertMarkdownContent( | 195 self._assertMarkdownContent( |
191 '\n'.join(text[1] for text in _MARKDOWN_CONTENT), | 196 '\n'.join(text[1] for text in _MARKDOWN_CONTENT), |
192 'dir7') | 197 'dir7', |
| 198 '0') |
193 self._assertContent( | 199 self._assertContent( |
194 'noextension content', 'text/plain', | 200 'noextension content', 'text/plain', |
195 self._content_provider.GetContentAndType('noextension').Get()) | 201 self._content_provider.GetContentAndType('noextension').Get()) |
196 self.assertRaises( | 202 self.assertRaises( |
197 FileNotFoundError, | 203 FileNotFoundError, |
198 self._content_provider.GetContentAndType('dir6').Get) | 204 self._content_provider.GetContentAndType('dir6').Get) |
199 | 205 |
| 206 def testCron(self): |
| 207 # Not entirely sure what to test here, but get some code coverage. |
| 208 self._content_provider.Cron().Get() |
| 209 |
| 210 |
200 if __name__ == '__main__': | 211 if __name__ == '__main__': |
201 unittest.main() | 212 unittest.main() |
OLD | NEW |