Skip to content

Commit abc2073

Browse files
author
Steve Canny
committed
opc: refactor DescribePackageWriter
use proper mock fixtures having autospecs for safety
1 parent 5d7cf81 commit abc2073

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

tests/opc/test_pkgwriter.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from docx.opc.oxml import oxml_fromstring
1313
from docx.opc.package import Part
1414
from docx.opc.packuri import PackURI
15+
from docx.opc.phys_pkg import _ZipPkgWriter
1516
from docx.opc.pkgwriter import _ContentTypesItem, PackageWriter
1617

1718
from .unitdata.types import a_Default, a_Types, an_Override
@@ -38,16 +39,13 @@ def it_can_write_a_package(self, PhysPkgWriter_, _write_methods):
3839
assert _write_methods.mock_calls == expected_calls
3940
phys_writer.close.assert_called_once_with()
4041

41-
def it_can_write_a_content_types_stream(self, xml_for):
42-
# mockery ----------------------
43-
phys_writer = Mock(name='phys_writer')
44-
parts = Mock(name='parts')
45-
# exercise ---------------------
46-
PackageWriter._write_content_types_stream(phys_writer, parts)
47-
# verify -----------------------
48-
xml_for.assert_called_once_with(parts)
49-
phys_writer.write.assert_called_once_with('/[Content_Types].xml',
50-
xml_for.return_value)
42+
def it_can_write_a_content_types_stream(self, write_cti_fixture):
43+
phys_pkg_writer_, parts_, xml_for_ = write_cti_fixture
44+
PackageWriter._write_content_types_stream(phys_pkg_writer_, parts_)
45+
xml_for_.assert_called_once_with(parts_)
46+
phys_pkg_writer_.write.assert_called_once_with(
47+
'/[Content_Types].xml', xml_for_.return_value
48+
)
5149

5250
def it_can_write_a_pkg_rels_item(self):
5351
# mockery ----------------------
@@ -78,12 +76,24 @@ def it_can_write_a_list_of_parts(self):
7876

7977
# fixtures ---------------------------------------------
8078

79+
@pytest.fixture
80+
def parts_(self, request):
81+
return instance_mock(request, list)
82+
8183
@pytest.fixture
8284
def PhysPkgWriter_(self, request):
8385
_patch = patch('docx.opc.pkgwriter.PhysPkgWriter')
8486
request.addfinalizer(_patch.stop)
8587
return _patch.start()
8688

89+
@pytest.fixture
90+
def phys_pkg_writer_(self, request):
91+
return instance_mock(request, _ZipPkgWriter)
92+
93+
@pytest.fixture
94+
def write_cti_fixture(self, phys_pkg_writer_, parts_, xml_for_):
95+
return phys_pkg_writer_, parts_, xml_for_
96+
8797
@pytest.fixture
8898
def _write_methods(self, request):
8999
"""Mock that patches all the _write_* methods of PackageWriter"""
@@ -104,7 +114,7 @@ def fin():
104114
return root_mock
105115

106116
@pytest.fixture
107-
def xml_for(self, request):
117+
def xml_for_(self, request):
108118
return method_mock(request, _ContentTypesItem, 'xml_for')
109119

110120

0 commit comments

Comments
 (0)