|
9 | 9 | from mock import call, MagicMock, Mock, patch |
10 | 10 |
|
11 | 11 | from docx.opc.constants import CONTENT_TYPE as CT |
| 12 | +from docx.opc.oxml import oxml_fromstring |
| 13 | +from docx.opc.package import Part |
12 | 14 | from docx.opc.packuri import PackURI |
13 | 15 | from docx.opc.pkgwriter import _ContentTypesItem, PackageWriter |
14 | 16 |
|
15 | | -from ..unitutil import function_mock, method_mock |
| 17 | +from .unitdata.types import a_Default, a_Types, an_Override |
| 18 | +from ..unitutil import instance_mock, method_mock |
16 | 19 |
|
17 | 20 |
|
18 | 21 | class DescribePackageWriter(object): |
@@ -107,55 +110,55 @@ def xml_for(self, request): |
107 | 110 |
|
108 | 111 | class Describe_ContentTypesItem(object): |
109 | 112 |
|
110 | | - def it_can_compose_content_types_xml( |
111 | | - self, parts, types, serialize_part_xml_): |
112 | | - # exercise --------------------- |
113 | | - _ContentTypesItem.xml_for(parts) |
114 | | - # verify ----------------------- |
115 | | - expected_types_calls = [ |
116 | | - call.add_default('.jpeg', CT.JPEG), |
117 | | - call.add_default('.rels', CT.OPC_RELATIONSHIPS), |
118 | | - call.add_default('.xml', CT.XML), |
119 | | - call.add_override('/docProps/core.xml', 'app/vnd.core'), |
120 | | - call.add_override('/ppt/slides/slide1.xml', 'app/vnd.ct_sld'), |
121 | | - call.add_override('/ppt/slides/slide2.xml', 'app/vnd.ct_sld'), |
122 | | - call.add_override('/zebra/foo.bar', 'app/vnd.foobar'), |
123 | | - ] |
124 | | - assert types.mock_calls == expected_types_calls |
125 | | - serialize_part_xml_.assert_called_once_with(types) |
| 113 | + def it_can_compose_content_types_xml(self, xml_for_fixture): |
| 114 | + parts, expected_xml = xml_for_fixture |
| 115 | + types_xml = _ContentTypesItem.xml_for(parts) |
| 116 | + types_elm = oxml_fromstring(types_xml) |
| 117 | + assert types_elm.xml == expected_xml |
126 | 118 |
|
127 | 119 | # fixtures --------------------------------------------- |
128 | 120 |
|
129 | | - @pytest.fixture |
130 | | - def serialize_part_xml_(self, request): |
131 | | - return function_mock(request, 'docx.opc.pkgwriter.serialize_part_xml') |
132 | | - |
133 | | - @pytest.fixture |
134 | | - def parts(self): |
135 | | - """ |
136 | | - list of parts that will exercise _ContentTypesItem.xml_for() |
137 | | - """ |
138 | | - return [ |
139 | | - Mock(name='part_1', partname=PackURI('/docProps/core.xml'), |
140 | | - content_type='app/vnd.core'), |
141 | | - Mock(name='part_2', partname=PackURI('/docProps/thumbnail.jpeg'), |
142 | | - content_type=CT.JPEG), |
143 | | - Mock(name='part_3', partname=PackURI('/ppt/slides/slide2.xml'), |
144 | | - content_type='app/vnd.ct_sld'), |
145 | | - Mock(name='part_4', partname=PackURI('/ppt/slides/slide1.xml'), |
146 | | - content_type='app/vnd.ct_sld'), |
147 | | - Mock(name='part_5', partname=PackURI('/zebra/foo.bar'), |
148 | | - content_type='app/vnd.foobar'), |
149 | | - ] |
150 | | - |
151 | | - @pytest.fixture |
152 | | - def types(self, request): |
153 | | - """ |
154 | | - Mock returned by CT_Types.new() call |
155 | | - """ |
156 | | - types = Mock(name='types') |
157 | | - _patch = patch('docx.opc.pkgwriter.CT_Types') |
158 | | - CT_Types = _patch.start() |
159 | | - CT_Types.new.return_value = types |
160 | | - request.addfinalizer(_patch.stop) |
161 | | - return types |
| 121 | + def _mock_part(self, request, name, partname_str, content_type): |
| 122 | + partname = PackURI(partname_str) |
| 123 | + return instance_mock( |
| 124 | + request, Part, name=name, partname=partname, |
| 125 | + content_type=content_type |
| 126 | + ) |
| 127 | + |
| 128 | + @pytest.fixture(params=[ |
| 129 | + # ('Default', '/ppt/MEDIA/image.PNG', CT.PNG), |
| 130 | + ('Default', '/ppt/media/image.xml', CT.XML), |
| 131 | + ('Default', '/ppt/media/image.rels', CT.OPC_RELATIONSHIPS), |
| 132 | + ('Default', '/ppt/media/image.jpeg', CT.JPEG), |
| 133 | + ('Override', '/docProps/core.xml', 'app/vnd.core'), |
| 134 | + ('Override', '/ppt/slides/slide1.xml', 'app/vnd.ct_sld'), |
| 135 | + ('Override', '/zebra/foo.bar', 'app/vnd.foobar'), |
| 136 | + ]) |
| 137 | + def xml_for_fixture(self, request): |
| 138 | + elm_type, partname_str, content_type = request.param |
| 139 | + part_ = self._mock_part(request, 'part_', partname_str, content_type) |
| 140 | + # expected_xml ----------------- |
| 141 | + types_bldr = a_Types().with_nsdecls() |
| 142 | + ext = partname_str.split('.')[-1].lower() |
| 143 | + if elm_type == 'Default' and ext not in ('rels', 'xml'): |
| 144 | + default_bldr = a_Default() |
| 145 | + default_bldr.with_Extension(ext) |
| 146 | + default_bldr.with_ContentType(content_type) |
| 147 | + types_bldr.with_child(default_bldr) |
| 148 | + |
| 149 | + types_bldr.with_child( |
| 150 | + a_Default().with_Extension('rels') |
| 151 | + .with_ContentType(CT.OPC_RELATIONSHIPS) |
| 152 | + ) |
| 153 | + types_bldr.with_child( |
| 154 | + a_Default().with_Extension('xml').with_ContentType(CT.XML) |
| 155 | + ) |
| 156 | + |
| 157 | + if elm_type == 'Override': |
| 158 | + override_bldr = an_Override() |
| 159 | + override_bldr.with_PartName(partname_str) |
| 160 | + override_bldr.with_ContentType(content_type) |
| 161 | + types_bldr.with_child(override_bldr) |
| 162 | + |
| 163 | + expected_xml = types_bldr.xml() |
| 164 | + return [part_], expected_xml |
0 commit comments