| 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 sys | |
| 9 import unittest | 8 import unittest |
| 10 | 9 |
| 11 from samples_data_source import SamplesDataSource | 10 from server_instance import ServerInstance |
| 12 from servlet import Request | 11 from test_file_system import TestFileSystem |
| 13 from test_util import Server2Path | 12 from test_util import Server2Path |
| 14 | 13 |
| 15 | 14 |
| 16 class SamplesDataSourceTest(unittest.TestCase): | 15 def _ReadLocalFile(filename): |
| 16 base_path = Server2Path('test_data', 'samples_data_source') |
| 17 with open(os.path.join(base_path, filename), 'r') as f: |
| 18 return f.read() |
| 19 |
| 20 |
| 21 class _FakeCache(object): |
| 22 def __init__(self, obj): |
| 23 self._cache = obj |
| 24 |
| 25 def GetFromFileListing(self, _): |
| 26 getter = lambda: 0 |
| 27 getter.Get = lambda: self._cache |
| 28 return getter |
| 29 |
| 30 |
| 31 class SamplesModelSourceTest(unittest.TestCase): |
| 17 def setUp(self): | 32 def setUp(self): |
| 18 self._base_path = Server2Path('test_data', 'samples_data_source') | 33 server_instance = ServerInstance.ForTest(file_system=TestFileSystem({})) |
| 19 | 34 self._samples_model = server_instance.platform_bundle.GetSamplesModel( |
| 20 def _ReadLocalFile(self, filename): | 35 'apps') |
| 21 with open(os.path.join(self._base_path, filename), 'r') as f: | 36 self._samples_model._samples_cache = _FakeCache(json.loads(_ReadLocalFile( |
| 22 return f.read() | 37 'samples.json'))) |
| 23 | |
| 24 def _FakeGet(self, key): | |
| 25 return json.loads(self._ReadLocalFile(key)) | |
| 26 | 38 |
| 27 def testFilterSamples(self): | 39 def testFilterSamples(self): |
| 28 sds = SamplesDataSource({}, {}, '.', Request.ForTest('/')) | 40 self.assertEquals(json.loads(_ReadLocalFile('expected.json')), |
| 29 sds.get = self._FakeGet | 41 self._samples_model.FilterSamples('bobaloo')) |
| 30 self.assertEquals(json.loads(self._ReadLocalFile('expected.json')), | |
| 31 sds.FilterSamples('samples.json', 'bobaloo')) | |
| 32 | 42 |
| 33 if __name__ == '__main__': | 43 if __name__ == '__main__': |
| 34 unittest.main() | 44 unittest.main() |
| OLD | NEW |