Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7cf36d6

Browse files
committed
xfail: acceptance test for Comment properties
1 parent 0eeaa2f commit 7cf36d6

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

features/cmt-props.feature

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Feature: Get comment properties
2+
In order to characterize comments by their metadata
3+
As a developer using python-docx
4+
I need methods to access comment metadata properties
5+
6+
7+
Scenario: Comment.id
8+
Given a Comment object
9+
Then comment.comment_id is the comment identifier
10+
11+
12+
@wip
13+
Scenario: Comment.author
14+
Given a Comment object
15+
Then comment.author is the author of the comment
16+
17+
18+
@wip
19+
Scenario: Comment.initials
20+
Given a Comment object
21+
Then comment.initials is the initials of the comment author
22+
23+
24+
@wip
25+
Scenario: Comment.timestamp
26+
Given a Comment object
27+
Then comment.timestamp is the date and time the comment was authored
28+
29+
30+
@wip
31+
Scenario: Comment.paragraphs[0].text
32+
Given a Comment object
33+
When I assign para_text = comment.paragraphs[0].text
34+
Then para_text is the text of the first paragraph in the comment
35+
36+
37+
@wip
38+
Scenario: Retrieve embedded image from a comment
39+
Given a Comment object containing an embedded image
40+
Then I can extract the image from the comment

features/steps/comments.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
"""Step implementations for document comments-related features."""
22

3+
import datetime as dt
4+
35
from behave import given, then, when
46
from behave.runner import Context
57

68
from docx import Document
79
from docx.comments import Comment, Comments
10+
from docx.drawing import Drawing
811

912
from 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")
1528
def 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)")
3452
def 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")
4282
def 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")
48102
def 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")
66127
def then_the_result_is_a_comment_object_with_id_2(context: Context):
67128
comment = context.comment

0 commit comments

Comments
 (0)