| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import json |
| 6 import os | 7 import os |
| 7 import unittest | 8 import unittest |
| 8 | 9 |
| 9 from fake_url_fetcher import FakeUrlFetcher | 10 from appengine_blobstore import AppEngineBlobstore |
| 11 from appengine_url_fetcher import AppEngineUrlFetcher |
| 12 from appengine_wrappers import files |
| 13 from fake_fetchers import ConfigureFakeFetchers |
| 10 from github_file_system import GithubFileSystem | 14 from github_file_system import GithubFileSystem |
| 11 from in_memory_object_store import InMemoryObjectStore | 15 from in_memory_object_store import InMemoryObjectStore |
| 12 | 16 import url_constants |
| 13 class FakeBlobstore(object): | |
| 14 def Set(self, blob, key, version): | |
| 15 return None | |
| 16 | |
| 17 def Get(self, key, version): | |
| 18 return None | |
| 19 | |
| 20 def Delete(self, key, version): | |
| 21 return None | |
| 22 | |
| 23 class FakeGithubFetcher(FakeUrlFetcher): | |
| 24 class _Response(object): | |
| 25 def __init__(self, content): | |
| 26 self.content = content | |
| 27 | |
| 28 def Fetch(self, path): | |
| 29 if path == 'zipball': | |
| 30 return super(FakeGithubFetcher, self).Fetch('file_system.zip') | |
| 31 return self._Response('{ "commit": { "tree": { "sha": 0 } } }') | |
| 32 | 17 |
| 33 class GithubFileSystemTest(unittest.TestCase): | 18 class GithubFileSystemTest(unittest.TestCase): |
| 34 def setUp(self): | 19 def setUp(self): |
| 35 self._file_system = GithubFileSystem(FakeGithubFetcher('test_data'), | 20 ConfigureFakeFetchers() |
| 36 InMemoryObjectStore('test'), | 21 self._base_path = os.path.join('test_data', 'github_file_system') |
| 37 FakeBlobstore()) | 22 self._file_system = GithubFileSystem( |
| 23 AppEngineUrlFetcher(url_constants.GITHUB_URL), |
| 24 InMemoryObjectStore('github'), |
| 25 AppEngineBlobstore()) |
| 38 | 26 |
| 39 def testReadFiles(self): | 27 def _ReadLocalFile(self, filename): |
| 40 expected = { | 28 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 41 '/test1.txt': 'test1\n', | 29 return f.read() |
| 42 '/test2.txt': 'test2\n', | |
| 43 '/test3.txt': 'test3\n', | |
| 44 } | |
| 45 self.assertEqual( | |
| 46 expected, | |
| 47 self._file_system.Read( | |
| 48 ['/test1.txt', '/test2.txt', '/test3.txt']).Get()) | |
| 49 | 30 |
| 50 def testListDir(self): | 31 def testList(self): |
| 51 expected = ['dir/'] | 32 self.assertEqual(json.loads(self._ReadLocalFile('expected_list.json')), |
| 52 for i in range(7): | 33 self._file_system.Read(['/']).Get()) |
| 53 expected.append('file%d.html' % i) | 34 |
| 54 self.assertEqual(expected, | 35 def testRead(self): |
| 55 sorted(self._file_system.ReadSingle('/list/'))) | 36 self.assertEqual(self._ReadLocalFile('expected_read.txt'), |
| 37 self._file_system.ReadSingle('/analytics/launch.js')) |
| 38 |
| 39 def testStat(self): |
| 40 self.assertEqual(0, self._file_system.Stat('zipball').version) |
| 41 |
| 42 def testKeyGeneration(self): |
| 43 self.assertEqual(0, len(files.GetBlobKeys())) |
| 44 self._file_system.ReadSingle('/analytics/launch.js') |
| 45 self.assertEqual(1, len(files.GetBlobKeys())) |
| 46 self._file_system.ReadSingle('/analytics/main.css') |
| 47 self.assertEqual(1, len(files.GetBlobKeys())) |
| 56 | 48 |
| 57 if __name__ == '__main__': | 49 if __name__ == '__main__': |
| 58 unittest.main() | 50 unittest.main() |
| OLD | NEW |