Skip to content

Commit 7c64652

Browse files
ondrej-111scanny
authored andcommitted
rfctr: Section gets document_part
* Modernize section unit test fixtures while in there.
1 parent 8097e39 commit 7c64652

File tree

4 files changed

+108
-59
lines changed

4 files changed

+108
-59
lines changed

docx/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def add_section(self, start_type=WD_SECTION.NEW_PAGE):
8080
"""
8181
new_sectPr = self._element.body.add_section_break()
8282
new_sectPr.start_type = start_type
83-
return Section(new_sectPr)
83+
return Section(new_sectPr, self._part)
8484

8585
def add_table(self, rows, cols, style=None):
8686
"""

docx/section.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ def __init__(self, document_elm, document_part):
2020

2121
def __getitem__(self, key):
2222
if isinstance(key, slice):
23-
return [Section(sectPr) for sectPr in self._document_elm.sectPr_lst[key]]
24-
return Section(self._document_elm.sectPr_lst[key])
23+
return [
24+
Section(sectPr, self._document_part)
25+
for sectPr in self._document_elm.sectPr_lst[key]
26+
]
27+
return Section(self._document_elm.sectPr_lst[key], self._document_part)
2528

2629
def __iter__(self):
2730
for sectPr in self._document_elm.sectPr_lst:
28-
yield Section(sectPr)
31+
yield Section(sectPr, self._document_part)
2932

3033
def __len__(self):
3134
return len(self._document_elm.sectPr_lst)
@@ -37,9 +40,10 @@ class Section(object):
3740
Also provides access to headers and footers.
3841
"""
3942

40-
def __init__(self, sectPr):
43+
def __init__(self, sectPr, document_part):
4144
super(Section, self).__init__()
4245
self._sectPr = sectPr
46+
self._document_part = document_part
4347

4448
@property
4549
def bottom_margin(self):

tests/test_document.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,18 @@ def it_can_add_a_picture(self, add_picture_fixture):
6767
run_.add_picture.assert_called_once_with(path, width, height)
6868
assert picture is picture_
6969

70-
def it_can_add_a_section(self, add_section_fixture):
71-
document, start_type, Section_ = add_section_fixture[:3]
72-
section_, expected_xml = add_section_fixture[3:]
70+
def it_can_add_a_section(
71+
self, add_section_fixture, Section_, section_, document_part_
72+
):
73+
document_elm, start_type, expected_xml = add_section_fixture
74+
Section_.return_value = section_
75+
document = Document(document_elm, document_part_)
7376

7477
section = document.add_section(start_type)
7578

7679
assert document.element.xml == expected_xml
7780
sectPr = document.element.xpath('w:body/w:sectPr')[0]
78-
Section_.assert_called_once_with(sectPr)
81+
Section_.assert_called_once_with(sectPr, document_part_)
7982
assert section is section_
8083

8184
def it_can_add_a_table(self, add_table_fixture):
@@ -182,16 +185,14 @@ def add_picture_fixture(self, request, add_paragraph_, run_, picture_):
182185
('w:sectPr/w:type{w:val=oddPage}', WD_SECTION.NEW_PAGE,
183186
'w:sectPr'),
184187
])
185-
def add_section_fixture(self, request, Section_):
188+
def add_section_fixture(self, request):
186189
sentinel, start_type, new_sentinel = request.param
187-
document_cxml = 'w:document/w:body/(w:p,%s)' % sentinel
188-
document = Document(element(document_cxml), None)
190+
document_elm = element('w:document/w:body/(w:p,%s)' % sentinel)
189191
expected_xml = xml(
190192
'w:document/w:body/(w:p,w:p/w:pPr/%s,%s)' %
191193
(sentinel, new_sentinel)
192194
)
193-
section_ = Section_.return_value
194-
return document, start_type, Section_, section_, expected_xml
195+
return document_elm, start_type, expected_xml
195196

196197
@pytest.fixture
197198
def add_table_fixture(self, _block_width_prop_, body_prop_, table_):

tests/test_section.py

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,55 @@ def it_knows_how_many_sections_it_contains(self):
2323
)
2424
assert len(sections) == 2
2525

26-
def it_can_iterate_over_its_Section_instances(self, Section_, section_):
26+
def it_can_iterate_over_its_Section_instances(
27+
self, Section_, section_, document_part_
28+
):
2729
document_elm = element("w:document/w:body/(w:p/w:pPr/w:sectPr, w:sectPr)")
2830
sectPrs = document_elm.xpath("//w:sectPr")
2931
Section_.return_value = section_
30-
sections = Sections(document_elm, None)
32+
sections = Sections(document_elm, document_part_)
3133

3234
section_lst = [s for s in sections]
3335

34-
assert Section_.call_args_list == [call(sectPrs[0]), call(sectPrs[1])]
36+
assert Section_.call_args_list == [
37+
call(sectPrs[0], document_part_), call(sectPrs[1], document_part_)
38+
]
3539
assert section_lst == [section_, section_]
3640

37-
def it_can_access_its_Section_instances_by_index(self, Section_, section_):
41+
def it_can_access_its_Section_instances_by_index(
42+
self, Section_, section_, document_part_
43+
):
3844
document_elm = element(
3945
"w:document/w:body/(w:p/w:pPr/w:sectPr,w:p/w:pPr/w:sectPr,w:sectPr)"
4046
)
4147
sectPrs = document_elm.xpath("//w:sectPr")
4248
Section_.return_value = section_
43-
sections = Sections(document_elm, None)
49+
sections = Sections(document_elm, document_part_)
4450

4551
section_lst = [sections[idx] for idx in range(3)]
4652

4753
assert Section_.call_args_list == [
48-
call(sectPrs[0]), call(sectPrs[1]), call(sectPrs[2])
54+
call(sectPrs[0], document_part_),
55+
call(sectPrs[1], document_part_),
56+
call(sectPrs[2], document_part_),
4957
]
5058
assert section_lst == [section_, section_, section_]
5159

52-
def it_can_access_its_Section_instances_by_slice(self, Section_, section_):
60+
def it_can_access_its_Section_instances_by_slice(
61+
self, Section_, section_, document_part_
62+
):
5363
document_elm = element(
5464
"w:document/w:body/(w:p/w:pPr/w:sectPr,w:p/w:pPr/w:sectPr,w:sectPr)"
5565
)
5666
sectPrs = document_elm.xpath("//w:sectPr")
5767
Section_.return_value = section_
58-
sections = Sections(document_elm, None)
68+
sections = Sections(document_elm, document_part_)
5969

6070
section_lst = sections[1:9]
6171

62-
assert Section_.call_args_list == [call(sectPrs[1]), call(sectPrs[2])]
72+
assert Section_.call_args_list == [
73+
call(sectPrs[1], document_part_), call(sectPrs[2], document_part_)
74+
]
6375
assert section_lst == [section_, section_]
6476

6577
# fixture components ---------------------------------------------
@@ -80,51 +92,83 @@ def section_(self, request):
8092
class DescribeSection(object):
8193

8294
def it_knows_its_start_type(self, start_type_get_fixture):
83-
section, expected_start_type = start_type_get_fixture
84-
assert section.start_type is expected_start_type
95+
sectPr, expected_start_type = start_type_get_fixture
96+
section = Section(sectPr, None)
97+
98+
start_type = section.start_type
99+
100+
assert start_type is expected_start_type
85101

86102
def it_can_change_its_start_type(self, start_type_set_fixture):
87-
section, new_start_type, expected_xml = start_type_set_fixture
103+
sectPr, new_start_type, expected_xml = start_type_set_fixture
104+
section = Section(sectPr, None)
105+
88106
section.start_type = new_start_type
107+
89108
assert section._sectPr.xml == expected_xml
90109

91110
def it_knows_its_page_width(self, page_width_get_fixture):
92-
section, expected_page_width = page_width_get_fixture
93-
assert section.page_width == expected_page_width
111+
sectPr, expected_page_width = page_width_get_fixture
112+
section = Section(sectPr, None)
113+
114+
page_width = section.page_width
115+
116+
assert page_width == expected_page_width
94117

95118
def it_can_change_its_page_width(self, page_width_set_fixture):
96-
section, new_page_width, expected_xml = page_width_set_fixture
119+
sectPr, new_page_width, expected_xml = page_width_set_fixture
120+
section = Section(sectPr, None)
121+
97122
section.page_width = new_page_width
123+
98124
assert section._sectPr.xml == expected_xml
99125

100126
def it_knows_its_page_height(self, page_height_get_fixture):
101-
section, expected_page_height = page_height_get_fixture
102-
assert section.page_height == expected_page_height
127+
sectPr, expected_page_height = page_height_get_fixture
128+
section = Section(sectPr, None)
129+
130+
page_height = section.page_height
131+
132+
assert page_height == expected_page_height
103133

104134
def it_can_change_its_page_height(self, page_height_set_fixture):
105-
section, new_page_height, expected_xml = page_height_set_fixture
135+
sectPr, new_page_height, expected_xml = page_height_set_fixture
136+
section = Section(sectPr, None)
137+
106138
section.page_height = new_page_height
139+
107140
assert section._sectPr.xml == expected_xml
108141

109142
def it_knows_its_page_orientation(self, orientation_get_fixture):
110-
section, expected_orientation = orientation_get_fixture
111-
assert section.orientation is expected_orientation
143+
sectPr, expected_orientation = orientation_get_fixture
144+
section = Section(sectPr, None)
145+
146+
orientation = section.orientation
147+
148+
assert orientation is expected_orientation
112149

113150
def it_can_change_its_orientation(self, orientation_set_fixture):
114-
section, new_orientation, expected_xml = orientation_set_fixture
151+
sectPr, new_orientation, expected_xml = orientation_set_fixture
152+
section = Section(sectPr, None)
153+
115154
section.orientation = new_orientation
155+
116156
assert section._sectPr.xml == expected_xml
117157

118158
def it_knows_its_page_margins(self, margins_get_fixture):
119-
section, margin_prop_name, expected_value = margins_get_fixture
159+
sectPr, margin_prop_name, expected_value = margins_get_fixture
160+
section = Section(sectPr, None)
161+
120162
value = getattr(section, margin_prop_name)
163+
121164
assert value == expected_value
122165

123166
def it_can_change_its_page_margins(self, margins_set_fixture):
124-
section, margin_prop_name, new_value, expected_xml = (
125-
margins_set_fixture
126-
)
167+
sectPr, margin_prop_name, new_value, expected_xml = margins_set_fixture
168+
section = Section(sectPr, None)
169+
127170
setattr(section, margin_prop_name, new_value)
171+
128172
assert section._sectPr.xml == expected_xml
129173

130174
# fixtures -------------------------------------------------------
@@ -142,8 +186,8 @@ def it_can_change_its_page_margins(self, margins_set_fixture):
142186
])
143187
def margins_get_fixture(self, request):
144188
sectPr_cxml, margin_prop_name, expected_value = request.param
145-
section = Section(element(sectPr_cxml))
146-
return section, margin_prop_name, expected_value
189+
sectPr = element(sectPr_cxml)
190+
return sectPr, margin_prop_name, expected_value
147191

148192
@pytest.fixture(params=[
149193
('w:sectPr', 'left_margin', Inches(1),
@@ -166,9 +210,9 @@ def margins_get_fixture(self, request):
166210
])
167211
def margins_set_fixture(self, request):
168212
sectPr_cxml, property_name, new_value, expected_cxml = request.param
169-
section = Section(element(sectPr_cxml))
213+
sectPr = element(sectPr_cxml)
170214
expected_xml = xml(expected_cxml)
171-
return section, property_name, new_value, expected_xml
215+
return sectPr, property_name, new_value, expected_xml
172216

173217
@pytest.fixture(params=[
174218
('w:sectPr/w:pgSz{w:orient=landscape}', WD_ORIENT.LANDSCAPE),
@@ -178,8 +222,8 @@ def margins_set_fixture(self, request):
178222
])
179223
def orientation_get_fixture(self, request):
180224
sectPr_cxml, expected_orientation = request.param
181-
section = Section(element(sectPr_cxml))
182-
return section, expected_orientation
225+
sectPr = element(sectPr_cxml)
226+
return sectPr, expected_orientation
183227

184228
@pytest.fixture(params=[
185229
(WD_ORIENT.LANDSCAPE, 'w:sectPr/w:pgSz{w:orient=landscape}'),
@@ -188,9 +232,9 @@ def orientation_get_fixture(self, request):
188232
])
189233
def orientation_set_fixture(self, request):
190234
new_orientation, expected_cxml = request.param
191-
section = Section(element('w:sectPr'))
235+
sectPr = element('w:sectPr')
192236
expected_xml = xml(expected_cxml)
193-
return section, new_orientation, expected_xml
237+
return sectPr, new_orientation, expected_xml
194238

195239
@pytest.fixture(params=[
196240
('w:sectPr/w:pgSz{w:h=2880}', Inches(2)),
@@ -199,18 +243,18 @@ def orientation_set_fixture(self, request):
199243
])
200244
def page_height_get_fixture(self, request):
201245
sectPr_cxml, expected_page_height = request.param
202-
section = Section(element(sectPr_cxml))
203-
return section, expected_page_height
246+
sectPr = element(sectPr_cxml)
247+
return sectPr, expected_page_height
204248

205249
@pytest.fixture(params=[
206250
(None, 'w:sectPr/w:pgSz'),
207251
(Inches(2), 'w:sectPr/w:pgSz{w:h=2880}'),
208252
])
209253
def page_height_set_fixture(self, request):
210254
new_page_height, expected_cxml = request.param
211-
section = Section(element('w:sectPr'))
255+
sectPr = element('w:sectPr')
212256
expected_xml = xml(expected_cxml)
213-
return section, new_page_height, expected_xml
257+
return sectPr, new_page_height, expected_xml
214258

215259
@pytest.fixture(params=[
216260
('w:sectPr/w:pgSz{w:w=1440}', Inches(1)),
@@ -219,18 +263,18 @@ def page_height_set_fixture(self, request):
219263
])
220264
def page_width_get_fixture(self, request):
221265
sectPr_cxml, expected_page_width = request.param
222-
section = Section(element(sectPr_cxml))
223-
return section, expected_page_width
266+
sectPr = element(sectPr_cxml)
267+
return sectPr, expected_page_width
224268

225269
@pytest.fixture(params=[
226270
(None, 'w:sectPr/w:pgSz'),
227271
(Inches(4), 'w:sectPr/w:pgSz{w:w=5760}'),
228272
])
229273
def page_width_set_fixture(self, request):
230274
new_page_width, expected_cxml = request.param
231-
section = Section(element('w:sectPr'))
275+
sectPr = element('w:sectPr')
232276
expected_xml = xml(expected_cxml)
233-
return section, new_page_width, expected_xml
277+
return sectPr, new_page_width, expected_xml
234278

235279
@pytest.fixture(params=[
236280
('w:sectPr', WD_SECTION.NEW_PAGE),
@@ -243,8 +287,8 @@ def page_width_set_fixture(self, request):
243287
])
244288
def start_type_get_fixture(self, request):
245289
sectPr_cxml, expected_start_type = request.param
246-
section = Section(element(sectPr_cxml))
247-
return section, expected_start_type
290+
sectPr = element(sectPr_cxml)
291+
return sectPr, expected_start_type
248292

249293
@pytest.fixture(params=[
250294
('w:sectPr/w:type{w:val=oddPage}', WD_SECTION.EVEN_PAGE,
@@ -262,6 +306,6 @@ def start_type_get_fixture(self, request):
262306
])
263307
def start_type_set_fixture(self, request):
264308
initial_cxml, new_start_type, expected_cxml = request.param
265-
section = Section(element(initial_cxml))
309+
sectPr = element(initial_cxml)
266310
expected_xml = xml(expected_cxml)
267-
return section, new_start_type, expected_xml
311+
return sectPr, new_start_type, expected_xml

0 commit comments

Comments
 (0)