def test_process_node_appliers(self): node = { "text": "Text text text.", "children": [], "label": ["123", "aaa"], 'node_type': REGTEXT } inline = Mock() inline.get_layer_pairs.return_value = [] par = Mock() par.apply_layers.return_value = node sr = Mock() sr.get_layer_pairs.return_value = [] builder = HTMLBuilder(inline, par, sr) builder.process_node(node) self.assertTrue(inline.get_layer_pairs.called) self.assertEqual("123-aaa", inline.get_layer_pairs.call_args[0][0]) self.assertEqual("Text text text.", inline.get_layer_pairs.call_args[0][1]) self.assertTrue(par.apply_layers.called) self.assertEqual(node, par.apply_layers.call_args[0][0])
def test_list_level_regulations_no_level(self): builder = HTMLBuilder(None, None, None) parts = ['101', '1'] node_type = REGTEXT result = builder.list_level(parts, node_type) self.assertEquals(result, None)
def generate_html_tree(subtree): """Use the HTMLBuilder to generate a version of this subtree with appropriate markup. Currently, includes no layers""" builder = HTMLBuilder(*LayerCreator().get_appliers()) builder.tree = subtree builder.generate_html() return {'node': builder.tree, 'markup_page_type': 'reg-section'}
def test_node_title_no_diff(self): builder = HTMLBuilder() node = { "label_id": "204", "title": "abcd", 'node_type': APPENDIX } builder.process_node_title(node) self.assertTrue('header' in node) self.assertEqual(node['title'], 'abcd')
def test_is_collapsed(self): for label, text in ((['111', '22', 'a'], '(a) '), (['111', '22', 'xx'], ' (xx) '), (['111', '22', 'a', '5'], '(5)')): node = {'label': label, 'text': text} self.assertTrue(HTMLBuilder.is_collapsed(node)) for label, text in ((['111', '22', 'a'], '(b) '), (['111', '22', ''], '(a) Some text'), (['111', '22', 'a'], ' ')): node = {'label': label, 'text': text} self.assertFalse(HTMLBuilder.is_collapsed(node))
def test_process_node_title_diff(self): builder = HTMLBuilder() diff = {'204': {'title': [('delete', 0, 2), ('insert', 4, 'AAC')], 'text': [('delete', 0, 2), ('insert', 4, 'AAB')], 'op': ''}} da = diff_applier.DiffApplier(diff, None) node = { "label_id": "204", "title": "abcd", 'node_type': APPENDIX } builder.diff_applier = da builder.process_node_title(node) self.assertEqual('<del>ab</del>cd<ins>AAC</ins>', node['header'])
def test_process_node_title_section_space_diff(self): """" Diffs and sections spaces need to place nicely together. """ builder = HTMLBuilder(None, None, None) diff = {'204': {'title': [('delete', 7, 9), ('insert', 10, 'AAC')], 'text': [('delete', 0, 2), ('insert', 4, 'AAB')], 'op': ''}} da = diff_applier.DiffApplier(diff, None) node = { "label_id": u"204", "title": u"§ 101.6 abcd", 'node_type': APPENDIX } builder.diff_applier = da builder.process_node_title(node) self.assertEqual( u'§ 101.6<del> a</del>b<ins>AAC</ins>cd', node['header'])
def test_list_level_interpretations(self): builder = HTMLBuilder(None, None, None) parts = ['101', '12', 'a', 'Interp', '1'] node_type = INTERP result = builder.list_level(parts, node_type) self.assertEquals(result, 1) parts.append('j') result = builder.list_level(parts, node_type) self.assertEquals(result, 2) parts.append('B') result = builder.list_level(parts, node_type) self.assertEquals(result, 3)
def test_interp_node_with_citations(self): inline, p, sr = Mock(), Mock(), Mock() builder = HTMLBuilder(inline, p, sr) node = { 'text': 'Interpretation with a link', 'children': [], 'node_type': INTERP, 'label': ['999', '5', 'Interp'] } p.apply_layers.return_value = node inline.get_layer_pairs.return_value = [] sr.get_layer_pairs.return_value = [] builder.process_node(node) layer_parameters = inline.get_layer_pairs.call_args[0] self.assertEqual('Interpretation with a link', layer_parameters[1]) self.assertEqual('999-5-Interp', layer_parameters[0])
def test_header_parsing(self): builder = HTMLBuilder(None, None, None) node = { "label": ["234", "a", "1"], "title": "Title (Regulation R)", 'node_type': APPENDIX } titleless_node = { "title": "Title", 'node_type': REGTEXT } parsed_title = builder.parse_doc_title(node['title']) no_title = builder.parse_doc_title(titleless_node['title']) self.assertEqual("(Regulation R)", parsed_title) self.assertEqual(no_title, None)
def get_context_data(self, **kwargs): # We don't want to run the content data of PartialView -- it assumes # we will be applying layers context = super(PartialView, self).get_context_data(**kwargs) label_id = context['label_id'] older = context['version'] newer = context['newer_version'] tree = generator.get_tree_paragraph(label_id, older) if tree is None: #TODO We need a more complicated check here to see if the diffs #add the requested section. If not -> 404 tree = {} appliers = get_appliers(label_id, older, newer) builder = HTMLBuilder(*appliers) builder.tree = tree if not builder.tree: return error_handling.handle_generic_404(self.request) builder.generate_html() child_of_root = builder.tree if builder.tree['node_type'] == REGTEXT: child_of_root = { 'node_type': EMPTYPART, 'children': [builder.tree]} context['tree'] = {'children': [child_of_root]} context['markup_page_type'] = 'diff' regpart = label_id.split('-')[0] old_toc = fetch_toc(regpart, older) diff = generator.get_diff_json(regpart, older, newer) from_version = self.request.GET.get('from_version', older) context['TOC'] = diff_toc(older, newer, old_toc, diff, from_version) context['navigation'] = self.footer_nav(label_id, context['TOC'], older, newer, from_version) return context
def test_modify_interp_node(self): node = { 'node_type': INTERP, 'label': ['872', '22', 'Interp'], 'children': [{'label': ['872', '22', 'Interp', '1']}, {'label': ['872', '22', 'a', 'Interp']}, {'label': ['872', '22', 'b', 'Interp']}] } builder = HTMLBuilder(None, None, None) builder.modify_interp_node(node) self.assertTrue(node['section_header']) self.assertEqual(node['header_children'], [{'label': ['872', '22', 'a', 'Interp']}, {'label': ['872', '22', 'b', 'Interp']}]) self.assertEqual(node['par_children'], [{'label': ['872', '22', 'Interp', '1']}]) node['label'] = ['872', '222', 'a', 'Interp'] builder.modify_interp_node(node) self.assertFalse(node['section_header'])
def test_list_level_appendices(self): builder = HTMLBuilder(None, None, None) parts = ['101', 'A', '1', 'a'] node_type = APPENDIX result = builder.list_level(parts, node_type) self.assertEquals(result, 1) parts.append('2') result = builder.list_level(parts, node_type) self.assertEquals(result, 2) parts.append('k') result = builder.list_level(parts, node_type) self.assertEquals(result, 3) parts.append('B') result = builder.list_level(parts, node_type) self.assertEquals(result, 4)
def test_process_node_title_section_space_diff(self): """" Diffs and sections spaces need to place nicely together. """ builder = HTMLBuilder(None, None, None) diff = { '204': { 'title': [('delete', 7, 9), ('insert', 10, 'AAC')], 'text': [('delete', 0, 2), ('insert', 4, 'AAB')], 'op': '' } } da = diff_applier.DiffApplier(diff, None) node = { "label_id": u"204", "title": u"§ 101.6 abcd", 'node_type': APPENDIX } builder.diff_applier = da builder.process_node_title(node) self.assertEqual(u'§ 101.6<del> a</del>b<ins>AAC</ins>cd', node['header'])
def test_list_level_regulations(self): builder = HTMLBuilder(None, None, None) parts = ['101', '1', 'a'] node_type = REGTEXT result = builder.list_level(parts, node_type) self.assertEquals(result, 1) parts.append('2') result = builder.list_level(parts, node_type) self.assertEquals(result, 2) parts.append('k') result = builder.list_level(parts, node_type) self.assertEquals(result, 3) parts.append('B') result = builder.list_level(parts, node_type) self.assertEquals(result, 4)
def test_modify_interp_node_header(self): node = { 'children': [], 'header': 'This interprets 22(a), a paragraph', 'label': ['872', '22', 'a', 'Interp'], 'node_type': INTERP, } icl = InternalCitationLayer(None) icl.sectional = True ila = InlineLayersApplier() ila.add_layer(icl) builder = HTMLBuilder(ila, None, None) builder.modify_interp_node(node) self.assertEqual('This interprets ' + icl.render_url(['872', '22', 'a'], '22(a)') + ', a paragraph', node['header_markup']) node['label'] = ['872', '22'] builder.modify_interp_node(node) self.assertEqual(node['header'], node['header_markup'])
def test_modify_interp_node(self): node = { 'node_type': INTERP, 'label': ['872', '22', 'Interp'], 'children': [{ 'label': ['872', '22', 'Interp', '1'] }, { 'label': ['872', '22', 'a', 'Interp'] }, { 'label': ['872', '22', 'b', 'Interp'] }] } builder = HTMLBuilder(None, None, None) builder.modify_interp_node(node) self.assertTrue(node['section_header']) self.assertEqual(node['header_children'], [{ 'label': ['872', '22', 'a', 'Interp'] }, { 'label': ['872', '22', 'b', 'Interp'] }]) self.assertEqual(node['par_children'], [{ 'label': ['872', '22', 'Interp', '1'] }]) node['label'] = ['872', '222', 'a', 'Interp'] builder.modify_interp_node(node) self.assertFalse(node['section_header'])
def get_context_data(self, **kwargs): # We don't want to run the content data of PartialView -- it assumes # we will be applying layers context = super(PartialView, self).get_context_data(**kwargs) label_id = context['label_id'] older = context['version'] newer = context['newer_version'] tree = generator.get_tree_paragraph(label_id, older) if tree is None: #TODO We need a more complicated check here to see if the diffs #add the requested section. If not -> 404 tree = {} appliers = get_appliers(label_id, older, newer) builder = HTMLBuilder(*appliers) builder.tree = tree builder.generate_html() child_of_root = builder.tree if builder.tree['node_type'] == REGTEXT: child_of_root = { 'node_type': EMPTYPART, 'children': [builder.tree] } context['tree'] = {'children': [child_of_root]} context['markup_page_type'] = 'diff' regpart = label_id.split('-')[0] old_toc = fetch_toc(regpart, older) diff = generator.get_diff_json(regpart, older, newer) from_version = self.request.GET.get('from_version', older) context['TOC'] = diff_toc(older, newer, old_toc, diff, from_version) context['navigation'] = self.footer_nav(label_id, context['TOC'], older, newer, from_version) return context
def test_process_node_header(self): builder = HTMLBuilder() node = {'text': '', 'children': [], 'label': ['99', '22'], 'node_type': REGTEXT} builder.process_node(node) self.assertFalse('header' in node) node = {'text': '', 'children': [], 'label': ['99', '22'], 'title': 'Some Title', 'node_type': REGTEXT} builder.process_node(node) self.assertTrue('header' in node) self.assertEqual('Some Title', node['header']) node = {'text': '', 'children': [], 'label': ['99', '22'], 'title': u'§ 22.1 Title', 'node_type': REGTEXT} builder.process_node(node) self.assertTrue('header' in node)
def test_generate_html(self): exex = Mock(spec=[]) # no attributes p_applier = Mock() p_applier.layers = {'exex': exex} p_applier.apply_layers.side_effect = lambda n: n # identity builder = HTMLBuilder(None, p_applier, None) builder.tree = {'label': ['1234'], 'children': [], 'node_type': 'regtext', 'text': ''} builder.generate_html() # No explosion so far means this works for most layers exex = Mock() # "includes" any attribute p_applier = Mock() p_applier.layers = {'exex': exex} p_applier.apply_layers.side_effect = lambda n: n # identity builder = HTMLBuilder(None, p_applier, None) builder.tree = {'label': ['1234'], 'children': [], 'node_type': 'regtext', 'text': ''} builder.generate_html() self.assertTrue(exex.preprocess_root.called) self.assertEqual(exex.preprocess_root.call_args[0][0], builder.tree)
def test_modify_interp_node_header(self): node = { 'children': [], 'header': 'This interprets 22(a), a paragraph', 'label': ['872', '22', 'a', 'Interp'], 'node_type': INTERP, } icl = InternalCitationLayer(None) icl.sectional = True ila = InlineLayersApplier() ila.add_layer(icl) builder = HTMLBuilder(ila, None, None) builder.modify_interp_node(node) self.assertEqual( 'This interprets ' + icl.render_url(['872', '22', 'a'], '22(a)') + ', a paragraph', node['header_markup']) node['label'] = ['872', '22'] builder.modify_interp_node(node) self.assertEqual(node['header'], node['header_markup'])
def test_human_label(self): self.assertEqual( '111', HTMLBuilder.human_label({'label': ['111']})) self.assertEqual( '111-22-33-aa', HTMLBuilder.human_label({'label': ['111', '22', '33', 'aa']}))
def generate_html(regulation_tree, layer_appliers): builder = HTMLBuilder(*layer_appliers) builder.tree = regulation_tree builder.generate_html() return builder
def test_no_section_sign(self): text = HTMLBuilder.section_space(' abc') self.assertEquals(text, ' abc') self.assertTrue(True)
def test_human_label(self): self.assertEqual('111', HTMLBuilder.human_label({'label': ['111']})) self.assertEqual( '111-22-33-aa', HTMLBuilder.human_label({'label': ['111', '22', '33', 'aa']}))
def test_node_title_no_diff(self): builder = HTMLBuilder(None, None, None) node = {"label_id": "204", "title": "abcd", 'node_type': APPENDIX} builder.process_node_title(node) self.assertTrue('header' in node) self.assertEqual(node['title'], 'abcd')
def get_builder(regulation, version, inline_applier, p_applier, s_applier): """ Returns an HTML builder with the appliers, and the regulation tree. """ builder = HTMLBuilder(inline_applier, p_applier, s_applier) builder.tree = get_regulation(regulation, version) return builder