-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_story.py
More file actions
144 lines (110 loc) · 5.13 KB
/
test_story.py
File metadata and controls
144 lines (110 loc) · 5.13 KB
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
"""Unit test suite for the docx.parts.story module."""
import pytest
from docx.enum.style import WD_STYLE_TYPE
from docx.image.image import Image
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from docx.package import Package
from docx.parts.document import DocumentPart
from docx.parts.image import ImagePart
from docx.parts.story import StoryPart
from docx.styles.style import BaseStyle
from ..unitutil.cxml import element
from ..unitutil.file import snippet_text
from ..unitutil.mock import instance_mock, method_mock, property_mock
class DescribeStoryPart:
def it_can_get_or_add_an_image(self, package_, image_part_, image_, relate_to_):
package_.get_or_add_image_part.return_value = image_part_
relate_to_.return_value = "rId42"
image_part_.image = image_
story_part = StoryPart(None, None, None, package_)
rId, image = story_part.get_or_add_image("image.png")
package_.get_or_add_image_part.assert_called_once_with("image.png")
relate_to_.assert_called_once_with(story_part, image_part_, RT.IMAGE)
assert rId == "rId42"
assert image is image_
def it_can_get_a_style_by_id_and_type(self, _document_part_prop_, document_part_, style_):
style_id = "BodyText"
style_type = WD_STYLE_TYPE.PARAGRAPH
_document_part_prop_.return_value = document_part_
document_part_.get_style.return_value = style_
story_part = StoryPart(None, None, None, None)
style = story_part.get_style(style_id, style_type)
document_part_.get_style.assert_called_once_with(style_id, style_type)
assert style is style_
def it_can_get_a_style_id_by_style_or_name_and_type(
self, _document_part_prop_, document_part_, style_
):
style_type = WD_STYLE_TYPE.PARAGRAPH
_document_part_prop_.return_value = document_part_
document_part_.get_style_id.return_value = "BodyText"
story_part = StoryPart(None, None, None, None)
style_id = story_part.get_style_id(style_, style_type)
document_part_.get_style_id.assert_called_once_with(style_, style_type)
assert style_id == "BodyText"
def it_can_create_a_new_pic_inline(self, get_or_add_image_, image_, next_id_prop_):
get_or_add_image_.return_value = "rId42", image_
image_.scaled_dimensions.return_value = 444, 888
image_.filename = "bar.png"
next_id_prop_.return_value = 24
expected_xml = snippet_text("inline")
story_part = StoryPart(None, None, None, None)
inline = story_part.new_pic_inline("foo/bar.png", width=100, height=200)
get_or_add_image_.assert_called_once_with(story_part, "foo/bar.png")
image_.scaled_dimensions.assert_called_once_with(100, 200)
assert inline.xml == expected_xml
def it_knows_the_next_available_xml_id(self, next_id_fixture):
story_element, expected_value = next_id_fixture
story_part = StoryPart(None, None, story_element, None)
next_id = story_part.next_id
assert next_id == expected_value
def it_knows_the_main_document_part_to_help(self, package_, document_part_):
package_.main_document_part = document_part_
story_part = StoryPart(None, None, None, package_)
document_part = story_part._document_part
assert document_part is document_part_
# fixtures -------------------------------------------------------
@pytest.fixture(
params=[
(("w:document"), 1),
(("w:document/w:p{id=1}"), 2),
(("w:document/w:p{id=2}"), 3),
(("w:hdr/(w:p{id=1},w:p{id=2},w:p{id=3})"), 4),
(("w:hdr/(w:p{id=1},w:p{id=2},w:p{id=4})"), 5),
(("w:hdr/(w:p{id=0},w:p{id=0})"), 1),
(("w:ftr/(w:p{id=0},w:p{id=0},w:p{id=1},w:p{id=3})"), 4),
(("w:ftr/(w:p{id=foo},w:p{id=1},w:p{id=2})"), 3),
(("w:ftr/(w:p{id=1},w:p{id=bar})"), 2),
]
)
def next_id_fixture(self, request):
story_cxml, expected_value = request.param
story_element = element(story_cxml)
return story_element, expected_value
# fixture components ---------------------------------------------
@pytest.fixture
def document_part_(self, request):
return instance_mock(request, DocumentPart)
@pytest.fixture
def _document_part_prop_(self, request):
return property_mock(request, StoryPart, "_document_part")
@pytest.fixture
def get_or_add_image_(self, request):
return method_mock(request, StoryPart, "get_or_add_image")
@pytest.fixture
def image_(self, request):
return instance_mock(request, Image)
@pytest.fixture
def image_part_(self, request):
return instance_mock(request, ImagePart)
@pytest.fixture
def next_id_prop_(self, request):
return property_mock(request, StoryPart, "next_id")
@pytest.fixture
def package_(self, request):
return instance_mock(request, Package)
@pytest.fixture
def relate_to_(self, request):
return method_mock(request, StoryPart, "relate_to")
@pytest.fixture
def style_(self, request):
return instance_mock(request, BaseStyle)