Skip to content

Commit

Permalink
Merge pull request #62 from cisagov/issue-24/test-report-generator-ge…
Browse files Browse the repository at this point in the history
…t-image

Add Tests for report_generator.get image and update project version
  • Loading branch information
nickviola committed Oct 17, 2023
2 parents 47434db + 7878c85 commit 3810c71
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bump_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -o nounset
set -o errexit
set -o pipefail

VERSION_FILE=src/example/_version.py
VERSION_FILE=src/tpt_reports/_version.py

HELP_INFORMATION="bump_version.sh (show|major|minor|patch|prerelease|build|finalize)"

Expand Down
2 changes: 1 addition & 1 deletion src/tpt_reports/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""This file defines the version of this module."""
__version__ = "0.0.1"
__version__ = "1.0.0"
15 changes: 13 additions & 2 deletions src/tpt_reports/report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,24 @@ def wrap(self, availWidth, availHeight):
return (availWidth, height)


# Issue #24 - test get_image()
# TODO: Add unit tests for following logic and remove this comment.
def get_image(path, width=1 * inch):
"""Read in an image and scale it based on the width argument."""
# Validate arguments
if not isinstance(path, str) or not isinstance(width, (int, float)):
# Raise TypeError if arguments are not the expected type
raise TypeError(
f"'get_image' expects path=(str) and width=(int/float). \
Got path={type(path)}, width={type(width)}."
)

# Read in image and get dimensions
img = utils.ImageReader(path)
img_width, img_height = img.getSize()

# Calculate aspect ratio
aspect = img_height / float(img_width)

# Return an Image object with the calculated height
return Image(path, width=width, height=(width * aspect))


Expand Down
39 changes: 39 additions & 0 deletions tests/test_report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Standard Python Libraries
from datetime import datetime
import json
from unittest import TestCase
from unittest.mock import patch

# Third-Party Libraries
Expand Down Expand Up @@ -86,3 +87,41 @@ def test_report_gen(test_dictionary):
assert result.filename == output_file_path
# Validate the page size of result
assert result.pagesize == (612.0, 792.0)


@patch("tpt_reports.report_generator.utils.ImageReader")
@patch("tpt_reports.report_generator.Image")
class TestGetImage(TestCase):
"""Tests for the get_image function."""

def test_get_image_invalid_params(self, mock_image, mock_image_reader):
"""Test that get_image raises an exception when given invalid arguments."""
# Setup test data
bad_path = 1
bad_width = "not a number"

# Assert that a TypeError is raised
with self.assertRaises(TypeError):
report_generator.get_image(path=bad_path, width=bad_width)

def test_get_image_returns_image(self, mock_image, mock_image_reader):
"""Test that the get_image function returns an Image object."""
# Setup test data
path = "test_path"
width = 72.0

# Mock the getSize method of the ImageReader class (width, height)
mock_image_reader().getSize.return_value = (50, 100)

# Expected aspect value is 2.0 based on getSize (height / width)
# Set the expected height based on (getSize height * aspect value)
expected_height = 144.0

# Run the get_image function with test data
result = report_generator.get_image(path=path, width=width)

# Confirm Image() called with expected args and height calculated
mock_image.assert_called_once_with(path, width=width, height=expected_height)

# Confirm return value is an Image object
assert result == mock_image.return_value

0 comments on commit 3810c71

Please sign in to comment.