|
9 | 9 | from docx.opc.constants import RELATIONSHIP_TYPE as RT |
10 | 10 | from docx.opc.coreprops import CoreProperties |
11 | 11 | from docx.opc.package import OpcPackage, Unmarshaller |
12 | | -from docx.opc.packuri import PACKAGE_URI |
| 12 | +from docx.opc.packuri import PACKAGE_URI, PackURI |
13 | 13 | from docx.opc.part import Part |
14 | 14 | from docx.opc.parts.coreprops import CorePropertiesPart |
15 | 15 | from docx.opc.pkgreader import PackageReader |
@@ -101,6 +101,20 @@ def it_can_iterate_over_parts_by_walking_rels_graph(self): |
101 | 101 | assert part2 in pkg.iter_parts() |
102 | 102 | assert len([p for p in pkg.iter_parts()]) == 2 |
103 | 103 |
|
| 104 | + def it_can_find_the_next_available_vector_partname( |
| 105 | + self, next_partname_fixture, iter_parts_, PackURI_, packuri_ |
| 106 | + ): |
| 107 | + """A vector partname is one with a numeric suffix, like header42.xml.""" |
| 108 | + parts_, expected_value = next_partname_fixture |
| 109 | + iter_parts_.return_value = iter(parts_) |
| 110 | + PackURI_.return_value = packuri_ |
| 111 | + package = OpcPackage() |
| 112 | + |
| 113 | + partname = package.next_partname(template="/foo/bar/baz%d.xml") |
| 114 | + |
| 115 | + PackURI_.assert_called_once_with(expected_value) |
| 116 | + assert partname is packuri_ |
| 117 | + |
104 | 118 | def it_can_find_a_part_related_by_reltype(self, related_part_fixture_): |
105 | 119 | pkg, reltype, related_part_ = related_part_fixture_ |
106 | 120 | related_part = pkg.part_related_by(reltype) |
@@ -161,6 +175,20 @@ def core_props_part_fixture( |
161 | 175 | part_related_by_.return_value = core_properties_part_ |
162 | 176 | return opc_package, core_properties_part_ |
163 | 177 |
|
| 178 | + @pytest.fixture( |
| 179 | + params=[((), 1), ((1,), 2), ((1, 2), 3), ((2, 3), 1), ((1, 3), 2)] |
| 180 | + ) |
| 181 | + def next_partname_fixture(self, request, iter_parts_): |
| 182 | + existing_partname_ns, next_partname_n = request.param |
| 183 | + parts_ = [ |
| 184 | + instance_mock( |
| 185 | + request, Part, name="part[%d]" % idx, partname="/foo/bar/baz%d.xml" % n |
| 186 | + ) |
| 187 | + for idx, n in enumerate(existing_partname_ns) |
| 188 | + ] |
| 189 | + expected_value = "/foo/bar/baz%d.xml" % next_partname_n |
| 190 | + return parts_, expected_value |
| 191 | + |
164 | 192 | @pytest.fixture |
165 | 193 | def relate_to_part_fixture_(self, request, pkg, rels_, reltype): |
166 | 194 | rId = 'rId99' |
@@ -196,10 +224,22 @@ def core_properties_part_(self, request): |
196 | 224 | def _core_properties_part_prop_(self, request): |
197 | 225 | return property_mock(request, OpcPackage, '_core_properties_part') |
198 | 226 |
|
| 227 | + @pytest.fixture |
| 228 | + def iter_parts_(self, request): |
| 229 | + return method_mock(request, OpcPackage, "iter_parts") |
| 230 | + |
199 | 231 | @pytest.fixture |
200 | 232 | def PackageReader_(self, request): |
201 | 233 | return class_mock(request, 'docx.opc.package.PackageReader') |
202 | 234 |
|
| 235 | + @pytest.fixture |
| 236 | + def PackURI_(self, request): |
| 237 | + return class_mock(request, "docx.opc.package.PackURI") |
| 238 | + |
| 239 | + @pytest.fixture |
| 240 | + def packuri_(self, request): |
| 241 | + return instance_mock(request, PackURI) |
| 242 | + |
203 | 243 | @pytest.fixture |
204 | 244 | def PackageWriter_(self, request): |
205 | 245 | return class_mock(request, 'docx.opc.package.PackageWriter') |
|
0 commit comments