| 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 |   8 import sys | 
|   9 import unittest |   9 import unittest | 
|  10  |  10  | 
|  11 from samples_data_source import SamplesDataSource, _MakeAPILink |  11 from samples_data_source import SamplesDataSource | 
 |  12  | 
 |  13 class _FakeRequest(object): | 
 |  14   pass | 
|  12  |  15  | 
|  13 class SamplesDataSourceTest(unittest.TestCase): |  16 class SamplesDataSourceTest(unittest.TestCase): | 
|  14   def setUp(self): |  17   def setUp(self): | 
|  15     self._base_path = os.path.join(sys.path[0], |  18     self._base_path = os.path.join(sys.path[0], | 
|  16                                    'test_data', |  19                                    'test_data', | 
|  17                                    'samples_data_source') |  20                                    'samples_data_source') | 
|  18  |  21  | 
|  19   def _ReadLocalFile(self, filename): |  22   def _ReadLocalFile(self, filename): | 
|  20     with open(os.path.join(self._base_path, filename), 'r') as f: |  23     with open(os.path.join(self._base_path, filename), 'r') as f: | 
|  21       return f.read() |  24       return f.read() | 
|  22  |  25  | 
|  23   def _FakeGet(self, key): |  26   def _FakeGet(self, key): | 
|  24     return json.loads(self._ReadLocalFile(key)) |  27     return json.loads(self._ReadLocalFile(key)) | 
|  25  |  28  | 
|  26   def testFilterSamples(self): |  29   def testFilterSamples(self): | 
|  27     sds = SamplesDataSource({}, {}, 'fake_path', None) |  30     sds = SamplesDataSource({}, {}, 'fake_path', _FakeRequest()) | 
|  28     sds.get = self._FakeGet |  31     sds.get = self._FakeGet | 
|  29     self.assertEquals(json.loads(self._ReadLocalFile('expected.json')), |  32     self.assertEquals(json.loads(self._ReadLocalFile('expected.json')), | 
|  30                       sds.FilterSamples('samples.json', 'bobaloo')) |  33                       sds.FilterSamples('samples.json', 'bobaloo')) | 
|  31  |  34  | 
|  32   def testMakeAPILink(self): |  | 
|  33     api_list = [ |  | 
|  34       'foo', |  | 
|  35       'bar', |  | 
|  36       'baz', |  | 
|  37       'jim.bob', |  | 
|  38       'jim.bif', |  | 
|  39       'joe.bob.bif' |  | 
|  40     ] |  | 
|  41     self.assertEquals('foo.html#type-baz', |  | 
|  42                       _MakeAPILink('type', 'chrome.foo.baz', api_list)) |  | 
|  43     self.assertEquals('jim.bob.html#type-joe', |  | 
|  44                       _MakeAPILink('type', 'chrome.jim.bob.joe', api_list)) |  | 
|  45     self.assertEquals('joe.bob.bif.html#event-lenny', |  | 
|  46                       _MakeAPILink('event', |  | 
|  47                                    'chrome.joe.bob.bif.lenny', |  | 
|  48                                    api_list)) |  | 
|  49     self.assertEquals('baz.html#floop-lox', |  | 
|  50                       _MakeAPILink('floop', 'chrome.baz.lox', api_list)) |  | 
|  51     self.assertEquals(None, _MakeAPILink('type', 'chrome.jim.foo', api_list)) |  | 
|  52     self.assertEquals(None, _MakeAPILink('type', 'chrome.joe.bob', api_list)) |  | 
|  53     self.assertEquals(None, _MakeAPILink('type', 'chrome.barn.foo', api_list)) |  | 
|  54  |  | 
|  55 if __name__ == '__main__': |  35 if __name__ == '__main__': | 
|  56   unittest.main() |  36   unittest.main() | 
| OLD | NEW |