|
13 | 13 | from docx.opc.constants import CONTENT_TYPE as CT |
14 | 14 | from docx.opc.packuri import PackURI |
15 | 15 | from docx.oxml.comments import CT_Comment, CT_Comments |
| 16 | +from docx.oxml.ns import qn |
16 | 17 | from docx.package import Package |
17 | 18 | from docx.parts.comments import CommentsPart |
18 | 19 |
|
@@ -86,8 +87,85 @@ def it_can_get_a_comment_by_id(self, package_: Mock): |
86 | 87 | assert type(comment) is Comment, "expected a `Comment` object" |
87 | 88 | assert comment._comment_elm is comments_elm.comment_lst[1] |
88 | 89 |
|
| 90 | + def but_it_returns_None_when_no_comment_with_that_id_exists(self, package_: Mock): |
| 91 | + comments_elm = cast( |
| 92 | + CT_Comments, |
| 93 | + element("w:comments/(w:comment{w:id=1},w:comment{w:id=2},w:comment{w:id=3})"), |
| 94 | + ) |
| 95 | + comments = Comments( |
| 96 | + comments_elm, |
| 97 | + CommentsPart( |
| 98 | + PackURI("/word/comments.xml"), |
| 99 | + CT.WML_COMMENTS, |
| 100 | + comments_elm, |
| 101 | + package_, |
| 102 | + ), |
| 103 | + ) |
| 104 | + |
| 105 | + comment = comments.get(4) |
| 106 | + |
| 107 | + assert comment is None, "expected None when no comment with that id exists" |
| 108 | + |
| 109 | + def it_can_add_a_new_comment(self, package_: Mock): |
| 110 | + comments_elm = cast(CT_Comments, element("w:comments")) |
| 111 | + comments_part = CommentsPart( |
| 112 | + PackURI("/word/comments.xml"), |
| 113 | + CT.WML_COMMENTS, |
| 114 | + comments_elm, |
| 115 | + package_, |
| 116 | + ) |
| 117 | + now_before = dt.datetime.now(dt.timezone.utc).replace(microsecond=0) |
| 118 | + comments = Comments(comments_elm, comments_part) |
| 119 | + |
| 120 | + comment = comments.add_comment() |
| 121 | + |
| 122 | + now_after = dt.datetime.now(dt.timezone.utc).replace(microsecond=0) |
| 123 | + # -- a comment is unconditionally added, and returned for any further adjustment -- |
| 124 | + assert isinstance(comment, Comment) |
| 125 | + # -- it is "linked" to the comments part so it can add images and hyperlinks, etc. -- |
| 126 | + assert comment.part is comments_part |
| 127 | + # -- comment numbering starts at 0, and is incremented for each new comment -- |
| 128 | + assert comment.comment_id == 0 |
| 129 | + # -- author is a required attribut, but is the empty string by default -- |
| 130 | + assert comment.author == "" |
| 131 | + # -- initials is an optional attribute, but defaults to the empty string, same as Word -- |
| 132 | + assert comment.initials == "" |
| 133 | + # -- timestamp is also optional, but defaults to now-UTC -- |
| 134 | + assert comment.timestamp is not None |
| 135 | + assert now_before <= comment.timestamp <= now_after |
| 136 | + # -- by default, a new comment contains a single empty paragraph -- |
| 137 | + assert [p.text for p in comment.paragraphs] == [""] |
| 138 | + # -- that paragraph has the "CommentText" style, same as Word applies -- |
| 139 | + comment_elm = comment._comment_elm |
| 140 | + assert len(comment_elm.p_lst) == 1 |
| 141 | + p = comment_elm.p_lst[0] |
| 142 | + assert p.style == "CommentText" |
| 143 | + # -- and that paragraph contains a single run with the necessary annotation reference -- |
| 144 | + assert len(p.r_lst) == 1 |
| 145 | + r = comment_elm.p_lst[0].r_lst[0] |
| 146 | + assert r.style == "CommentReference" |
| 147 | + assert r[-1].tag == qn("w:annotationRef") |
| 148 | + |
| 149 | + def and_it_can_add_text_to_the_comment_when_adding_it(self, comments: Comments, package_: Mock): |
| 150 | + comment = comments.add_comment(text="para 1\n\npara 2") |
| 151 | + |
| 152 | + assert len(comment.paragraphs) == 3 |
| 153 | + assert [p.text for p in comment.paragraphs] == ["para 1", "", "para 2"] |
| 154 | + assert all(p._p.style == "CommentText" for p in comment.paragraphs) |
| 155 | + |
89 | 156 | # -- fixtures -------------------------------------------------------------------------------- |
90 | 157 |
|
| 158 | + @pytest.fixture |
| 159 | + def comments(self, package_: Mock) -> Comments: |
| 160 | + comments_elm = cast(CT_Comments, element("w:comments")) |
| 161 | + comments_part = CommentsPart( |
| 162 | + PackURI("/word/comments.xml"), |
| 163 | + CT.WML_COMMENTS, |
| 164 | + comments_elm, |
| 165 | + package_, |
| 166 | + ) |
| 167 | + return Comments(comments_elm, comments_part) |
| 168 | + |
91 | 169 | @pytest.fixture |
92 | 170 | def package_(self, request: FixtureRequest): |
93 | 171 | return instance_mock(request, Package) |
|
0 commit comments