summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/template_data_source_test.py
blob: 423acce679b41ed60226a44cfb05097b03a6c0a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import unittest

from extensions_paths import SERVER2
from server_instance import ServerInstance
from template_data_source import TemplateDataSource
from test_util import DisableLogging, ReadFile
from third_party.handlebar import Handlebar

def _ReadFile(*path):
  return ReadFile(SERVER2, 'test_data', 'template_data_source', *path)

def _CreateTestDataSource(base_dir):
  '''TemplateDataSource is not instantiated directly, rather, its methods
  are invoked through a subclass of it, which has as its only data the
  directory in which TemplateDataSource methods should act on. Thus, we test
  TemplateDataSource indirectly through the TestDataSource class
  '''
  return TestDataSource(ServerInstance.ForLocal(),
                        '%stest_data/template_data_source/%s/' %
                        (SERVER2, base_dir))


class TestDataSource(TemplateDataSource):
  '''Provides a subclass we can use to test the TemplateDataSource methods
  '''
  def __init__(self, server_instance, base_dir):
    type(self)._BASE = base_dir
    TemplateDataSource.__init__(self, server_instance)


class TemplateDataSourceTest(unittest.TestCase):

  def testSimple(self):
    test_data_source = _CreateTestDataSource('simple')
    template_a1 = Handlebar(_ReadFile('simple', 'test1.html'))
    context = [{}, {'templates': {}}]
    self.assertEqual(
        template_a1.Render(*context).text,
        test_data_source.get('test1').Render(*context).text)
    template_a2 = Handlebar(_ReadFile('simple', 'test2.html'))
    self.assertEqual(
        template_a2.Render(*context).text,
        test_data_source.get('test2').Render(*context).text)

  @DisableLogging('warning')
  def testNotFound(self):
    test_data_source = _CreateTestDataSource('simple')
    self.assertEqual(None, test_data_source.get('junk'))

  @DisableLogging('warning')
  def testPartials(self):
    test_data_source = _CreateTestDataSource('partials')
    context = json.loads(_ReadFile('partials', 'input.json'))
    self.assertEqual(
        _ReadFile('partials', 'test_expected.html'),
        test_data_source.get('test_tmpl').Render(
            context, test_data_source).text)


if __name__ == '__main__':
  unittest.main()