Esempio n. 1
0
    def assert_context_contains(self,
                                template_name,
                                data_key,
                                data_value,
                                expected_context=None):
        """Verify that FormattingLayer converts the `data` elements into a
        context variable similar to `expect_context`. If `expected_context` is
        not provided, assume that it should match `data_value`"""
        if expected_context is None:
            expected_context = dict(data_value)
        template_file = template_loc.format(template_name)
        with patch('regulations.generator.layers.formatting.loader') as ldr:
            # we will want to reference these templates later
            templates = defaultdict(Mock)
            ldr.get_template.side_effect = templates.__getitem__
            fl = FormattingLayer({})
            # materialize
            next(fl.replacements_for('', {data_key: data_value}))
            render = templates[template_file].render

        self.assertTrue(render.called)
        context = render.call_args[0][0]
        for key, value in expected_context.items():
            self.assertTrue(key in context)
            self.assertEqual(context[key], value)
    def test_apply_layer_code(self, loader):
        mocks = {'table': Mock(), 'note': Mock(), 'code': Mock(),
                 'subscript': Mock()}

        def ret_mock(arg):
            for key in mocks:
                if key in arg:
                    return mocks[key]

        loader.get_template.side_effect = ret_mock
        render = mocks['code'].render

        fence_data = {'type': 'python',
                      'lines': ['def double(x):', '    return x + x']}
        data = {'111-1': [],
                '111-2': [{}],
                '111-3': [{'text': 'original', 'locations': [0],
                           'fence_data': fence_data}]}
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['lines'],
                         ['def double(x):', '    return x + x'])
    def test_apply_layer_note(self, loader):
        note_mock = Mock()
        loader.get_template.side_effect = lambda arg: note_mock if "note" in arg else Mock(
        )
        render = note_mock.render

        fence_data = {
            'type': 'note',
            'lines': ['Note:', '1. Content1', '2. Content2']
        }
        data = {
            '111-1': [],
            '111-2': [{}],
            '111-3': [{
                'text': 'original',
                'locations': [0],
                'fence_data': fence_data
            }]
        }
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['lines'], ['1. Content1', '2. Content2'])
    def assert_context_contains(self, template_name, data_key, data_value, expected_context=None):
        """Verify that FormattingLayer converts the `data` elements into a
        context variable similar to `expect_context`. If `expected_context` is
        not provided, assume that it should match `data_value`"""
        if expected_context is None:
            expected_context = dict(data_value)
        data = {"111-3": [{"text": "original", "locations": [0, 2], data_key: data_value}]}
        template_file = "regulations/layers/{}.html".format(template_name)
        with patch("regulations.generator.layers.formatting.loader") as ldr:
            # we will want to reference these templates later
            templates = defaultdict(Mock)
            ldr.get_template.side_effect = templates.__getitem__
            fl = FormattingLayer(data)
            result = fl.apply_layer("111-3")
            render = templates[template_file].render

        self.assertEqual(len(result), 1)
        self.assertEqual("original", result[0][0])
        self.assertEqual([0, 2], result[0][2])

        self.assertTrue(render.called)
        context = render.call_args[0][0]
        for key, value in expected_context.iteritems():
            self.assertTrue(key in context)
            self.assertEqual(context[key], value)
    def test_apply_layer_dash(self, loader):
        dash_mock = Mock()
        loader.get_template.side_effect = lambda arg: dash_mock if "dash" in arg else Mock(
        )
        render = dash_mock.render

        dash_data = {'text': 'This is an fp-dash'}
        data = {
            '111-1': [],
            '111-2': [{}],
            '111-3': [{
                'text': 'This is an fp-dash_____',
                'locations': [0],
                'dash_data': dash_data
            }]
        }
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('This is an fp-dash_____', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['text'], 'This is an fp-dash')
    def test_apply_layer_subscript(self, loader):
        mocks = {'table': Mock(), 'note': Mock(), 'code': Mock(),
                 'subscript': Mock()}

        def ret_mock(arg):
            for key in mocks:
                if key in arg:
                    return mocks[key]

        loader.get_template.side_effect = ret_mock
        render = mocks['subscript'].render

        subscript_data = {'variable': 'abc', 'subscript': '123'}
        data = {'111-1': [],
                '111-2': [{}],
                '111-3': [{'text': 'abc_{123}', 'locations': [0, 1, 2],
                           'subscript_data': subscript_data}]}
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('abc_{123}', result[0][0])
        self.assertEqual([0, 1, 2], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['variable'], 'abc')
        self.assertEqual(context['subscript'], '123')
Esempio n. 7
0
    def assert_context_contains(self, template_name, data_key, data_value,
                                expected_context=None):
        """Verify that FormattingLayer converts the `data` elements into a
        context variable similar to `expect_context`. If `expected_context` is
        not provided, assume that it should match `data_value`"""
        if expected_context is None:
            expected_context = dict(data_value)
        data = {'111-3': [{'text': 'original', 'locations': [0, 2],
                           data_key: data_value}]}
        template_file = template_loc.format(template_name)
        with patch('regulations.generator.layers.formatting.loader') as ldr:
            # we will want to reference these templates later
            templates = defaultdict(Mock)
            ldr.get_template.side_effect = templates.__getitem__
            fl = FormattingLayer(data)
            result = list(fl.apply_layer('111-3'))
            render = templates[template_file].render

        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0, 2], result[0][2])

        self.assertTrue(render.called)
        context = render.call_args[0][0]
        for key, value in expected_context.items():
            self.assertTrue(key in context)
            self.assertEqual(context[key], value)
    def test_apply_layer_code(self, loader):
        code_mock = Mock()
        loader.get_template.side_effect = lambda arg: code_mock if "code" in arg else Mock()
        render = code_mock.render

        fence_data = {'type': 'python',
                      'lines': ['def double(x):', '    return x + x']}
        data = {'111-1': [],
                '111-2': [{}],
                '111-3': [{'text': 'original', 'locations': [0],
                           'fence_data': fence_data}]}
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['lines'],
                         ['def double(x):', '    return x + x'])
    def test_apply_layer_note(self, loader):
        note_mock = Mock()
        loader.get_template.side_effect = lambda arg: note_mock if "note" in arg else Mock()
        render = note_mock.render

        fence_data = {'type': 'note',
                      'lines': ['Note:', '1. Content1', '2. Content2']}
        data = {'111-1': [],
                '111-2': [{}],
                '111-3': [{'text': 'original', 'locations': [0],
                           'fence_data': fence_data}]}
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['lines'],
                         ['1. Content1', '2. Content2'])
    def test_apply_layer_code(self, loader):
        code_mock = Mock()
        loader.get_template.side_effect = lambda arg: code_mock if "code" in arg else Mock(
        )
        render = code_mock.render

        fence_data = {
            'type': 'python',
            'lines': ['def double(x):', '    return x + x']
        }
        data = {
            '111-1': [],
            '111-2': [{}],
            '111-3': [{
                'text': 'original',
                'locations': [0],
                'fence_data': fence_data
            }]
        }
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['lines'],
                         ['def double(x):', '    return x + x'])
    def test_apply_layer_subscript(self, loader):
        subscript_mock = Mock()
        loader.get_template.side_effect = lambda arg: subscript_mock if "subscript" in arg else Mock(
        )
        render = subscript_mock.render

        subscript_data = {'variable': 'abc', 'subscript': '123'}
        data = {
            '111-1': [],
            '111-2': [{}],
            '111-3': [{
                'text': 'abc_{123}',
                'locations': [0, 1, 2],
                'subscript_data': subscript_data
            }]
        }
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('abc_{123}', result[0][0])
        self.assertEqual([0, 1, 2], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['variable'], 'abc')
        self.assertEqual(context['subscript'], '123')
 def test_empty(self):
     """FormattingLayer ignores empty of missing node labels"""
     data = {"111-1": [], "111-2": [{}]}
     with patch("regulations.generator.layers.formatting.loader") as ldr:
         render = ldr.get_template.return_value.render
         fl = FormattingLayer(data)
     self.assertEqual([], fl.apply_layer("111-0"))
     self.assertEqual([], fl.apply_layer("111-1"))
     self.assertEqual([], fl.apply_layer("111-2"))
     self.assertFalse(render.called)
Esempio n. 13
0
 def test_empty(self):
     """FormattingLayer ignores empty of missing node labels"""
     data = {'111-1': [], '111-2': [{'text': ''}]}
     with patch('regulations.generator.layers.formatting.loader') as ldr:
         render = ldr.get_template.return_value.render
         fl = FormattingLayer(data)
     self.assertEqual([], list(fl.apply_layer('111-0')))
     self.assertEqual([], list(fl.apply_layer('111-1')))
     self.assertEqual([], list(fl.apply_layer('111-2')))
     self.assertFalse(render.called)
Esempio n. 14
0
    def test_apply_layer_code(self, loader):
        mocks = {
            'table': Mock(),
            'note': Mock(),
            'code': Mock(),
            'subscript': Mock()
        }

        def ret_mock(arg):
            for key in mocks:
                if key in arg:
                    return mocks[key]

        loader.get_template.side_effect = ret_mock
        render = mocks['code'].render

        fence_data = {
            'type': 'python',
            'lines': ['def double(x):', '    return x + x']
        }
        data = {
            '111-1': [],
            '111-2': [{}],
            '111-3': [{
                'text': 'original',
                'locations': [0],
                'fence_data': fence_data
            }]
        }
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['lines'],
                         ['def double(x):', '    return x + x'])
Esempio n. 15
0
    def test_apply_layer_subscript(self, loader):
        mocks = {
            'table': Mock(),
            'note': Mock(),
            'code': Mock(),
            'subscript': Mock()
        }

        def ret_mock(arg):
            for key in mocks:
                if key in arg:
                    return mocks[key]

        loader.get_template.side_effect = ret_mock
        render = mocks['subscript'].render

        subscript_data = {'variable': 'abc', 'subscript': '123'}
        data = {
            '111-1': [],
            '111-2': [{}],
            '111-3': [{
                'text': 'abc_{123}',
                'locations': [0, 1, 2],
                'subscript_data': subscript_data
            }]
        }
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('abc_{123}', result[0][0])
        self.assertEqual([0, 1, 2], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['variable'], 'abc')
        self.assertEqual(context['subscript'], '123')
    def test_apply_layer(self, loader):
        render = loader.get_template.return_value.render

        table_data = {'header': [[{'colspan': 2, 'rowspan': 1, 'text': 'Title'}]], 
                    'rows': [['cell 11', 'cell 12'], ['cell 21', 'cell 22']]}
        data = {'111-1': [],
                '111-2': [{}], 
                '111-3': [{'text': 'original', 'locations': [0, 2],
                                   'table_data': table_data}]}
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0, 2], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['header'], [[{'colspan': 2, 'rowspan': 1, 'text': 'Title'}]])
    def assert_context_contains(self, template_name, data_key, data_value,
                                expected_context=None):
        """Verify that FormattingLayer converts the `data` elements into a
        context variable similar to `expect_context`. If `expected_context` is
        not provided, assume that it should match `data_value`"""
        if expected_context is None:
            expected_context = dict(data_value)
        template_file = template_loc.format(template_name)
        with patch('regulations.generator.layers.formatting.loader') as ldr:
            # we will want to reference these templates later
            templates = defaultdict(Mock)
            ldr.get_template.side_effect = templates.__getitem__
            fl = FormattingLayer({})
            # materialize
            next(fl.replacements_for('', {data_key: data_value}))
            render = templates[template_file].render

        self.assertTrue(render.called)
        context = render.call_args[0][0]
        for key, value in expected_context.items():
            self.assertTrue(key in context)
            self.assertEqual(context[key], value)
    def test_apply_layer_dash(self, loader):
        dash_mock = Mock()
        loader.get_template.side_effect = lambda arg: dash_mock if "dash" in arg else Mock()
        render = dash_mock.render

        dash_data = {'text': 'This is an fp-dash'}
        data = {'111-1': [],
                '111-2': [{}],
                '111-3': [{'text': 'This is an fp-dash_____', 'locations': [0],
                           'dash_data': dash_data}]}
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('This is an fp-dash_____', result[0][0])
        self.assertEqual([0], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['text'], 'This is an fp-dash')
Esempio n. 19
0
    def test_apply_layer(self, loader):
        render = loader.get_template.return_value.render

        table_data = {
            'header': [[{
                'colspan': 2,
                'rowspan': 1,
                'text': 'Title'
            }]],
            'rows': [['cell 11', 'cell 12'], ['cell 21', 'cell 22']]
        }
        data = {
            '111-1': [],
            '111-2': [{}],
            '111-3': [{
                'text': 'original',
                'locations': [0, 2],
                'table_data': table_data
            }]
        }
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('original', result[0][0])
        self.assertEqual([0, 2], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['header'], [[{
            'colspan': 2,
            'rowspan': 1,
            'text': 'Title'
        }]])
    def test_apply_layer_subscript(self, loader):
        subscript_mock = Mock()
        loader.get_template.side_effect = lambda arg: subscript_mock if "subscript" in arg else Mock()
        render = subscript_mock.render

        subscript_data = {'variable': 'abc', 'subscript': '123'}
        data = {'111-1': [],
                '111-2': [{}],
                '111-3': [{'text': 'abc_{123}', 'locations': [0, 1, 2],
                           'subscript_data': subscript_data}]}
        fl = FormattingLayer(data)
        self.assertEqual([], fl.apply_layer('111-0'))
        self.assertEqual([], fl.apply_layer('111-1'))
        self.assertEqual([], fl.apply_layer('111-2'))
        self.assertFalse(render.called)

        result = fl.apply_layer('111-3')
        self.assertEqual(len(result), 1)
        self.assertEqual('abc_{123}', result[0][0])
        self.assertEqual([0, 1, 2], result[0][2])
        self.assertTrue(render.called)
        context = render.call_args[0][0]
        self.assertEqual(context['variable'], 'abc')
        self.assertEqual(context['subscript'], '123')