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 6import unittest 7from caching_rietveld_patcher import (CachingRietveldPatcher, 8 _VERSION_CACHE_MAXAGE) 9from datetime import datetime 10from object_store_creator import ObjectStoreCreator 11from test_patcher import TestPatcher 12 13_TEST_PATCH_VERSION = '1' 14_TEST_PATCH_FILES = (['add.txt'], ['del.txt'], ['modify.txt']) 15_TEST_PATCH_DATA = { 16 'add.txt': 'add', 17 'modify.txt': 'modify', 18} 19 20class FakeDateTime(object): 21 def __init__(self, time=datetime.now()): 22 self.time = time 23 24 def now(self): 25 return self.time 26 27class CachingRietveldPatcherTest(unittest.TestCase): 28 def setUp(self): 29 self._datetime = FakeDateTime() 30 self._test_patcher = TestPatcher(_TEST_PATCH_VERSION, 31 _TEST_PATCH_FILES, 32 _TEST_PATCH_DATA) 33 self._patcher = CachingRietveldPatcher( 34 self._test_patcher, 35 ObjectStoreCreator(start_empty=False), 36 self._datetime) 37 38 def testGetVersion(self): 39 # Invalidate cache. 40 self._datetime.time += _VERSION_CACHE_MAXAGE 41 # Fill cache. 42 self._patcher.GetVersion() 43 count = self._test_patcher.get_version_count 44 # Should read from cache. 45 self._patcher.GetVersion() 46 self.assertEqual(count, self._test_patcher.get_version_count) 47 # Invalidate cache. 48 self._datetime.time += _VERSION_CACHE_MAXAGE 49 # Should fetch version. 50 self._patcher.GetVersion() 51 self.assertEqual(count + 1, self._test_patcher.get_version_count) 52 53 def testGetPatchedFiles(self): 54 # Fill cache. 55 self._patcher.GetPatchedFiles() 56 count = self._test_patcher.get_patched_files_count 57 # Should read from cache. 58 self._patcher.GetPatchedFiles() 59 self.assertEqual(count, self._test_patcher.get_patched_files_count) 60 61 def testApply(self): 62 # Fill cache. 63 self._patcher.Apply(['add.txt'], None).Get() 64 count = self._test_patcher.apply_count 65 # Should read from cache even though it's reading another file. 66 self._patcher.Apply(['modify.txt'], None).Get() 67 self.assertEqual(count, self._test_patcher.apply_count) 68 69if __name__ == '__main__': 70 unittest.main() 71