| 1 | from pathlib import Path |
| 2 | import unittest |
| 3 | import xml.etree.ElementTree as ET |
| 4 | |
| 5 | ROOT = Path(__file__).resolve().parent.parent |
| 6 | FIXTURE = ROOT / 'corpus/recycle-bin-repair' |
| 7 | |
| 8 | |
| 9 | def tree(node): |
| 10 | out = [] |
| 11 | for child in node: |
| 12 | tag = child.tag.rsplit('}', 1)[-1] |
| 13 | if tag in ('Section', 'SectionGroup', 'Page'): |
| 14 | flags = tuple(sorted(key for key in ('isRecycleBin', 'isDeletedPages', 'isInRecycleBin') |
| 15 | if child.get(key) == 'true')) |
| 16 | out.append((tag, child.get('name'), flags, tree(child))) |
| 17 | return out |
| 18 | |
| 19 | |
| 20 | class RecycleBinRepairTest(unittest.TestCase): |
| 21 | def test_onenote_repairs_nothing_in_the_bin_a_page_delete_repaired(self): |
| 22 | candidates = sorted(path.relative_to(FIXTURE / 'candidate') |
| 23 | for path in (FIXTURE / 'candidate').rglob('*') if path.is_file()) |
| 24 | self.assertEqual(candidates, sorted(path.relative_to(FIXTURE / 'cold/notebook') |
| 25 | for path in (FIXTURE / 'cold/notebook').rglob('*') |
| 26 | if path.is_file())) |
| 27 | for relative in candidates: |
| 28 | self.assertEqual((FIXTURE / 'cold/notebook' / relative).read_bytes(), |
| 29 | (FIXTURE / 'candidate' / relative).read_bytes(), str(relative)) |
| 30 | |
| 31 | def test_the_bin_holds_the_deleted_pages(self): |
| 32 | hierarchy = ET.parse(FIXTURE / 'cold/read/hierarchy.xml').getroot() |
| 33 | self.assertEqual(tree(hierarchy), [ |
| 34 | ('Section', 'New Section 1', (), [('Page', 'Kept', (), [])]), |
| 35 | ('SectionGroup', 'OneNote_RecycleBin', ('isRecycleBin',), [ |
| 36 | ('Section', 'Deleted Pages', ('isDeletedPages', 'isInRecycleBin'), [ |
| 37 | ('Page', 'Kept', ('isInRecycleBin',), []), |
| 38 | ('Page', 'Kept', ('isInRecycleBin',), []), |
| 39 | ]), |
| 40 | ]), |
| 41 | ]) |
| 42 | |
| 43 | |
| 44 | if __name__ == '__main__': |
| 45 | unittest.main() |