1414from docx .opc .package import PartFactory
1515from docx .opc .packuri import PackURI
1616from docx .oxml .parts .document import CT_Body , CT_Document
17+ from docx .oxml .section import CT_SectPr
1718from docx .oxml .text import CT_R
1819from docx .package import ImageParts , Package
1920from docx .parts .document import _Body , DocumentPart , InlineShapes , Sections
@@ -76,6 +77,15 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
7677 body_ .add_paragraph .assert_called_once_with ()
7778 assert p is p_
7879
80+ def it_can_add_a_section (self , add_section_fixture ):
81+ (document_part , start_type_ , body_elm_ , new_sectPr_ , Section_ ,
82+ section_ ) = add_section_fixture
83+ section = document_part .add_section (start_type_ )
84+ body_elm_ .add_section_break .assert_called_once_with ()
85+ assert new_sectPr_ .start_type == start_type_
86+ Section_ .assert_called_once_with (new_sectPr_ )
87+ assert section is section_
88+
7989 def it_can_add_a_table (self , add_table_fixture ):
8090 document_part , rows , cols , body_ , table_ = add_table_fixture
8191 table = document_part .add_table (rows , cols )
@@ -146,6 +156,16 @@ def add_paragraph_fixture(self, document_part_body_, body_, p_):
146156 document_part = DocumentPart (None , None , None , None )
147157 return document_part , body_ , p_
148158
159+ @pytest .fixture
160+ def add_section_fixture (
161+ self , document_elm_ , start_type_ , body_elm_ , sectPr_ , Section_ ,
162+ section_ ):
163+ document_part = DocumentPart (None , None , document_elm_ , None )
164+ return (
165+ document_part , start_type_ , body_elm_ , sectPr_ , Section_ ,
166+ section_
167+ )
168+
149169 @pytest .fixture
150170 def add_table_fixture (self , document_part_body_ , body_ , table_ ):
151171 document_part = DocumentPart (None , None , None , None )
@@ -223,6 +243,12 @@ def body_(self, request, p_, table_):
223243 body_ .add_table .return_value = table_
224244 return body_
225245
246+ @pytest .fixture
247+ def body_elm_ (self , request , sectPr_ ):
248+ body_elm_ = instance_mock (request , CT_Body )
249+ body_elm_ .add_section_break .return_value = sectPr_
250+ return body_elm_
251+
226252 @pytest .fixture
227253 def blob_ (self , request ):
228254 return instance_mock (request , str )
@@ -235,6 +261,10 @@ def content_type_(self, request):
235261 def document (self ):
236262 return DocumentPart (None , None , None , None )
237263
264+ @pytest .fixture
265+ def document_elm_ (self , request , body_elm_ ):
266+ return instance_mock (request , CT_Document , body = body_elm_ )
267+
238268 @pytest .fixture
239269 def document_part_ (self , request ):
240270 return instance_mock (request , DocumentPart )
@@ -321,16 +351,34 @@ def relate_to_(self, request, rId_):
321351 def rId_ (self , request ):
322352 return instance_mock (request , str )
323353
354+ @pytest .fixture
355+ def Section_ (self , request , section_ ):
356+ return class_mock (
357+ request , 'docx.parts.document.Section' , return_value = section_
358+ )
359+
360+ @pytest .fixture
361+ def section_ (self , request ):
362+ return instance_mock (request , Section )
363+
324364 @pytest .fixture
325365 def Sections_ (self , request ):
326366 return class_mock (request , 'docx.parts.document.Sections' )
327367
368+ @pytest .fixture
369+ def sectPr_ (self , request ):
370+ return instance_mock (request , CT_SectPr )
371+
328372 @pytest .fixture
329373 def serialize_part_xml_ (self , request ):
330374 return function_mock (
331375 request , 'docx.parts.document.serialize_part_xml'
332376 )
333377
378+ @pytest .fixture
379+ def start_type_ (self , request ):
380+ return instance_mock (request , int )
381+
334382 @pytest .fixture
335383 def table_ (self , request ):
336384 return instance_mock (request , Table )
0 commit comments