Skip to content

Commit 20ef6c5

Browse files
author
Steve Canny
committed
acpt: add img-characterize-image.feature
Direct image operations are not strictly speaking part of the API, but an acceptance/integration test will help drive the development and be handy later as an integration test.
1 parent 87460cc commit 20ef6c5

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Feature: Characterize an image file
2+
In order add a picture to a document
3+
As a programmer using the advanced python-docx API
4+
I need a way to determine the image content type and size
5+
6+
@wip
7+
Scenario Outline: Characterize an image file
8+
Given the image file '<filename>'
9+
When I construct an image using the image path
10+
Then the image has content type '<mime_type>'
11+
And the image is <cx> pixels wide
12+
And the image is <cy> pixels high
13+
And the image has <horz_dpi> horizontal dpi
14+
And the image has <vert_dpi> vertical dpi
15+
16+
Examples: Image file characteristics
17+
| filename | mime_type | cx | cy | horz_dpi | vert_dpi |
18+
| test.png | image/png | 11 | 22 | 333 | 444 |

features/steps/image.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Step implementations for image characterization features
5+
"""
6+
7+
from __future__ import absolute_import, print_function, unicode_literals
8+
9+
from behave import given, then, when
10+
11+
from docx.image.image import Image
12+
13+
from helpers import test_file_path
14+
15+
16+
# given ===================================================
17+
18+
@given('the image file \'{filename}\'')
19+
def given_image_filename(context, filename):
20+
context.image_path = test_file_path(filename)
21+
22+
23+
# when ====================================================
24+
25+
@when('I construct an image using the image path')
26+
def when_construct_image_using_path(context):
27+
context.image = Image.from_file(context.image_path)
28+
29+
30+
# then ====================================================
31+
32+
@then('the image has content type \'{mime_type}\'')
33+
def then_image_has_content_type(context, mime_type):
34+
content_type = context.image.content_type
35+
assert content_type == mime_type, (
36+
"expected MIME type '%s', got '%s'" % (mime_type, content_type)
37+
)
38+
39+
40+
@then('the image has {horz_dpi_str} horizontal dpi')
41+
def then_image_has_horizontal_dpi(context, horz_dpi_str):
42+
expected_horz_dpi = int(horz_dpi_str)
43+
horz_dpi = context.image.horz_dpi
44+
assert horz_dpi == expected_horz_dpi, (
45+
"expected horizontal dpi %d, got %d" % (expected_horz_dpi, horz_dpi)
46+
)
47+
48+
49+
@then('the image has {vert_dpi_str} vertical dpi')
50+
def then_image_has_vertical_dpi(context, vert_dpi_str):
51+
expected_vert_dpi = int(vert_dpi_str)
52+
vert_dpi = context.image.vert_dpi
53+
assert vert_dpi == expected_vert_dpi, (
54+
"expected vertical dpi %d, got %d" % (expected_vert_dpi, vert_dpi)
55+
)
56+
57+
58+
@then('the image is {px_height_str} pixels high')
59+
def then_image_is_cx_pixels_high(context, px_height_str):
60+
expected_px_height = int(px_height_str)
61+
px_height = context.image.px_height
62+
assert px_height == expected_px_height, (
63+
"expected pixel height %d, got %d" % (expected_px_height, px_height)
64+
)
65+
66+
67+
@then('the image is {px_width_str} pixels wide')
68+
def then_image_is_cx_pixels_wide(context, px_width_str):
69+
expected_px_width = int(px_width_str)
70+
px_width = context.image.px_width
71+
assert px_width == expected_px_width, (
72+
"expected pixel width %d, got %d" % (expected_px_width, px_width)
73+
)

features/steps/test_files/test.png

143 KB
Loading

0 commit comments

Comments
 (0)