Skip to content

Commit 4321a40

Browse files
author
Steve Canny
committed
test: refactor parts.test_document.py to use cxml
1 parent 6ebedac commit 4321a40

1 file changed

Lines changed: 46 additions & 82 deletions

File tree

tests/parts/test_document.py

Lines changed: 46 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
from docx.table import Table
2323
from docx.text import Paragraph
2424

25-
from ..oxml.unitdata.dml import a_drawing, an_inline
2625
from ..oxml.parts.unitdata.document import a_body, a_document
2726
from ..oxml.unitdata.table import (
2827
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tblW, a_tc, a_tr
2928
)
30-
from ..oxml.unitdata.text import a_p, a_pPr, a_sectPr, an_r
29+
from ..oxml.unitdata.text import a_p, a_sectPr
30+
from ..unitutil.cxml import element, xml
3131
from ..unitutil.mock import (
3232
function_mock, class_mock, initializer_mock, instance_mock, loose_mock,
3333
method_mock, Mock, property_mock
@@ -400,24 +400,22 @@ def it_can_add_a_table(self, add_table_fixture):
400400
assert body._body.xml == expected_xml
401401
assert isinstance(table, Table)
402402

403-
def it_can_clear_itself_of_all_content_it_holds(
404-
self, clear_content_fixture):
405-
body, expected_xml = clear_content_fixture
403+
def it_can_clear_itself_of_all_content_it_holds(self, clear_fixture):
404+
body, expected_xml = clear_fixture
406405
_body = body.clear_content()
407406
assert body._body.xml == expected_xml
408407
assert _body is body
409408

410409
def it_provides_access_to_the_paragraphs_it_contains(
411-
self, body_with_paragraphs):
412-
body = body_with_paragraphs
410+
self, paragraphs_fixture):
411+
body = paragraphs_fixture
413412
paragraphs = body.paragraphs
414413
assert len(paragraphs) == 2
415414
for p in paragraphs:
416415
assert isinstance(p, Paragraph)
417416

418-
def it_provides_access_to_the_tables_it_contains(
419-
self, body_with_tables):
420-
body = body_with_tables
417+
def it_provides_access_to_the_tables_it_contains(self, tables_fixture):
418+
body = tables_fixture
421419
tables = body.tables
422420
assert len(tables) == 2
423421
for table in tables:
@@ -426,18 +424,15 @@ def it_provides_access_to_the_tables_it_contains(
426424
# fixtures -------------------------------------------------------
427425

428426
@pytest.fixture(params=[
429-
(0, False), (1, False), (0, True), (1, True)
427+
('w:body', 'w:body/w:p'),
428+
('w:body/w:p', 'w:body/(w:p, w:p)'),
429+
('w:body/w:sectPr', 'w:body/(w:p, w:sectPr)'),
430+
('w:body/(w:p, w:sectPr)', 'w:body/(w:p, w:p, w:sectPr)'),
430431
])
431432
def add_paragraph_fixture(self, request):
432-
p_count, has_sectPr = request.param
433-
# body element -----------------
434-
body_bldr = self._body_bldr(p_count=p_count, sectPr=has_sectPr)
435-
body_elm = body_bldr.element
436-
body = _Body(body_elm)
437-
# expected XML -----------------
438-
p_count += 1
439-
body_bldr = self._body_bldr(p_count=p_count, sectPr=has_sectPr)
440-
expected_xml = body_bldr.xml()
433+
before_cxml, after_cxml = request.param
434+
body = _Body(element(before_cxml))
435+
expected_xml = xml(after_cxml)
441436
return body, expected_xml
442437

443438
@pytest.fixture(params=[(0, False), (0, True), (1, False), (1, True)])
@@ -454,42 +449,27 @@ def add_table_fixture(self, request):
454449

455450
return body, expected_xml
456451

452+
@pytest.fixture(params=[
453+
('w:body', 'w:body'),
454+
('w:body/w:p', 'w:body'),
455+
('w:body/w:sectPr', 'w:body/w:sectPr'),
456+
('w:body/(w:p, w:sectPr)', 'w:body/w:sectPr'),
457+
])
458+
def clear_fixture(self, request):
459+
before_cxml, after_cxml = request.param
460+
body = _Body(element(before_cxml))
461+
expected_xml = xml(after_cxml)
462+
return body, expected_xml
463+
457464
@pytest.fixture
458-
def body_with_paragraphs(self):
459-
body_elm = (
460-
a_body().with_nsdecls()
461-
.with_child(a_p())
462-
.with_child(a_p())
463-
.element
464-
)
465-
return _Body(body_elm)
465+
def paragraphs_fixture(self):
466+
return _Body(element('w:body/(w:p, w:p)'))
466467

467468
@pytest.fixture
468-
def body_with_tables(self):
469-
body_elm = (
470-
a_body().with_nsdecls()
471-
.with_child(a_tbl())
472-
.with_child(a_tbl())
473-
.element
474-
)
475-
return _Body(body_elm)
469+
def tables_fixture(self):
470+
return _Body(element('w:body/(w:tbl, w:tbl)'))
476471

477-
@pytest.fixture(params=[False, True])
478-
def clear_content_fixture(self, request):
479-
has_sectPr = request.param
480-
# body element -----------------
481-
body_bldr = a_body().with_nsdecls()
482-
body_bldr.with_child(a_p())
483-
if has_sectPr:
484-
body_bldr.with_child(a_sectPr())
485-
body_elm = body_bldr.element
486-
body = _Body(body_elm)
487-
# expected XML -----------------
488-
body_bldr = a_body().with_nsdecls()
489-
if has_sectPr:
490-
body_bldr.with_child(a_sectPr())
491-
expected_xml = body_bldr.xml()
492-
return body, expected_xml
472+
# fixture components ---------------------------------------------
493473

494474
def _body_bldr(self, p_count=0, tbl_bldr=None, sectPr=False):
495475
body_bldr = a_body().with_nsdecls()
@@ -535,8 +515,8 @@ class DescribeInlineShapes(object):
535515

536516
def it_knows_how_many_inline_shapes_it_contains(
537517
self, inline_shapes_fixture):
538-
inline_shapes, inline_shape_count = inline_shapes_fixture
539-
assert len(inline_shapes) == inline_shape_count
518+
inline_shapes, expected_count = inline_shapes_fixture
519+
assert len(inline_shapes) == expected_count
540520

541521
def it_can_iterate_over_its_InlineShape_instances(
542522
self, inline_shapes_fixture):
@@ -599,6 +579,17 @@ def add_picture_fixture(
599579
image_part_, rId_, shape_id_, new_picture_shape_
600580
)
601581

582+
@pytest.fixture
583+
def inline_shapes_fixture(self):
584+
body = element(
585+
'w:body/w:p/(w:r/w:drawing/wp:inline, w:r/w:drawing/wp:inline)'
586+
)
587+
inline_shapes = InlineShapes(body, None)
588+
expected_count = 2
589+
return inline_shapes, expected_count
590+
591+
# fixture components ---------------------------------------------
592+
602593
@pytest.fixture
603594
def body_(self, request, r_):
604595
body_ = instance_mock(request, CT_Body)
@@ -626,25 +617,6 @@ def InlineShape_(self, request, new_picture_shape_):
626617
InlineShape_.new_picture.return_value = new_picture_shape_
627618
return InlineShape_
628619

629-
@pytest.fixture
630-
def inline_shapes_fixture(self):
631-
inline_shape_count = 2
632-
body = (
633-
a_body().with_nsdecls('w', 'wp').with_child(
634-
a_p().with_child(
635-
an_r().with_child(
636-
a_drawing().with_child(
637-
an_inline()))).with_child(
638-
an_r().with_child(
639-
a_drawing().with_child(
640-
an_inline())
641-
)
642-
)
643-
)
644-
).element
645-
inline_shapes = InlineShapes(body, None)
646-
return inline_shapes, inline_shape_count
647-
648620
@pytest.fixture
649621
def inline_shapes_with_parent_(self, request):
650622
parent_ = loose_mock(request, name='parent_')
@@ -672,7 +644,6 @@ class DescribeSections(object):
672644

673645
def it_knows_how_many_sections_it_contains(self, len_fixture):
674646
sections, expected_len = len_fixture
675-
print(sections._document_elm.xml)
676647
assert len(sections) == expected_len
677648

678649
def it_can_iterate_over_its_Section_instances(self, iter_fixture):
@@ -710,11 +681,4 @@ def len_fixture(self, document_elm):
710681

711682
@pytest.fixture
712683
def document_elm(self):
713-
return (
714-
a_document().with_nsdecls().with_child(
715-
a_body().with_child(
716-
a_p().with_child(
717-
a_pPr().with_child(
718-
a_sectPr()))).with_child(
719-
a_sectPr()))
720-
).element
684+
return element('w:document/w:body/(w:p/w:pPr/w:sectPr, w:sectPr)')

0 commit comments

Comments
 (0)