11"""Step implementations for document comments-related features."""
22
3+ import datetime as dt
4+
35from behave import given , then , when
46from behave .runner import Context
57
68from docx import Document
79from docx .comments import Comment , Comments
10+ from docx .drawing import Drawing
811
912from helpers import test_docx
1013
1114# given ====================================================
1215
1316
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+
1427@given ("a Comments object with {count} comments" )
1528def given_a_comments_object_with_count_comments (context : Context , count : str ):
1629 testfile_name = {"0" : "doc-default" , "4" : "comments-rich-para" }[count ]
@@ -30,6 +43,11 @@ def given_a_document_having_no_comments_part(context: Context):
3043# when =====================================================
3144
3245
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+
3351@when ("I call comments.get(2)" )
3452def when_I_call_comments_get_2 (context : Context ):
3553 context .comment = context .comments .get (2 )
@@ -38,12 +56,48 @@ def when_I_call_comments_get_2(context: Context):
3856# then =====================================================
3957
4058
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+
4181@then ("document.comments is a Comments object" )
4282def then_document_comments_is_a_Comments_object (context : Context ):
4383 document = context .document
4484 assert type (document .comments ) is Comments
4585
4686
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+
47101@then ("iterating comments yields {count} Comment objects" )
48102def then_iterating_comments_yields_count_comments (context : Context , count : str ):
49103 comment_iter = iter (context .comments )
@@ -62,6 +116,13 @@ def then_len_comments_eq_count(context: Context, count: str):
62116 assert actual == expected , f"expected len(comments) of { expected } , got { actual } "
63117
64118
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+
65126@then ("the result is a Comment object with id 2" )
66127def then_the_result_is_a_comment_object_with_id_2 (context : Context ):
67128 comment = context .comment
0 commit comments