Home | History | Annotate | Download | only in server2
      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 from test_patcher import TestPatcher
     15 
     16 _TEST_PATCH_VERSION = '1'
     17 _TEST_PATCH_FILES = (['add.txt'], ['del.txt'], ['modify.txt'])
     18 _TEST_PATCH_DATA = {
     19   'add.txt': 'add',
     20   'modify.txt': 'modify',
     21 }
     22 
     23 class FakeDateTime(object):
     24   def __init__(self, time=datetime.now()):
     25     self.time = time
     26 
     27   def now(self):
     28     return self.time
     29 
     30 class CachingRietveldPatcherTest(unittest.TestCase):
     31   def setUp(self):
     32     self._datetime = FakeDateTime()
     33     # CachingRietveldPatcher should always call Apply with binary=True.
     34     self._test_patcher = TestPatcher(_TEST_PATCH_VERSION,
     35                                      _TEST_PATCH_FILES,
     36                                      _TEST_PATCH_DATA,
     37                                      assert_binary=True)
     38     self._patcher = CachingRietveldPatcher(
     39         self._test_patcher,
     40         ObjectStoreCreator(start_empty=False),
     41         self._datetime)
     42 
     43   def testGetVersion(self):
     44     # Invalidate cache.
     45     self._datetime.time += _VERSION_CACHE_MAXAGE
     46     # Fill cache.
     47     self._patcher.GetVersion()
     48     count = self._test_patcher.get_version_count
     49     # Should read from cache.
     50     self._patcher.GetVersion()
     51     self.assertEqual(count, self._test_patcher.get_version_count)
     52     # Invalidate cache.
     53     self._datetime.time += _VERSION_CACHE_MAXAGE
     54     # Should fetch version.
     55     self._patcher.GetVersion()
     56     self.assertEqual(count + 1, self._test_patcher.get_version_count)
     57 
     58   def testGetPatchedFiles(self):
     59     # Fill cache.
     60     self._patcher.GetPatchedFiles()
     61     count = self._test_patcher.get_patched_files_count
     62     # Should read from cache.
     63     self._patcher.GetPatchedFiles()
     64     self.assertEqual(count, self._test_patcher.get_patched_files_count)
     65 
     66   def testApply(self):
     67     # Fill cache.
     68     self._patcher.Apply(['add.txt'], None).Get()
     69     count = self._test_patcher.apply_count
     70     # Should read from cache even though it's reading another file.
     71     self._patcher.Apply(['modify.txt'], None).Get()
     72     self.assertEqual(count, self._test_patcher.apply_count)
     73 
     74     # Make sure RietveldPatcher handles |binary| correctly.
     75     self.assertTrue(isinstance(self._patcher.Apply(['add.txt'], None, True).
     76         Get()['add.txt'], str),
     77         'Expected result is binary. It was text.')
     78     self.assertTrue(isinstance(self._patcher.Apply(['add.txt'], None).
     79         Get()['add.txt'], unicode),
     80         'Expected result is text. It was binary.')
     81 
     82 if __name__ == '__main__':
     83   unittest.main()
     84