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 json |
7 import os | 7 import os |
8 import unittest | 8 import unittest |
9 | 9 |
| 10 from api_data_source import APIDataSource |
| 11 from in_memory_object_store import InMemoryObjectStore |
10 from file_system import FileNotFoundError | 12 from file_system import FileNotFoundError |
11 from file_system_cache import FileSystemCache | 13 from file_system_cache import FileSystemCache |
12 from local_file_system import LocalFileSystem | 14 from local_file_system import LocalFileSystem |
13 from api_data_source import APIDataSource | |
14 | 15 |
15 class FakeSamplesDataSource: | 16 class FakeSamplesDataSource: |
16 def Create(self, request): | 17 def Create(self, request): |
17 return {} | 18 return {} |
18 | 19 |
19 class APIDataSourceTest(unittest.TestCase): | 20 class APIDataSourceTest(unittest.TestCase): |
20 def setUp(self): | 21 def setUp(self): |
21 self._base_path = os.path.join('test_data', 'test_json') | 22 self._base_path = os.path.join('test_data', 'test_json') |
22 | 23 |
23 def _ReadLocalFile(self, filename): | 24 def _ReadLocalFile(self, filename): |
24 with open(os.path.join(self._base_path, filename), 'r') as f: | 25 with open(os.path.join(self._base_path, filename), 'r') as f: |
25 return f.read() | 26 return f.read() |
26 | 27 |
27 def testSimple(self): | 28 def testSimple(self): |
28 cache_builder = FileSystemCache.Builder(LocalFileSystem(self._base_path)) | 29 cache_builder = FileSystemCache.Builder(LocalFileSystem(self._base_path), |
| 30 InMemoryObjectStore('fake_branch')) |
29 data_source_factory = APIDataSource.Factory(cache_builder, | 31 data_source_factory = APIDataSource.Factory(cache_builder, |
30 '.', | 32 '.', |
31 FakeSamplesDataSource()) | 33 FakeSamplesDataSource()) |
32 data_source = data_source_factory.Create({}) | 34 data_source = data_source_factory.Create({}) |
33 | 35 |
34 # Take the dict out of the list. | 36 # Take the dict out of the list. |
35 expected = json.loads(self._ReadLocalFile('expected_test_file.json')) | 37 expected = json.loads(self._ReadLocalFile('expected_test_file.json')) |
36 expected['permissions'] = None | 38 expected['permissions'] = None |
37 test1 = data_source['test_file'] | 39 test1 = data_source['test_file'] |
38 test1.pop('samples') | 40 test1.pop('samples') |
39 self.assertEqual(expected, test1) | 41 self.assertEqual(expected, test1) |
40 test2 = data_source['testFile'] | 42 test2 = data_source['testFile'] |
41 test2.pop('samples') | 43 test2.pop('samples') |
42 self.assertEqual(expected, test2) | 44 self.assertEqual(expected, test2) |
43 test3 = data_source['testFile.html'] | 45 test3 = data_source['testFile.html'] |
44 test3.pop('samples') | 46 test3.pop('samples') |
45 self.assertEqual(expected, test3) | 47 self.assertEqual(expected, test3) |
46 self.assertRaises(FileNotFoundError, data_source.get, 'junk') | 48 self.assertRaises(FileNotFoundError, data_source.get, 'junk') |
47 | 49 |
48 if __name__ == '__main__': | 50 if __name__ == '__main__': |
49 unittest.main() | 51 unittest.main() |
OLD | NEW |