| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for docutils odtwriter. |
| 9 |
|
| 10 |
Instructions for adding a new test: |
| 11 |
|
| 12 |
1. Add a new method to class DocutilsOdtTestCase (below) named |
| 13 |
test_odt_xxxx, where xxxx describes your new feature. See |
| 14 |
test_odt_basic for an example. |
| 15 |
|
| 16 |
2. Add a new input reST (.rst) file in test/functional/input. This |
| 17 |
file should contain the smallest amount of reST that tests your |
| 18 |
new feature. Name this file odt_xxxx.rst. |
| 19 |
|
| 20 |
3. Convert your input reST (.rst) file to an ODF (.odt) file using |
| 21 |
rst2odt.py. Place this ODF (.odt) file in |
| 22 |
test/functional/expected. Name this file odt_xxxx.odt. |
| 23 |
You can also pass parameter save_output_name='filename' to method |
| 24 |
process_test() in order to produce expected output. |
| 25 |
See and modify variable TEMP_FILE_PATH for destination. |
| 26 |
|
| 27 |
4. Run your test. Your new test should pass. |
| 28 |
|
| 29 |
5. If any other tests fail, that's a possible regression. |
| 30 |
|
| 31 |
""" |
| 32 |
|
| 33 |
from io import BytesIO |
| 34 |
from pathlib import Path |
| 35 |
import os |
| 36 |
import sys |
| 37 |
import unittest |
| 38 |
import xml.etree.ElementTree as ET |
| 39 |
import zipfile |
| 40 |
|
| 41 |
if __name__ == '__main__': |
| 42 |
|
| 43 |
|
| 44 |
sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 45 |
|
| 46 |
import docutils |
| 47 |
import docutils.core |
| 48 |
from docutils.writers import odf_odt |
| 49 |
|
| 50 |
|
| 51 |
FUNCTIONAL_ROOT = os.path.abspath(os.path.join(__file__, '..', '..', 'functional')) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
TEMP_FILE_PATH = os.path.join(FUNCTIONAL_ROOT, 'output') |
| 56 |
INPUT_PATH = os.path.join(FUNCTIONAL_ROOT, 'input') |
| 57 |
EXPECTED_PATH = os.path.join(FUNCTIONAL_ROOT, 'expected') |
| 58 |
|
| 59 |
|
| 60 |
class DocutilsOdtTestCase(unittest.TestCase): |
| 61 |
|
| 62 |
def process_test(self, input_filename, expected_filename, |
| 63 |
save_output_name=None, settings_overrides=None): |
| 64 |
|
| 65 |
|
| 66 |
input_path = os.path.join(INPUT_PATH, input_filename) |
| 67 |
input_file = open(input_path, 'rb') |
| 68 |
expected_file = open(os.path.join(EXPECTED_PATH, expected_filename), 'rb') |
| 69 |
source = input_file.read() |
| 70 |
expected = expected_file.read() |
| 71 |
input_file.close() |
| 72 |
expected_file.close() |
| 73 |
if settings_overrides is None: |
| 74 |
settings_overrides = {} |
| 75 |
settings_overrides['_disable_config'] = True |
| 76 |
settings_overrides['language_code'] = 'en-US' |
| 77 |
|
| 78 |
result = docutils.core.publish_string( |
| 79 |
source=source, |
| 80 |
source_path=input_path, |
| 81 |
writer=odf_odt.Writer(), |
| 82 |
settings_overrides=settings_overrides) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if save_output_name: |
| 87 |
filename = os.path.join(TEMP_FILE_PATH, save_output_name) |
| 88 |
outfile = open(filename, 'wb') |
| 89 |
outfile.write(result) |
| 90 |
outfile.close() |
| 91 |
content1 = self.extract_file(result, 'content.xml') |
| 92 |
content2 = self.extract_file(expected, 'content.xml') |
| 93 |
msg = 'content.xml not equal: expected len: %d actual len: %d' % ( |
| 94 |
len(content2), len(content1), ) |
| 95 |
self.assertEqual(content1, content2, msg) |
| 96 |
|
| 97 |
def reorder_attributes(self, root): |
| 98 |
""" |
| 99 |
Make attribute order independent of python version. |
| 100 |
python3.8 is different to previous. |
| 101 |
""" |
| 102 |
for el in root.iter(): |
| 103 |
attrib = el.attrib |
| 104 |
if len(attrib) > 1: |
| 105 |
|
| 106 |
attribs = sorted(attrib.items()) |
| 107 |
attrib.clear() |
| 108 |
attrib.update(attribs) |
| 109 |
|
| 110 |
def extract_file(self, payload, filename): |
| 111 |
payloadfile = BytesIO() |
| 112 |
payloadfile.write(payload) |
| 113 |
payloadfile.seek(0) |
| 114 |
zfile = zipfile.ZipFile(payloadfile, 'r') |
| 115 |
content1 = zfile.read(filename) |
| 116 |
doc = ET.fromstring(content1) |
| 117 |
self.reorder_attributes(doc) |
| 118 |
|
| 119 |
return ET.tostring(doc) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
def test_odt_basic(self): |
| 130 |
self.process_test('odt_basic.rst', 'odt_basic.odt', |
| 131 |
save_output_name='odt_basic.odt') |
| 132 |
|
| 133 |
def test_odt_nested_class(self): |
| 134 |
self.process_test('odt_nested_class.rst', |
| 135 |
'odt_nested_class.odt', |
| 136 |
save_output_name='odt_nested_class.odt') |
| 137 |
self.process_test('odt_unnested_class.rst', |
| 138 |
'odt_unnested_class.odt', |
| 139 |
save_output_name='odt_unnested_class.odt') |
| 140 |
self.process_test('odt_no_class.rst', |
| 141 |
'odt_no_class.odt', |
| 142 |
save_output_name='odt_no_class.odt') |
| 143 |
|
| 144 |
def test_odt_tables1(self): |
| 145 |
self.process_test('odt_tables1.rst', 'odt_tables1.odt', |
| 146 |
save_output_name='odt_tables1.odt') |
| 147 |
|
| 148 |
def test_odt_custom_headfoot(self): |
| 149 |
settings_overrides = { |
| 150 |
'custom_header': 'Page %p% of %P%', |
| 151 |
'custom_footer': 'Title: %t% Date: %d3% Time: %t4%', |
| 152 |
'language_code': 'en-US', |
| 153 |
} |
| 154 |
self.process_test('odt_custom_headfoot.rst', 'odt_custom_headfoot.odt', |
| 155 |
settings_overrides=settings_overrides, |
| 156 |
save_output_name='odt_custom_headfoot.odt') |
| 157 |
|
| 158 |
def test_odt_header_footer(self): |
| 159 |
self.process_test('odt_header_footer.rst', 'odt_header_footer.odt', |
| 160 |
save_output_name='odt_header_footer.odt') |
| 161 |
|
| 162 |
def test_odt_literal_block(self): |
| 163 |
self.process_test('odt_literal_block.rst', 'odt_literal_block.odt') |
| 164 |
|
| 165 |
def test_odt_contents(self): |
| 166 |
self.process_test('odt_contents.rst', 'odt_contents.odt') |
| 167 |
|
| 168 |
def test_odt_classifier(self): |
| 169 |
self.process_test('odt_classifier.rst', 'odt_classifier.odt') |
| 170 |
|
| 171 |
def test_odt_footnotes(self): |
| 172 |
self.process_test('odt_footnotes.rst', 'odt_footnotes.odt', |
| 173 |
save_output_name='odt_footnotes.odt') |
| 174 |
|
| 175 |
def test_odt_raw(self): |
| 176 |
self.process_test('odt_raw.rst', 'odt_raw.odt', |
| 177 |
save_output_name='odt_raw.odt') |
| 178 |
|
| 179 |
def test_odt_image(self): |
| 180 |
self.process_test('odt_image.rst', 'odt_image.odt', |
| 181 |
save_output_name='odt_image.odt') |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if __name__ == '__main__': |
| 195 |
unittest.main() |