44Test suite for the docx.api module
55"""
66
7+ from __future__ import (
8+ absolute_import , division , print_function , unicode_literals
9+ )
10+
711import pytest
812
913from docx .api import Document
1014from docx .enum .text import WD_BREAK
1115from docx .opc .constants import CONTENT_TYPE as CT
1216from docx .package import Package
1317from docx .parts .document import DocumentPart , InlineShapes
18+ from docx .table import Table
1419from docx .text import Paragraph , Run
1520
1621from .unitutil import (
@@ -89,6 +94,16 @@ def it_can_add_a_picture(self, add_picture_fixture):
8994 assert picture .height == expected_height
9095 assert picture is picture_
9196
97+ def it_can_add_a_table (self , add_table_fixture ):
98+ document , rows , cols , style , document_part_ , expected_style , table_ = (
99+ add_table_fixture
100+ )
101+ table = document .add_table (rows , cols , style )
102+ print (table )
103+ document_part_ .add_table .assert_called_once_with (rows , cols )
104+ assert table .style == expected_style
105+ assert table == table_
106+
92107 def it_provides_access_to_the_document_body (self , document ):
93108 body = document .body
94109 assert body is document ._document_part .body
@@ -154,6 +169,15 @@ def add_styled_paragraph_fixture(self, document, p_):
154169 style = 'foobaresque'
155170 return document , style , p_
156171
172+ @pytest .fixture (params = [None , 'LightShading-Accent1' , 'foobar' ])
173+ def add_table_fixture (self , request , document , document_part_ , table_ ):
174+ rows , cols = 4 , 2
175+ style = expected_style = request .param
176+ return (
177+ document , rows , cols , style , document_part_ , expected_style ,
178+ table_
179+ )
180+
157181 @pytest .fixture
158182 def add_text_paragraph_fixture (self , document , p_ , r_ ):
159183 text = 'foobar\r barfoo'
@@ -174,11 +198,12 @@ def document(self, open_):
174198 return Document ()
175199
176200 @pytest .fixture
177- def document_part_ (self , request , p_ , paragraphs_ ):
201+ def document_part_ (self , request , p_ , paragraphs_ , table_ ):
178202 document_part_ = instance_mock (
179203 request , DocumentPart , content_type = CT .WML_DOCUMENT_MAIN
180204 )
181205 document_part_ .add_paragraph .return_value = p_
206+ document_part_ .add_table .return_value = table_
182207 document_part_ .paragraphs = paragraphs_
183208 return document_part_
184209
@@ -240,3 +265,7 @@ def save_fixture(self, request, open_, package_):
240265 file_ = instance_mock (request , str )
241266 document = Document ()
242267 return document , package_ , file_
268+
269+ @pytest .fixture
270+ def table_ (self , request ):
271+ return instance_mock (request , Table , style = None )
0 commit comments