File: test_hdrftr.py

package info (click to toggle)
python-docx 0.8.11%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,640 kB
  • sloc: xml: 25,311; python: 21,911; makefile: 168
file content (153 lines) | stat: -rw-r--r-- 5,152 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# encoding: utf-8

"""Unit test suite for the docx.parts.hdrftr module"""

from __future__ import absolute_import, division, print_function, unicode_literals

import pytest

from docx.opc.constants import CONTENT_TYPE as CT, RELATIONSHIP_TYPE as RT
from docx.opc.part import PartFactory
from docx.package import Package
from docx.parts.hdrftr import FooterPart, HeaderPart

from ..unitutil.cxml import element
from ..unitutil.mock import function_mock, initializer_mock, instance_mock, method_mock


class DescribeFooterPart(object):

    def it_is_used_by_loader_to_construct_footer_part(
        self, package_, FooterPart_load_, footer_part_
    ):
        partname = "footer1.xml"
        content_type = CT.WML_FOOTER
        reltype = RT.FOOTER
        blob = "<w:ftr/>"
        FooterPart_load_.return_value = footer_part_

        part = PartFactory(partname, content_type, reltype, blob, package_)

        FooterPart_load_.assert_called_once_with(partname, content_type, blob, package_)
        assert part is footer_part_

    def it_can_create_a_new_footer_part(
        self, package_, _default_footer_xml_, parse_xml_, _init_
    ):
        ftr = element("w:ftr")
        package_.next_partname.return_value = "/word/footer24.xml"
        _default_footer_xml_.return_value = "<w:ftr>"
        parse_xml_.return_value = ftr

        footer_part = FooterPart.new(package_)

        package_.next_partname.assert_called_once_with("/word/footer%d.xml")
        _default_footer_xml_.assert_called_once_with()
        parse_xml_.assert_called_once_with("<w:ftr>")
        _init_.assert_called_once_with(
            footer_part, "/word/footer24.xml", CT.WML_FOOTER, ftr, package_
        )

    def it_loads_default_footer_XML_from_a_template_to_help(self):
        # ---tests integration with OS---
        xml_bytes = FooterPart._default_footer_xml()

        assert xml_bytes.startswith(
            b"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n<w:ftr\n"
        )
        assert len(xml_bytes) == 1395

    # fixture components ---------------------------------------------

    @pytest.fixture
    def _default_footer_xml_(self, request):
        return method_mock(request, FooterPart, "_default_footer_xml", autospec=False)

    @pytest.fixture
    def footer_part_(self, request):
        return instance_mock(request, FooterPart)

    @pytest.fixture
    def FooterPart_load_(self, request):
        return method_mock(request, FooterPart, "load", autospec=False)

    @pytest.fixture
    def _init_(self, request):
        return initializer_mock(request, FooterPart)

    @pytest.fixture
    def package_(self, request):
        return instance_mock(request, Package)

    @pytest.fixture
    def parse_xml_(self, request):
        return function_mock(request, "docx.parts.hdrftr.parse_xml")


class DescribeHeaderPart(object):

    def it_is_used_by_loader_to_construct_header_part(
        self, package_, HeaderPart_load_, header_part_
    ):
        partname = "header1.xml"
        content_type = CT.WML_HEADER
        reltype = RT.HEADER
        blob = "<w:hdr/>"
        HeaderPart_load_.return_value = header_part_

        part = PartFactory(partname, content_type, reltype, blob, package_)

        HeaderPart_load_.assert_called_once_with(partname, content_type, blob, package_)
        assert part is header_part_

    def it_can_create_a_new_header_part(
        self, package_, _default_header_xml_, parse_xml_, _init_
    ):
        hdr = element("w:hdr")
        package_.next_partname.return_value = "/word/header42.xml"
        _default_header_xml_.return_value = "<w:hdr>"
        parse_xml_.return_value = hdr

        header_part = HeaderPart.new(package_)

        package_.next_partname.assert_called_once_with("/word/header%d.xml")
        _default_header_xml_.assert_called_once_with()
        parse_xml_.assert_called_once_with("<w:hdr>")
        _init_.assert_called_once_with(
            header_part, "/word/header42.xml", CT.WML_HEADER, hdr, package_
        )

    def it_loads_default_header_XML_from_a_template_to_help(self):
        # ---tests integration with OS---
        xml_bytes = HeaderPart._default_header_xml()

        assert xml_bytes.startswith(
            b"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n<w:hdr\n"
        )
        assert len(xml_bytes) == 1395

    # fixture components ---------------------------------------------

    @pytest.fixture
    def _default_header_xml_(self, request):
        return method_mock(request, HeaderPart, "_default_header_xml", autospec=False)

    @pytest.fixture
    def HeaderPart_load_(self, request):
        return method_mock(request, HeaderPart, "load", autospec=False)

    @pytest.fixture
    def header_part_(self, request):
        return instance_mock(request, HeaderPart)

    @pytest.fixture
    def _init_(self, request):
        return initializer_mock(request, HeaderPart)

    @pytest.fixture
    def package_(self, request):
        return instance_mock(request, Package)

    @pytest.fixture
    def parse_xml_(self, request):
        return function_mock(request, "docx.parts.hdrftr.parse_xml")