Skip to content

Commit 8cdfa29

Browse files
author
Steve Canny
committed
doc: add DocumentPart.add_paragraph()
1 parent 770db0a commit 8cdfa29

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

docx/parts/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ def add_paragraph(self):
3232
"""
3333
Return a paragraph newly added to the end of body content.
3434
"""
35-
raise NotImplementedError
35+
return self.body.add_paragraph()
3636

3737
@property
3838
def blob(self):
3939
return serialize_part_xml(self._element)
4040

41-
@property
41+
@lazyproperty
4242
def body(self):
4343
"""
4444
The |_Body| instance containing the content for this document.

tests/parts/test_document.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ def it_can_be_constructed_by_opc_part_factory(
7070
)
7171
assert isinstance(doc, DocumentPart)
7272

73+
def it_can_add_a_paragraph(self, add_paragraph_fixture):
74+
document_part, body_, p_ = add_paragraph_fixture
75+
p = document_part.add_paragraph()
76+
body_.add_paragraph.assert_called_once_with()
77+
assert p is p_
78+
7379
def it_can_add_an_image_part_to_the_document(
7480
self, get_or_add_image_fixture):
7581
(document, image_descriptor_, image_parts_, relate_to_, image_part_,
@@ -89,8 +95,10 @@ def it_has_a_body(self, document_body_fixture):
8995
assert _body is _Body_.return_value
9096

9197
def it_can_serialize_to_xml(self, document_blob_fixture):
92-
document, document_elm, serialize_part_xml_ = document_blob_fixture
93-
blob = document.blob
98+
document_part, document_elm, serialize_part_xml_ = (
99+
document_blob_fixture
100+
)
101+
blob = document_part.blob
94102
serialize_part_xml_.assert_called_once_with(document_elm)
95103
assert blob is serialize_part_xml_.return_value
96104

@@ -110,10 +118,21 @@ def it_knows_the_next_available_xml_id(self, next_id_fixture):
110118

111119
# fixtures -------------------------------------------------------
112120

121+
@pytest.fixture
122+
def add_paragraph_fixture(self, document_part_body_, body_, p_):
123+
document_part = DocumentPart(None, None, None, None)
124+
return document_part, body_, p_
125+
113126
@pytest.fixture
114127
def _Body_(self, request):
115128
return class_mock(request, 'docx.parts.document._Body')
116129

130+
@pytest.fixture
131+
def body_(self, request, p_):
132+
body_ = instance_mock(request, _Body)
133+
body_.add_paragraph.return_value = p_
134+
return body_
135+
117136
@pytest.fixture
118137
def blob_(self, request):
119138
return instance_mock(request, str)
@@ -129,8 +148,8 @@ def document(self):
129148
@pytest.fixture
130149
def document_blob_fixture(self, request, serialize_part_xml_):
131150
document_elm = instance_mock(request, CT_Document)
132-
document = DocumentPart(None, None, document_elm, None)
133-
return document, document_elm, serialize_part_xml_
151+
document_part = DocumentPart(None, None, document_elm, None)
152+
return document_part, document_elm, serialize_part_xml_
134153

135154
@pytest.fixture
136155
def document_body_fixture(self, request, _Body_):
@@ -146,6 +165,12 @@ def document_body_fixture(self, request, _Body_):
146165
def document_part_(self, request):
147166
return instance_mock(request, DocumentPart)
148167

168+
@pytest.fixture
169+
def document_part_body_(self, request, body_):
170+
return property_mock(
171+
request, DocumentPart, 'body', return_value=body_
172+
)
173+
149174
@pytest.fixture
150175
def document_part_load_(self, request):
151176
return method_mock(request, DocumentPart, 'load')
@@ -211,6 +236,10 @@ def next_id_fixture(self, request):
211236
def oxml_fromstring_(self, request):
212237
return function_mock(request, 'docx.parts.document.oxml_fromstring')
213238

239+
@pytest.fixture
240+
def p_(self, request):
241+
return instance_mock(request, Paragraph)
242+
214243
@pytest.fixture
215244
def package_(self, request):
216245
return instance_mock(request, Package)

0 commit comments

Comments
 (0)