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