|
| 1 | +# |
| 2 | +# Copyright 2018 Vitalii Kulanov |
| 3 | +# |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from docx import Document |
| 8 | +from docx.shared import Pt |
| 9 | + |
| 10 | +from validocx import wrapper |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def document(): |
| 15 | + doc = Document() |
| 16 | + doc.add_heading('Fake Title', 0) |
| 17 | + doc.add_heading('Fake Header 1', 1) |
| 18 | + doc.add_heading('Fake Header 2', 2) |
| 19 | + # Redefine font parameters for 'Normal' style |
| 20 | + style = doc.styles['Normal'] |
| 21 | + font = style.font |
| 22 | + font.name = 'Calibri' |
| 23 | + font.size = Pt(12) |
| 24 | + # Add some text in 'Normal' style |
| 25 | + p = doc.add_paragraph('Some ') |
| 26 | + p.add_run('bold').bold = True |
| 27 | + p.add_run(' and some ') |
| 28 | + p.add_run('italic.').italic = True |
| 29 | + return doc |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def docx_wrapper(document): |
| 34 | + document_wrapper = wrapper.DocumentWrapper(document) |
| 35 | + return document_wrapper |
| 36 | + |
| 37 | + |
| 38 | +def test_fetch_author_data(docx_wrapper): |
| 39 | + assert docx_wrapper.author == 'python-docx' |
| 40 | + |
| 41 | + |
| 42 | +def test_fetch_date_of_creation(docx_wrapper): |
| 43 | + assert docx_wrapper.created.isoformat(' ') == '2013-12-23 23:15:00' |
| 44 | + |
| 45 | + |
| 46 | +def test_fetch_date_of_modification(docx_wrapper): |
| 47 | + assert docx_wrapper.modified.isoformat(' ') == '2013-12-23 23:15:00' |
| 48 | + |
| 49 | + |
| 50 | +def test_fetch_last_modifier_data(docx_wrapper): |
| 51 | + assert docx_wrapper.last_modified_by == '' |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize('paragraph_styles, expected', [ |
| 55 | + ('Title', ['Fake Title']), |
| 56 | + (None, ['Fake Title', 'Fake Header 1', |
| 57 | + 'Fake Header 2', 'Some bold and some italic.']) |
| 58 | +]) |
| 59 | +def test_fetch_paragraph_data(docx_wrapper, paragraph_styles, expected): |
| 60 | + for item in docx_wrapper.iter_paragraphs(paragraph_styles): |
| 61 | + assert item.text in expected |
| 62 | + |
| 63 | + |
| 64 | +def test_fetch_section_data(docx_wrapper): |
| 65 | + assert len(list(docx_wrapper.iter_sections())) == 1 |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize('p_style, expected', [ |
| 69 | + ('Title', [[26.0, 'Calibri']]), |
| 70 | + ('Normal', [[12.0, 'Calibri'], [12.0, 'Calibri', 'bold'], |
| 71 | + [12.0, 'Calibri'], [12.0, 'Calibri', 'italic']]) |
| 72 | +]) |
| 73 | +def test_fetch_font_attributes(docx_wrapper, p_style, expected): |
| 74 | + p = list(docx_wrapper.iter_paragraphs(p_style))[0] |
| 75 | + runs = docx_wrapper.get_font_attributes(p) |
| 76 | + assert runs == expected |
0 commit comments