1
1
"""Step implementations for document comments-related features."""
2
2
3
+ import datetime as dt
4
+
3
5
from behave import given , then , when
4
6
from behave .runner import Context
5
7
6
8
from docx import Document
7
9
from docx .comments import Comment , Comments
10
+ from docx .drawing import Drawing
8
11
9
12
from helpers import test_docx
10
13
11
14
# given ====================================================
12
15
13
16
17
+ @given ("a Comment object" )
18
+ def given_a_comment_object (context : Context ):
19
+ context .comment = Document (test_docx ("comments-rich-para" )).comments .get (0 )
20
+
21
+
22
+ @given ("a Comment object containing an embedded image" )
23
+ def given_a_comment_object_containing_an_embedded_image (context : Context ):
24
+ context .comment = Document (test_docx ("comments-rich-para" )).comments .get (1 )
25
+
26
+
14
27
@given ("a Comments object with {count} comments" )
15
28
def given_a_comments_object_with_count_comments (context : Context , count : str ):
16
29
testfile_name = {"0" : "doc-default" , "4" : "comments-rich-para" }[count ]
@@ -30,6 +43,11 @@ def given_a_document_having_no_comments_part(context: Context):
30
43
# when =====================================================
31
44
32
45
46
+ @when ("I assign para_text = comment.paragraphs[0].text" )
47
+ def when_I_assign_para_text (context : Context ):
48
+ context .para_text = context .comment .paragraphs [0 ].text
49
+
50
+
33
51
@when ("I call comments.get(2)" )
34
52
def when_I_call_comments_get_2 (context : Context ):
35
53
context .comment = context .comments .get (2 )
@@ -38,12 +56,48 @@ def when_I_call_comments_get_2(context: Context):
38
56
# then =====================================================
39
57
40
58
59
+ @then ("comment.author is the author of the comment" )
60
+ def then_comment_author_is_the_author_of_the_comment (context : Context ):
61
+ actual = context .comment .author
62
+ assert actual == "Steve Canny" , f"expected author 'Steve Canny', got '{ actual } '"
63
+
64
+
65
+ @then ("comment.comment_id is the comment identifier" )
66
+ def then_comment_comment_id_is_the_comment_identifier (context : Context ):
67
+ assert context .comment .comment_id == 0
68
+
69
+
70
+ @then ("comment.initials is the initials of the comment author" )
71
+ def then_comment_initials_is_the_initials_of_the_comment_author (context : Context ):
72
+ initials = context .comment .initials
73
+ assert initials == "SJC" , f"expected initials 'SJC', got '{ initials } '"
74
+
75
+
76
+ @then ("comment.timestamp is the date and time the comment was authored" )
77
+ def then_comment_timestamp_is_the_date_and_time_the_comment_was_authored (context : Context ):
78
+ assert context .comment .timestamp == dt .datetime (2025 , 6 , 7 , 11 , 20 , 0 , tzinfo = dt .timezone .utc )
79
+
80
+
41
81
@then ("document.comments is a Comments object" )
42
82
def then_document_comments_is_a_Comments_object (context : Context ):
43
83
document = context .document
44
84
assert type (document .comments ) is Comments
45
85
46
86
87
+ @then ("I can extract the image from the comment" )
88
+ def then_I_can_extract_the_image_from_the_comment (context : Context ):
89
+ paragraph = context .comment .paragraphs [0 ]
90
+ run = paragraph .runs [2 ]
91
+ drawing = next (d for d in run .iter_inner_content () if isinstance (d , Drawing ))
92
+ assert drawing .has_picture
93
+
94
+ image = drawing .image
95
+
96
+ assert image .content_type == "image/jpeg" , f"got { image .content_type } "
97
+ assert image .filename == "image.jpg" , f"got { image .filename } "
98
+ assert image .sha1 == "1be010ea47803b00e140b852765cdf84f491da47" , f"got { image .sha1 } "
99
+
100
+
47
101
@then ("iterating comments yields {count} Comment objects" )
48
102
def then_iterating_comments_yields_count_comments (context : Context , count : str ):
49
103
comment_iter = iter (context .comments )
@@ -62,6 +116,13 @@ def then_len_comments_eq_count(context: Context, count: str):
62
116
assert actual == expected , f"expected len(comments) of { expected } , got { actual } "
63
117
64
118
119
+ @then ("para_text is the text of the first paragraph in the comment" )
120
+ def then_para_text_is_the_text_of_the_first_paragraph_in_the_comment (context : Context ):
121
+ actual = context .para_text
122
+ expected = "Text with hyperlink https://google.com embedded."
123
+ assert actual == expected , f"expected para_text '{ expected } ', got '{ actual } '"
124
+
125
+
65
126
@then ("the result is a Comment object with id 2" )
66
127
def then_the_result_is_a_comment_object_with_id_2 (context : Context ):
67
128
comment = context .comment
0 commit comments