| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 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 future import Future |
| 10 from server_instance import ServerInstance | 11 from server_instance import ServerInstance |
| 11 from test_file_system import TestFileSystem | 12 from test_file_system import TestFileSystem |
| 12 from test_util import Server2Path | 13 from test_util import Server2Path |
| 13 | 14 |
| 14 | 15 |
| 15 def _ReadLocalFile(filename): | 16 def _ReadLocalFile(filename): |
| 16 base_path = Server2Path('test_data', 'samples_data_source') | 17 base_path = Server2Path('test_data', 'samples_data_source') |
| 17 with open(os.path.join(base_path, filename), 'r') as f: | 18 with open(os.path.join(base_path, filename), 'r') as f: |
| 18 return f.read() | 19 return f.read() |
| 19 | 20 |
| 20 | 21 |
| 21 class _FakeCache(object): | 22 class _FakeCache(object): |
| 22 def __init__(self, obj): | 23 def __init__(self, obj): |
| 23 self._cache = obj | 24 self._cache = obj |
| 24 | 25 |
| 25 def GetFromFileListing(self, _): | 26 def GetFromFileListing(self, _): |
| 26 getter = lambda: 0 | 27 return Future(value=self._cache) |
| 27 getter.Get = lambda: self._cache | |
| 28 return getter | |
| 29 | 28 |
| 30 | 29 |
| 31 class SamplesModelSourceTest(unittest.TestCase): | 30 class SamplesModelSourceTest(unittest.TestCase): |
| 32 def setUp(self): | 31 def setUp(self): |
| 33 server_instance = ServerInstance.ForTest(file_system=TestFileSystem({})) | 32 server_instance = ServerInstance.ForTest(file_system=TestFileSystem({})) |
| 34 self._samples_model = server_instance.platform_bundle.GetSamplesModel( | 33 self._samples_model = server_instance.platform_bundle.GetSamplesModel( |
| 35 'apps') | 34 'apps') |
| 36 self._samples_model._samples_cache = _FakeCache(json.loads(_ReadLocalFile( | 35 self._samples_model._samples_cache = _FakeCache(json.loads(_ReadLocalFile( |
| 37 'samples.json'))) | 36 'samples.json'))) |
| 38 | 37 |
| 39 def testFilterSamples(self): | 38 def testFilterSamples(self): |
| 40 self.assertEquals(json.loads(_ReadLocalFile('expected.json')), | 39 self.assertEquals(json.loads(_ReadLocalFile('expected.json')), |
| 41 self._samples_model.FilterSamples('bobaloo')) | 40 self._samples_model.FilterSamples('bobaloo').Get()) |
| 42 | 41 |
| 43 if __name__ == '__main__': | 42 if __name__ == '__main__': |
| 44 unittest.main() | 43 unittest.main() |
| OLD | NEW |