def testSimple(self):
        self._base_path = os.path.join(self._base_path, 'simple')
        fetcher = LocalFileSystem(self._base_path)
        compiled_fs_factory = CompiledFileSystem.Factory(
            fetcher, ObjectStoreCreator.Factory())
        t_data_source = self._CreateTemplateDataSource(
            compiled_fs_factory, ObjectStoreCreator.Factory())
        template_a1 = Handlebar(self._ReadLocalFile('test1.html'))
        self.assertEqual(
            template_a1.render({}, {
                'templates': {}
            }).text,
            t_data_source.get('test1').render({}, {
                'templates': {}
            }).text)

        template_a2 = Handlebar(self._ReadLocalFile('test2.html'))
        self.assertEqual(
            template_a2.render({}, {
                'templates': {}
            }).text,
            t_data_source.get('test2').render({}, {
                'templates': {}
            }).text)

        self.assertEqual(None, t_data_source.get('junk.html'))
Esempio n. 2
0
 def testFactoryWithBranch(self):
   store = ObjectStoreCreator.Factory().Create(
       _FooClass, store_type=TestObjectStore).Create()
   self.assertEqual('_FooClass', store.namespace)
   store = ObjectStoreCreator.Factory(branch='dev').Create(
       _FooClass, store_type=TestObjectStore).Create()
   self.assertEqual('_FooClass@dev', store.namespace)
Esempio n. 3
0
    def CreateOnline(channel):
        '''Creates/creates an online server instance, meaning that both local and
    subversion/github resources are queried.
    '''
        branch_utility = ServerInstance._GetOrCreateBranchUtility()
        branch = branch_utility.GetBranchNumberForChannelName(channel)

        if branch == 'trunk':
            svn_url = '/'.join((url_constants.SVN_TRUNK_URL, 'src',
                                svn_constants.EXTENSIONS_PATH))
        else:
            svn_url = '/'.join((url_constants.SVN_BRANCH_URL, branch, 'src',
                                svn_constants.EXTENSIONS_PATH))

        viewvc_url = svn_url.replace(url_constants.SVN_URL,
                                     url_constants.VIEWVC_URL)

        object_store_creator_factory = ObjectStoreCreator.Factory(
            GetAppVersion(), branch, start_empty=True)

        svn_file_system = CachingFileSystem(
            SubversionFileSystem(AppEngineUrlFetcher(svn_url),
                                 AppEngineUrlFetcher(viewvc_url)),
            object_store_creator_factory)

        return ServerInstance(channel, object_store_creator_factory,
                              svn_file_system,
                              ServerInstance._GetOrCreateGithubFileSystem())
Esempio n. 4
0
 def setUp(self):
     object_store_creator_factory = ObjectStoreCreator.Factory()
     self._file_system = CachingFileSystem(
         LocalFileSystem(os.path.join(sys.path[0], 'test_data')),
         object_store_creator_factory)
     self._example_zipper = ExampleZipper(
         self._file_system,
         CompiledFileSystem.Factory(self._file_system,
                                    object_store_creator_factory),
         'example_zipper')
 def testPartials(self):
     self._base_path = os.path.join(self._base_path, 'partials')
     fetcher = LocalFileSystem(self._base_path)
     compiled_fs_factory = CompiledFileSystem.Factory(
         fetcher, ObjectStoreCreator.Factory())
     t_data_source = self._CreateTemplateDataSource(compiled_fs_factory)
     self.assertEqual(
         self._ReadLocalFile('test_expected.html'),
         t_data_source.get('test_tmpl').render(
             json.loads(self._ReadLocalFile('input.json')),
             t_data_source).text)
Esempio n. 6
0
    def testListDir(self):
        file_system = CachingFileSystem(_CreateLocalFs(),
                                        ObjectStoreCreator.Factory())
        expected = ['dir/'] + ['file%d.html' % i for i in range(7)]
        file_system._read_object_store.Set(
            'list/', (expected, file_system.Stat('list/').version))
        self.assertEqual(expected, sorted(file_system.ReadSingle('list/')))

        expected.remove('file0.html')
        file_system._read_object_store.Set(
            'list/', (expected, file_system.Stat('list/').version))
        self.assertEqual(expected, sorted(file_system.ReadSingle('list/')))
Esempio n. 7
0
 def testReadFiles(self):
     file_system = CachingFileSystem(_CreateLocalFs(),
                                     ObjectStoreCreator.Factory())
     expected = {
         './test1.txt': 'test1\n',
         './test2.txt': 'test2\n',
         './test3.txt': 'test3\n',
     }
     self.assertEqual(
         expected,
         file_system.Read(['./test1.txt', './test2.txt',
                           './test3.txt']).Get())
 def testRender(self):
     self._base_path = os.path.join(self._base_path, 'render')
     fetcher = LocalFileSystem(self._base_path)
     context = json.loads(self._ReadLocalFile('test1.json'))
     compiled_fs_factory = CompiledFileSystem.Factory(
         fetcher, ObjectStoreCreator.Factory())
     self._RenderTest(
         'test1',
         self._CreateTemplateDataSource(
             compiled_fs_factory,
             api_data=json.loads(self._ReadLocalFile('test1.json'))))
     self._RenderTest(
         'test2',
         self._CreateTemplateDataSource(
             compiled_fs_factory,
             api_data=json.loads(self._ReadLocalFile('test2.json'))))
 def _CreateTemplateDataSource(self, compiled_fs_factory, api_data=None):
     if api_data is None:
         api_data_factory = APIDataSource.Factory(compiled_fs_factory,
                                                  'fake_path')
     else:
         api_data_factory = _FakeFactory(api_data)
     reference_resolver_factory = ReferenceResolver.Factory(
         api_data_factory, self._fake_api_list_data_source_factory,
         ObjectStoreCreator.Factory())
     return (TemplateDataSource.Factory(
         'fake_channel', api_data_factory,
         self._fake_api_list_data_source_factory,
         self._fake_intro_data_source_factory,
         self._fake_samples_data_source_factory,
         self._fake_sidenav_data_source_factory, compiled_fs_factory,
         reference_resolver_factory, '.',
         '.').Create(_FakeRequest(), 'extensions/foo'))
Esempio n. 10
0
 def GetOrCreateOffline(channel):
     '''Gets/creates a local ServerInstance, meaning that only resources local to
 the server - memcache, object store, etc, are queried. This amounts to not
 setting up the subversion nor github file systems.
 '''
     branch_utility = ServerInstance._GetOrCreateBranchUtility()
     branch = branch_utility.GetBranchNumberForChannelName(channel)
     object_store_creator_factory = ObjectStoreCreator.Factory(branch)
     # No svn nor github file systems. Rely on the crons to fill the caches, and
     # for the caches to exist.
     return ServerInstance(
         channel,
         object_store_creator_factory,
         CachingFileSystem(OfflineFileSystem(SubversionFileSystem),
                           object_store_creator_factory),
         # TODO(kalman): convert GithubFileSystem to be wrappable in a
         # CachingFileSystem so that it can be replaced with an
         # OfflineFileSystem. Currently GFS doesn't set the child versions of
         # stat requests so it doesn't.
         ServerInstance._GetOrCreateGithubFileSystem())
 def setUp(self):
     self._factory = APIListDataSource.Factory(
         CompiledFileSystem.Factory(TestFileSystem(deepcopy(_TEST_DATA)),
                                    ObjectStoreCreator.Factory()), 'api',
         'public')
Esempio n. 12
0
 def CreateForTest(file_system):
     return ServerInstance('test', ObjectStoreCreator.Factory('test'),
                           file_system, None)
 def setUp(self):
     test_fs = TestFileSystem(_TEST_DATA)
     compiled_fs_factory = CompiledFileSystem.Factory(
         test_fs, ObjectStoreCreator.Factory())
     self._path_canonicalizer = PathCanonicalizer('stable',
                                                  compiled_fs_factory)
 def _CreateRefResolver(self, filename):
     data_source = FakeAPIAndListDataSource(self._LoadJSON(filename))
     return ReferenceResolver.Factory(
         data_source, data_source, ObjectStoreCreator.Factory()).Create()
Esempio n. 15
0
    def testCaching(self):
        fake_fs = TestFileSystem({
            'bob': {
                'bob0': 'bob/bob0 contents',
                'bob1': 'bob/bob1 contents',
                'bob2': 'bob/bob2 contents',
                'bob3': 'bob/bob3 contents',
            }
        })
        file_system = CachingFileSystem(fake_fs, ObjectStoreCreator.Factory())

        self.assertEqual('bob/bob0 contents',
                         file_system.ReadSingle('bob/bob0'))
        self.assertTrue(fake_fs.CheckAndReset(read_count=1, stat_count=1))

        # Resource has been cached, so test resource is not re-fetched.
        self.assertEqual('bob/bob0 contents',
                         file_system.ReadSingle('bob/bob0'))
        self.assertTrue(fake_fs.CheckAndReset())

        # Test if the Stat version is the same the resource is not re-fetched.
        file_system._stat_object_store.Delete('bob/bob0')
        self.assertEqual('bob/bob0 contents',
                         file_system.ReadSingle('bob/bob0'))
        self.assertTrue(fake_fs.CheckAndReset(stat_count=1))

        # Test if there is a newer version, the resource is re-fetched.
        file_system._stat_object_store.Delete('bob/bob0')
        fake_fs.IncrementStat()
        self.assertEqual('bob/bob0 contents',
                         file_system.ReadSingle('bob/bob0'))
        self.assertTrue(fake_fs.CheckAndReset(read_count=1, stat_count=1))

        # Test directory and subdirectory stats are cached.
        file_system._stat_object_store.Delete('bob/bob0')
        file_system._read_object_store.Delete('bob/bob0')
        file_system._stat_object_store.Delete('bob/bob1')
        fake_fs.IncrementStat()
        self.assertEqual('bob/bob1 contents',
                         file_system.ReadSingle('bob/bob1'))
        self.assertEqual('bob/bob0 contents',
                         file_system.ReadSingle('bob/bob0'))
        self.assertTrue(fake_fs.CheckAndReset(read_count=2, stat_count=1))
        self.assertEqual('bob/bob1 contents',
                         file_system.ReadSingle('bob/bob1'))
        self.assertTrue(fake_fs.CheckAndReset())

        # Test a more recent parent directory doesn't force a refetch of children.
        file_system._read_object_store.Delete('bob/bob0')
        file_system._read_object_store.Delete('bob/bob1')
        self.assertEqual('bob/bob1 contents',
                         file_system.ReadSingle('bob/bob1'))
        self.assertEqual('bob/bob2 contents',
                         file_system.ReadSingle('bob/bob2'))
        self.assertEqual('bob/bob3 contents',
                         file_system.ReadSingle('bob/bob3'))
        self.assertTrue(fake_fs.CheckAndReset(read_count=3))
        fake_fs.IncrementStat(path='bob/')
        self.assertEqual('bob/bob1 contents',
                         file_system.ReadSingle('bob/bob1'))
        self.assertEqual('bob/bob2 contents',
                         file_system.ReadSingle('bob/bob2'))
        self.assertEqual('bob/bob3 contents',
                         file_system.ReadSingle('bob/bob3'))
        self.assertTrue(fake_fs.CheckAndReset())

        file_system._stat_object_store.Delete('bob/bob0')
        self.assertEqual('bob/bob0 contents',
                         file_system.ReadSingle('bob/bob0'))
        self.assertTrue(fake_fs.CheckAndReset(read_count=1, stat_count=1))
        self.assertEqual('bob/bob0 contents',
                         file_system.ReadSingle('bob/bob0'))
        self.assertTrue(fake_fs.CheckAndReset())
def _CreateFactory():
    return CompiledFileSystem.Factory(TestFileSystem(deepcopy(_TEST_DATA)),
                                      ObjectStoreCreator.Factory(
                                          '3-0', 'test'),
                                      store_type=TestObjectStore)
Esempio n. 17
0
 def setUp(self):
     self._base_path = os.path.join(sys.path[0], 'test_data',
                                    'sidenav_data_source')
     self._compiled_fs_factory = CompiledFileSystem.Factory(
         LocalFileSystem(self._base_path), ObjectStoreCreator.Factory())