OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 from compiled_file_system import CompiledFileSystem |
| 7 from copy import deepcopy |
| 8 from file_system import FileNotFoundError |
| 9 from test_file_system import TestFileSystem |
| 10 from test_object_store import TestObjectStore |
| 11 import unittest |
| 12 |
| 13 _TEST_DATA = { |
| 14 '404.html': '404.html contents', |
| 15 'apps': { |
| 16 'a11y.html': 'a11y.html contents', |
| 17 'about_apps.html': 'about_apps.html contents', |
| 18 'fakedir': { |
| 19 'file.html': 'file.html contents' |
| 20 } |
| 21 }, |
| 22 'extensions': { |
| 23 'activeTab.html': 'activeTab.html contents', |
| 24 'alarms.html': 'alarms.html contents' |
| 25 } |
| 26 } |
| 27 |
| 28 def _CreateFactory(): |
| 29 return CompiledFileSystem.Factory(TestFileSystem(deepcopy(_TEST_DATA)), |
| 30 store_type=TestObjectStore) |
| 31 |
| 32 class CompiledFileSystemTest(unittest.TestCase): |
| 33 def testIdentityNamespace(self): |
| 34 factory = _CreateFactory() |
| 35 compiled_fs = factory.GetOrCreateIdentity() |
| 36 self.assertEqual('CompiledFileSystem/id/file', |
| 37 compiled_fs._file_object_store.namespace) |
| 38 compiled_fs = factory.GetOrCreateIdentity() # should be the same |
| 39 self.assertEqual('CompiledFileSystem/id/list', |
| 40 compiled_fs._list_object_store.namespace) |
| 41 |
| 42 def testIdentityFromFile(self): |
| 43 compiled_fs = _CreateFactory().GetOrCreateIdentity() |
| 44 self.assertEqual('404.html contents', compiled_fs.GetFromFile('404.html')) |
| 45 self.assertEqual('a11y.html contents', |
| 46 compiled_fs.GetFromFile('apps/a11y.html')) |
| 47 self.assertEqual('file.html contents', |
| 48 compiled_fs.GetFromFile('/apps/fakedir/file.html')) |
| 49 |
| 50 def testIdentityFromFileListing(self): |
| 51 compiled_fs = _CreateFactory().GetOrCreateIdentity() |
| 52 self.assertEqual({'404.html', 'apps/a11y.html', 'apps/about_apps.html', |
| 53 'apps/fakedir/file.html', |
| 54 'extensions/activeTab.html', 'extensions/alarms.html'}, |
| 55 set(compiled_fs.GetFromFileListing('/'))) |
| 56 self.assertEqual({'a11y.html', 'about_apps.html', 'fakedir/file.html'}, |
| 57 set(compiled_fs.GetFromFileListing('apps/'))) |
| 58 self.assertEqual({'file.html'}, |
| 59 set(compiled_fs.GetFromFileListing('apps/fakedir'))) |
| 60 |
| 61 def testPopulateNamespace(self): |
| 62 def CheckNamespace(expected_file, expected_list, fs): |
| 63 self.assertEqual(expected_file, fs._file_object_store.namespace) |
| 64 self.assertEqual(expected_list, fs._list_object_store.namespace) |
| 65 factory = _CreateFactory() |
| 66 f = lambda x: x |
| 67 CheckNamespace('CompiledFileSystem/CompiledFileSystemTest/file', |
| 68 'CompiledFileSystem/CompiledFileSystemTest/list', |
| 69 factory.Create(f, CompiledFileSystemTest)) |
| 70 CheckNamespace('CompiledFileSystem/CompiledFileSystemTest/foo/file', |
| 71 'CompiledFileSystem/CompiledFileSystemTest/foo/list', |
| 72 factory.Create(f, CompiledFileSystemTest, category='foo')) |
| 73 CheckNamespace('CompiledFileSystem/CompiledFileSystemTest/file/6', |
| 74 'CompiledFileSystem/CompiledFileSystemTest/list/6', |
| 75 factory.Create(f, CompiledFileSystemTest, version=6)) |
| 76 CheckNamespace('CompiledFileSystem/CompiledFileSystemTest/foo/file/6', |
| 77 'CompiledFileSystem/CompiledFileSystemTest/foo/list/6', |
| 78 factory.Create(f, CompiledFileSystemTest, category='foo', |
| 79 version=6)) |
| 80 |
| 81 def testPopulateFromFile(self): |
| 82 def Sleepy(key, val): |
| 83 return '%s%s' % ('Z' * len(key), 'z' * len(val)) |
| 84 compiled_fs = _CreateFactory().Create(Sleepy, CompiledFileSystemTest) |
| 85 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz', |
| 86 compiled_fs.GetFromFile('404.html')) |
| 87 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', |
| 88 compiled_fs.GetFromFile('apps/a11y.html')) |
| 89 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', |
| 90 compiled_fs.GetFromFile('/apps/fakedir/file.html')) |
| 91 |
| 92 def testCaching(self): |
| 93 compiled_fs = _CreateFactory().GetOrCreateIdentity() |
| 94 self.assertEqual('404.html contents', compiled_fs.GetFromFile('404.html')) |
| 95 self.assertEqual({'file.html'}, |
| 96 set(compiled_fs.GetFromFileListing('apps/fakedir'))) |
| 97 |
| 98 compiled_fs._file_system._obj['404.html'] = 'boom' |
| 99 compiled_fs._file_system._obj['apps']['fakedir']['boom.html'] = 'blam' |
| 100 self.assertEqual('404.html contents', compiled_fs.GetFromFile('404.html')) |
| 101 self.assertEqual({'file.html'}, |
| 102 set(compiled_fs.GetFromFileListing('apps/fakedir'))) |
| 103 |
| 104 compiled_fs._file_system.IncrementStat() |
| 105 self.assertEqual('boom', compiled_fs.GetFromFile('404.html')) |
| 106 self.assertEqual({'file.html', 'boom.html'}, |
| 107 set(compiled_fs.GetFromFileListing('apps/fakedir'))) |
| 108 |
| 109 def testFailures(self): |
| 110 compiled_fs = _CreateFactory().GetOrCreateIdentity() |
| 111 self.assertRaises(FileNotFoundError, compiled_fs.GetFromFile, '405.html') |
| 112 # TODO(kalman): would be nice to test this fails since apps/ is a dir. |
| 113 compiled_fs.GetFromFile('apps/') |
| 114 #self.assertRaises(SomeError, compiled_fs.GetFromFile, 'apps/') |
| 115 self.assertRaises(FileNotFoundError, |
| 116 compiled_fs.GetFromFileListing, 'nodir/') |
| 117 # TODO(kalman): likewise, not a FileNotFoundError. |
| 118 self.assertRaises(FileNotFoundError, |
| 119 compiled_fs.GetFromFileListing, '404.html') |
| 120 |
| 121 if __name__ == '__main__': |
| 122 unittest.main() |
OLD | NEW |