def test_new_subpart_added(): amended_label = Amendment('POST', '200-Subpart:B') assert changes.new_subpart_added(amended_label) amended_label = Amendment('PUT', '200-Subpart:B') assert not changes.new_subpart_added(amended_label) amended_label = Amendment('POST', '200-Subpart:B-a-3') assert not changes.new_subpart_added(amended_label)
def test_new_subpart_added(self): amended_label = Amendment('POST', '200-Subpart:B') self.assertTrue(changes.new_subpart_added(amended_label)) amended_label = Amendment('PUT', '200-Subpart:B') self.assertFalse(changes.new_subpart_added(amended_label)) amended_label = Amendment('POST', '200-Subpart:B-a-3') self.assertFalse(changes.new_subpart_added(amended_label))
def test_create_xmlless_changes(): labels_amended = [Amendment('DELETE', '200-?-2-a'), Amendment('MOVE', '200-?-2-b', '200-?-2-c')] notice_changes = changes.NoticeChanges() for amendment in labels_amended: fetch.create_xmlless_change(amendment, notice_changes) delete = notice_changes[None]['200-2-a'][0] move = notice_changes[None]['200-2-b'][0] assert delete == {'action': 'DELETE'} assert move == {'action': 'MOVE', 'destination': ['200', '2', 'c']}
def test_parse_appendix_changes(self, whole): amends = [ Amendment('POST', '111-Appendix:A'), Amendment('POST', '111-Appendix:A'), Amendment('PUT', '111-Appendix:B-12'), Amendment('PUT', '111-Appendix:D'), Amendment('PUT', '111-Appendix:E-12-b') ] build_appendix.parse_appendix_changes(amends, '1234', 'XML') self.assertEqual(3, whole.call_count)
def test_write_encoding(self): writer = FSWriteContent(self.tmpdir, "replace", "it") writer.write(Node("Content")) self.assertEqual(self.read("replace", "it")['text'], 'Content') writer.write(Amendment("action", "label")) self.assertEqual(self.read("replace", "it"), ['action', ['label']]) writer.write(Amendment("action", "label", 'destination')) self.assertEqual(self.read("replace", "it"), ['action', ['label'], ['destination']])
def test_create_xmlless_changes(self): labels_amended = [Amendment('DELETE', '200-?-2-a'), Amendment('MOVE', '200-?-2-b', '200-?-2-c')] notice_changes = changes.NoticeChanges() for amendment in labels_amended: amendments.create_xmlless_change(amendment, notice_changes) delete = notice_changes.changes_by_xml[None]['200-2-a'][0] move = notice_changes.changes_by_xml[None]['200-2-b'][0] self.assertEqual({'action': 'DELETE'}, delete) self.assertEqual({'action': 'MOVE', 'destination': ['200', '2', 'c']}, move)
def test_match_labels_and_changes(): labels_amended = [Amendment('POST', '200-?-2'), Amendment('PUT', '200-?-2-a')] amend_map = changes.match_labels_and_changes( labels_amended, _section_node()) assert len(amend_map) == 2 for amendments in amend_map.values(): amend = amendments[0] assert not amend['candidate'] assert amend['action'] in ('POST', 'PUT')
def test_match_labels_and_changes(self): labels_amended = [ Amendment('POST', '200-?-2'), Amendment('PUT', '200-?-2-a') ] amend_map = changes.match_labels_and_changes(labels_amended, self.section_node()) self.assertEqual(2, len(amend_map.keys())) for label, amendments in amend_map.items(): amend = amendments[0] self.assertFalse(amend['candidate']) self.assertTrue(amend['action'] in ['POST', 'PUT'])
def test_match_labels_and_changes_move(): labels_amended = [Amendment('MOVE', '200-?-1', '200-?-2')] amend_map = changes.match_labels_and_changes(labels_amended, None) assert dict(amend_map) == { '200-1': [{'action': 'MOVE', 'destination': ['200', '2'], 'amdpar_xml': None}] }
def test_create_xml_changes_child_stars(): labels_amended = [Amendment('PUT', '200-?-2-a')] with XMLBuilder("ROOT") as ctx: ctx.P("(a) Content") ctx.STARS() n2a = Node('(a) Content', label=['200', '2', 'a'], source_xml=ctx.xml.xpath('//P')[0]) n2b = Node('(b) Content', label=['200', '2', 'b']) n2 = Node('n2', label=['200', '2'], children=[n2a, n2b]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() fetch.create_xml_changes(labels_amended, root, notice_changes) data = notice_changes[None] assert '200-2-a' in data assert len(data['200-2-a']) == 1 change = data['200-2-a'][0] assert change['action'] == 'PUT' assert 'field' not in change n2a.text = n2a.text + ":" n2a.source_xml.text = n2a.source_xml.text + ":" notice_changes = changes.NoticeChanges() fetch.create_xml_changes(labels_amended, root, notice_changes) data = notice_changes[None] assert '200-2-a' in data assert len(data['200-2-a']) == 1 change = data['200-2-a'][0] assert change['action'] == 'PUT' assert change.get('field') == '[text]'
def test_create_xml_changes_child_stars(self): labels_amended = [Amendment('PUT', '200-2-a')] xml = etree.fromstring("<ROOT><P>(a) Content</P><STARS /></ROOT>") n2a = Node('(a) Content', label=['200', '2', 'a'], source_xml=xml.xpath('//P')[0]) n2b = Node('(b) Content', label=['200', '2', 'b']) n2 = Node('n2', label=['200', '2'], children=[n2a, n2b]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() build.create_xml_changes(labels_amended, root, notice_changes) self.assertTrue('200-2-a' in notice_changes.changes) self.assertTrue(1, len(notice_changes.changes['200-2-a'])) change = notice_changes.changes['200-2-a'][0] self.assertEqual('PUT', change['action']) self.assertFalse('field' in change) n2a.text = n2a.text + ":" n2a.source_xml.text = n2a.source_xml.text + ":" notice_changes = changes.NoticeChanges() build.create_xml_changes(labels_amended, root, notice_changes) self.assertTrue('200-2-a' in notice_changes.changes) self.assertTrue(1, len(notice_changes.changes['200-2-a'])) change = notice_changes.changes['200-2-a'][0] self.assertEqual('PUT', change['action']) self.assertEqual('[text]', change.get('field'))
def test_match_labels_and_changes_candidate(): labels_amended = [ Amendment('POST', '200-?-2'), Amendment('PUT', '200-?-2-a-1-i')] n1 = Node('n2', label=['200', '2']) n2 = Node('n2a', label=['200', '2', 'i']) n1.children = [n2] root = Node('root', label=['200'], children=[n1]) amend_map = changes.match_labels_and_changes( labels_amended, root) assert amend_map['200-2-a-1-i'][0]['candidate'] assert amend_map['200-2-a-1-i'][0]['node'].label_id() == '200-2-a-1-i'
def test_create_xml_changes_child_stars(self): labels_amended = [Amendment('PUT', '200-?-2-a')] with XMLBuilder("ROOT") as ctx: ctx.P("(a) Content") ctx.STARS() n2a = Node('(a) Content', label=['200', '2', 'a'], source_xml=ctx.xml.xpath('//P')[0]) n2b = Node('(b) Content', label=['200', '2', 'b']) n2 = Node('n2', label=['200', '2'], children=[n2a, n2b]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() amendments.create_xml_changes(labels_amended, root, notice_changes) data = notice_changes.changes_by_xml[None] self.assertIn('200-2-a', data) self.assertTrue(1, len(data['200-2-a'])) change = data['200-2-a'][0] self.assertEqual('PUT', change['action']) self.assertNotIn('field', change) n2a.text = n2a.text + ":" n2a.source_xml.text = n2a.source_xml.text + ":" notice_changes = changes.NoticeChanges() amendments.create_xml_changes(labels_amended, root, notice_changes) data = notice_changes.changes_by_xml[None] self.assertIn('200-2-a', data) self.assertTrue(1, len(data['200-2-a'])) change = data['200-2-a'][0] self.assertEqual('PUT', change['action']) self.assertEqual('[text]', change.get('field'))
def test_tree_format_level2(self): for label, expected in (('100', None), ('100-Interpretations', None), ('100-Subpart:A-105', ['100', 'Subpart', 'A']), ('100-Subjgrp:AbCdE', ['100', 'Subjgrp', 'AbCdE']), ('100-Appendix:R', ['100', 'R'])): self.assertEqual( Amendment('VERB', label).tree_format_level2(), expected)
def test_match_labels_and_changes_move(self): labels_amended = [Amendment('MOVE', '200-1', '200-2')] amend_map = changes.match_labels_and_changes(labels_amended, None) self.assertEqual( amend_map, {'200-1': [{ 'action': 'MOVE', 'destination': ['200', '2'] }]})
def test_match_labels_and_changes_reserve(): labels_amended = [Amendment('RESERVE', '200-?-2-a')] amend_map = changes.match_labels_and_changes( labels_amended, _section_node()) assert set(amend_map.keys()) == {'200-2-a'} amendments = amend_map['200-2-a'] assert amendments[0]['action'] == 'RESERVE' assert amendments[0]['node'] == Node('n2a', label=['200', '2', 'a'])
def test_match_labels_and_changes_delete(self): labels_amended = [Amendment('DELETE', '200-?-1-a-i')] amend_map = changes.match_labels_and_changes(labels_amended, None) self.assertEqual( dict(amend_map), {'200-1-a-i': [{ 'action': 'DELETE', 'amdpar_xml': None }]})
def test_process_designate_subpart(self): amended_label = Amendment('MOVE_INTO_SUBPART', '200-?-1-a', '205-Subpart:A') subpart_changes = amendments.process_designate_subpart(amended_label) six.assertCountEqual(self, ['200-1-a'], subpart_changes.keys()) change = subpart_changes['200-1-a'] self.assertEqual(change['destination'], ['205', 'Subpart', 'A']) self.assertEqual(change['action'], 'DESIGNATE')
def test_match_labels_and_changes_reserve(self): labels_amended = [Amendment('RESERVE', '200-?-2-a')] amend_map = changes.match_labels_and_changes(labels_amended, self.section_node()) six.assertCountEqual(self, ['200-2-a'], amend_map.keys()) amendments = amend_map['200-2-a'] self.assertEqual(amendments[0]['action'], 'RESERVE') self.assertEqual(amendments[0]['node'], Node('n2a', label=['200', '2', 'a']))
def test_process_designate_subpart(): amended_label = Amendment('MOVE_INTO_SUBPART', '200-?-1-a', '205-Subpart:A') subpart_changes = subpart.process_designate_subpart(amended_label) assert '200-1-a' == subpart_changes.label_id change = subpart_changes.content assert change['destination'] == ['205', 'Subpart', 'A'] assert change['action'] == 'DESIGNATE'
def test_create_xml_changes_reserve(self): labels_amended = [Amendment('RESERVE', '200-2-a')] n2a = Node('[Reserved]', label=['200', '2', 'a']) n2 = Node('n2', label=['200', '2'], children=[n2a]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() build.create_xml_changes(labels_amended, root, notice_changes) reserve = notice_changes.changes['200-2-a'][0] self.assertEqual(reserve['action'], 'RESERVE') self.assertEqual(reserve['node']['text'], u'[Reserved]')
def test_create_xml_changes_reserve(): labels_amended = [Amendment('RESERVE', '200-?-2-a')] n2a = Node('[Reserved]', label=['200', '2', 'a']) n2 = Node('n2', label=['200', '2'], children=[n2a]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() fetch.create_xml_changes(labels_amended, root, notice_changes) reserve = notice_changes[None]['200-2-a'][0] assert reserve['action'] == 'RESERVE' assert reserve['node']['text'] == '[Reserved]'
def test_process_new_subpart(self): par = self.new_subpart_xml().xpath('//AMDPAR')[1] amended_label = Amendment('POST', '105-Subpart:B') notice = {'cfr_parts': ['105']} subpart_changes = build.process_new_subpart(notice, amended_label, par) new_nodes_added = ['105-Subpart-B', '105-30', '105-30-a'] self.assertItemsEqual(new_nodes_added, subpart_changes.keys()) for l, n in subpart_changes.items(): self.assertEqual(n['action'], 'POST') self.assertEqual(subpart_changes['105-Subpart-B']['node']['node_type'], 'subpart')
def test_fix_label(self): """Fix label converts between the AMDPAR label format and the Node label format""" tests = { '1005-Interpretations': ['1005', 'Interp'], '1005-Interpretations-31-(b)(1)-3': ['1005', '31', 'b', '1', 'Interp', '3'], '1005-Interpretations-31-(b)(1)-3[title]': ['1005', '31', 'b', '1', 'Interp', '3'], '1005-Interpretations-31-(c)-2-xi': ['1005', '31', 'c', 'Interp', '2', 'xi'], '1005-Interpretations-31-()-2-xi': ['1005', '31', 'Interp', '2', 'xi'], '1005-Interpretations-Appendix:A-2': ['1005', 'A', '2', 'Interp'], '1005-Appendix:A-2': ['1005', 'A', '2'], '1005-Subpart:A-2': ['1005', '2'], '1005-Subjgrp:AbCd-2': ['1005', '2'] } for in_label, out_label in tests.items(): amd = Amendment('action', in_label) self.assertEqual(amd.label, out_label)
def test_create_xml_changes_stars_hole(self): labels_amended = [Amendment('PUT', '200-2-a')] n2a1 = Node('(1) * * *', label=['200', '2', 'a', '1']) n2a2 = Node('(2) a2a2a2', label=['200', '2', 'a', '2']) n2a = Node('(a) aaa', label=['200', '2', 'a'], children=[n2a1, n2a2]) n2 = Node('n2', label=['200', '2'], children=[n2a]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() build.create_xml_changes(labels_amended, root, notice_changes) for label in ('200-2-a', '200-2-a-2'): self.assertTrue(label in notice_changes.changes) self.assertEqual(1, len(notice_changes.changes[label])) change = notice_changes.changes[label][0] self.assertEqual('PUT', change['action']) self.assertFalse('field' in change) self.assertTrue('200-2-a-1' in notice_changes.changes) self.assertEqual(1, len(notice_changes.changes['200-2-a-1'])) change = notice_changes.changes['200-2-a-1'][0] self.assertEqual('KEEP', change['action']) self.assertFalse('field' in change)
def test_create_xml_changes_stars(self): labels_amended = [Amendment('PUT', '200-?-2-a')] n2a1 = Node('(1) Content', label=['200', '2', 'a', '1']) n2a2 = Node('(2) Content', label=['200', '2', 'a', '2']) n2a = Node('(a) * * *', label=['200', '2', 'a'], children=[n2a1, n2a2]) n2 = Node('n2', label=['200', '2'], children=[n2a]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() amendments.create_xml_changes(labels_amended, root, notice_changes) data = notice_changes.changes_by_xml[None] for label in ('200-2-a-1', '200-2-a-2'): self.assertIn(label, data) self.assertEqual(1, len(data[label])) change = data[label][0] self.assertEqual('PUT', change['action']) self.assertNotIn('field', change) self.assertTrue('200-2-a' in data) self.assertEqual(1, len(data['200-2-a'])) change = data['200-2-a'][0] self.assertEqual('KEEP', change['action']) self.assertNotIn('field', change)
def test_create_xml_changes_stars_hole(): labels_amended = [Amendment('PUT', '200-?-2-a')] n2a1 = Node('(1) * * *', label=['200', '2', 'a', '1']) n2a2 = Node('(2) a2a2a2', label=['200', '2', 'a', '2']) n2a = Node('(a) aaa', label=['200', '2', 'a'], children=[n2a1, n2a2]) n2 = Node('n2', label=['200', '2'], children=[n2a]) root = Node('root', label=['200'], children=[n2]) notice_changes = changes.NoticeChanges() fetch.create_xml_changes(labels_amended, root, notice_changes) data = notice_changes[None] for label in ('200-2-a', '200-2-a-2'): assert label in data assert len(data[label]) == 1 change = data[label][0] assert change['action'] == 'PUT' assert 'field' not in change assert '200-2-a-1' in data assert len(data['200-2-a-1']) == 1 change = data['200-2-a-1'][0] assert change['action'] == 'KEEP' assert 'field' not in change
def test_tree_format_level2(label, expected): assert Amendment('VERB', label).tree_format_level2() == expected
def test_amendment_heading(): amendment = Amendment('PUT', '100-2-a[heading]') assert amendment.action == 'PUT' assert amendment.label == ['100', '2', 'a'] assert amendment.field == '[heading]'
def test_fix_label(in_label, out_label): """Fix label converts between the AMDPAR label format and the Node label format""" amd = Amendment('action', in_label) assert amd.label == out_label