Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/caching_rietveld_patcher_test.py |
| =================================================================== |
| --- chrome/common/extensions/docs/server2/caching_rietveld_patcher_test.py (revision 0) |
| +++ chrome/common/extensions/docs/server2/caching_rietveld_patcher_test.py (revision 0) |
| @@ -0,0 +1,96 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import unittest |
| +from caching_rietveld_patcher import (CachingRietveldPatcher, |
| + _VERSION_CACHE_MAXAGE) |
| +from datetime import datetime |
| +from file_system import FileNotFoundError |
| +from future import Future |
| +from object_store_creator import ObjectStoreCreator |
| +from patcher import Patcher |
| + |
| +class TestPatcher(Patcher): |
|
not at google - send to devlin
2013/05/11 20:39:53
pull this into its own file. maybe a class in the
方觉(Fang Jue)
2013/05/12 03:01:47
Done.
|
| + def __init__(self, version='1'): |
| + self.get_version_count = 0 |
| + self.get_patched_files_count = 0 |
| + self.apply_count = 0 |
| + self.version = version |
| + |
| + def GetVersion(self): |
| + self.get_version_count += 1 |
| + return self.version |
| + |
| + def GetPatchedFiles(self, version=None): |
| + self.get_patched_files_count += 1 |
| + return (['add.txt'], ['del.txt'], ['modify.txt']) |
| + |
| + def Apply(self, paths, file_system, binary, version=None): |
| + # CachingRietveldPatcher should always call Apply with binary=True. |
| + assert binary |
| + self.apply_count += 1 |
| + data = { |
| + 'add.txt': 'add', |
| + 'modify.txt': 'modify', |
| + } |
| + return Future(value={path: data[path] for path in paths}) |
| + |
| +class FakeDateTime(object): |
| + def __init__(self, time=datetime.now()): |
| + self.time = time |
| + |
| + def now(self): |
| + return self.time |
| + |
| +class CachingRietveldPatcherTest(unittest.TestCase): |
| + def setUp(self): |
| + self._datetime = FakeDateTime() |
| + self._test_patcher = TestPatcher() |
| + self._patcher = CachingRietveldPatcher( |
| + self._test_patcher, |
| + ObjectStoreCreator('test', start_empty=False), |
| + self._datetime) |
| + |
| + def testGetVersion(self): |
| + # Invalidate cache. |
| + self._datetime.time += _VERSION_CACHE_MAXAGE |
| + # Fill cache. |
| + self._patcher.GetVersion() |
| + count = self._test_patcher.get_version_count |
| + # Should read from cache. |
| + self._patcher.GetVersion() |
| + self.assertEqual(count, self._test_patcher.get_version_count) |
| + # Invalidate cache. |
| + self._datetime.time += _VERSION_CACHE_MAXAGE |
| + # Should fetch version. |
| + self._patcher.GetVersion() |
| + self.assertEqual(count + 1, self._test_patcher.get_version_count) |
| + |
| + def testGetPatchedFiles(self): |
| + # Fill cache. |
| + self._patcher.GetPatchedFiles() |
| + count = self._test_patcher.get_patched_files_count |
| + # Should read from cache. |
| + self._patcher.GetPatchedFiles() |
| + self.assertEqual(count, self._test_patcher.get_patched_files_count) |
| + |
| + def testApply(self): |
| + # Fill cache. |
| + self._patcher.Apply(['add.txt'], None).Get() |
| + count = self._test_patcher.apply_count |
| + # Should read from cache even though it's reading another file. |
| + self._patcher.Apply(['modify.txt'], None).Get() |
| + self.assertEqual(count, self._test_patcher.apply_count) |
| + |
| + # Make sure RietveldPatcher handles |binary| correctly. |
| + self.assertTrue(isinstance(self._patcher.Apply(['add.txt'], None, True). |
| + Get()['add.txt'], str), |
| + 'Expected result is binary. It was text.') |
| + self.assertTrue(isinstance(self._patcher.Apply(['add.txt'], None). |
| + Get()['add.txt'], unicode), |
| + 'Expected result is text. It was binary.') |
| + |
| +if __name__ == '__main__': |
| + unittest.main() |
| Property changes on: chrome/common/extensions/docs/server2/caching_rietveld_patcher_test.py |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |
| Added: svn:executable |
| + * |