| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import __builtin__ | 5 import __builtin__ |
| 6 import mock | 6 import mock |
| 7 import os | 7 import os |
| 8 import pickle | 8 import pickle |
| 9 import zlib | 9 import zlib |
| 10 | 10 |
| 11 from testing_utils import testing | 11 from testing_utils import testing |
| 12 | 12 |
| 13 import local_cache | 13 import local_cache |
| 14 | 14 |
| 15 | 15 |
| 16 class LocalCacheTest(testing.AppengineTestCase): | 16 class LocalCacheTest(testing.AppengineTestCase): |
| 17 | 17 |
| 18 def testLocalCacher(self): | 18 def testLocalCache(self): |
| 19 fake_dir = 'fake_dir' | 19 fake_dir = 'fake_dir' |
| 20 cacher = local_cache.LocalCacher(cache_dir=fake_dir) | 20 cacher = local_cache.LocalCache(cache_dir=fake_dir) |
| 21 def _MockPathExists(path, *_): | 21 def _MockPathExists(path, *_): |
| 22 return False if path == os.path.join( | 22 return False if path == os.path.join( |
| 23 fake_dir, 'uncached_key') else True | 23 fake_dir, 'uncached_key') else True |
| 24 self.mock(os.path, 'exists', _MockPathExists) | 24 self.mock(os.path, 'exists', _MockPathExists) |
| 25 | 25 |
| 26 value = 'val' | 26 value = 'val' |
| 27 with mock.patch('__builtin__.open', mock.mock_open( | 27 with mock.patch('__builtin__.open', mock.mock_open( |
| 28 read_data=zlib.compress(pickle.dumps(value)))) as m: | 28 read_data=zlib.compress(pickle.dumps(value)))) as m: |
| 29 cacher.Set('a', 'b') | 29 cacher.Set('a', 'b') |
| 30 m.assert_called_once_with(os.path.join(fake_dir, 'a'), 'wb') | 30 m.assert_called_once_with(os.path.join(fake_dir, 'a'), 'wb') |
| 31 self.assertEqual(value, cacher.Get('key')) | 31 self.assertEqual(value, cacher.Get('key')) |
| 32 self.assertIsNone(cacher.Get('uncached_key')) | 32 self.assertIsNone(cacher.Get('uncached_key')) |
| OLD | NEW |