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