| 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 import unittest |
| 7 from caching_rietveld_patcher import (CachingRietveldPatcher, |
| 8 _VERSION_CACHE_MAXAGE) |
| 9 from datetime import datetime |
| 10 from file_system import FileNotFoundError |
| 11 from future import Future |
| 12 from object_store_creator import ObjectStoreCreator |
| 13 from patcher import Patcher |
| 14 |
| 15 class TestPatcher(Patcher): |
| 16 def __init__(self, version='1'): |
| 17 self.get_version_count = 0 |
| 18 self.get_patched_files_count = 0 |
| 19 self.apply_count = 0 |
| 20 self.version = version |
| 21 |
| 22 def GetVersion(self): |
| 23 self.get_version_count += 1 |
| 24 return self.version |
| 25 |
| 26 def GetPatchedFiles(self, version=None): |
| 27 self.get_patched_files_count += 1 |
| 28 return (['add.txt'], ['del.txt'], ['modify.txt']) |
| 29 |
| 30 def Apply(self, paths, file_system, binary, version=None): |
| 31 # CachingRietveldPatcher should always call Apply with binary=True. |
| 32 assert binary |
| 33 self.apply_count += 1 |
| 34 data = { |
| 35 'add.txt': 'add', |
| 36 'modify.txt': 'modify', |
| 37 } |
| 38 return Future(value={path: data[path] for path in paths}) |
| 39 |
| 40 class FakeDateTime(object): |
| 41 def __init__(self, time=datetime.now()): |
| 42 self.time = time |
| 43 |
| 44 def now(self): |
| 45 return self.time |
| 46 |
| 47 class CachingRietveldPatcherTest(unittest.TestCase): |
| 48 def setUp(self): |
| 49 self._datetime = FakeDateTime() |
| 50 self._test_patcher = TestPatcher() |
| 51 self._patcher = CachingRietveldPatcher( |
| 52 self._test_patcher, |
| 53 ObjectStoreCreator('test', start_empty=False), |
| 54 self._datetime) |
| 55 |
| 56 def testGetVersion(self): |
| 57 # Invalidate cache. |
| 58 self._datetime.time += _VERSION_CACHE_MAXAGE |
| 59 # Fill cache. |
| 60 self._patcher.GetVersion() |
| 61 count = self._test_patcher.get_version_count |
| 62 # Should read from cache. |
| 63 self._patcher.GetVersion() |
| 64 self.assertEqual(count, self._test_patcher.get_version_count) |
| 65 # Invalidate cache. |
| 66 self._datetime.time += _VERSION_CACHE_MAXAGE |
| 67 # Should fetch version. |
| 68 self._patcher.GetVersion() |
| 69 self.assertEqual(count + 1, self._test_patcher.get_version_count) |
| 70 |
| 71 def testGetPatchedFiles(self): |
| 72 # Fill cache. |
| 73 self._patcher.GetPatchedFiles() |
| 74 count = self._test_patcher.get_patched_files_count |
| 75 # Should read from cache. |
| 76 self._patcher.GetPatchedFiles() |
| 77 self.assertEqual(count, self._test_patcher.get_patched_files_count) |
| 78 |
| 79 def testApply(self): |
| 80 # Fill cache. |
| 81 self._patcher.Apply(['add.txt'], None).Get() |
| 82 count = self._test_patcher.apply_count |
| 83 # Should read from cache even though it's reading another file. |
| 84 self._patcher.Apply(['modify.txt'], None).Get() |
| 85 self.assertEqual(count, self._test_patcher.apply_count) |
| 86 |
| 87 # Make sure RietveldPatcher handles |binary| correctly. |
| 88 self.assertTrue(isinstance(self._patcher.Apply(['add.txt'], None, True). |
| 89 Get()['add.txt'], str), |
| 90 'Expected result is binary. It was text.') |
| 91 self.assertTrue(isinstance(self._patcher.Apply(['add.txt'], None). |
| 92 Get()['add.txt'], unicode), |
| 93 'Expected result is text. It was binary.') |
| 94 |
| 95 if __name__ == '__main__': |
| 96 unittest.main() |
| OLD | NEW |