Skip to content

Commit

Permalink
MAINT: address deprecation warnings in unit tests (#691)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Lutz <romanlutz13@gmail.com>
  • Loading branch information
paulinek13 and romanlutz authored Feb 12, 2025
1 parent d962a24 commit 6d07f5b
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 31 deletions.
6 changes: 3 additions & 3 deletions doc/code/converters/pdf_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
# Initialize the PDFConverter
pdf_converter = PDFConverter(
prompt_template=prompt_template,
font_type="Arial",
font_type="Helvetica",
font_size=12,
page_width=210,
page_height=297,
Expand Down Expand Up @@ -102,7 +102,7 @@
# Initialize the PDFConverter without a template
pdf_converter = PDFConverter(
prompt_template=None, # No template provided
font_type="Arial",
font_type="Helvetica",
font_size=12,
page_width=210,
page_height=297,
Expand Down Expand Up @@ -177,7 +177,7 @@
# Initialize the PDFConverter with the existing PDF and injection items
pdf_converter = PDFConverter(
prompt_template=None, # No template provided
font_type="Arial",
font_type="Helvetica",
font_size=12,
page_width=210,
page_height=297,
Expand Down
2 changes: 1 addition & 1 deletion pyrit/models/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def save_to_file(self, directory_path: Path) -> str:
Returns:
The full path to the file that was saved
"""
embedding_json = self.json()
embedding_json = self.model_dump_json()
embedding_hash = sha256(embedding_json.encode()).hexdigest()
embedding_output_file_path = Path(directory_path, f"{embedding_hash}.json")
embedding_output_file_path.write_text(embedding_json)
Expand Down
2 changes: 1 addition & 1 deletion pyrit/models/prompt_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def save_to_file(self, directory_path: Path) -> str:
Returns:
The full path to the file that was saved
"""
embedding_json = self.json()
embedding_json = self.model_dump_json()
embedding_hash = hashlib.sha256(embedding_json.encode()).hexdigest()
embedding_output_file_path = Path(directory_path, f"{embedding_hash}.json")
embedding_output_file_path.write_text(embedding_json)
Expand Down
4 changes: 2 additions & 2 deletions pyrit/prompt_converter/add_image_text_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AddImageTextConverter(PromptConverter):
Args:
img_to_add (str): File path of image to add text to
font_name (str, Optional): Path of font to use. Must be a TrueType font (.ttf). Defaults to "arial.ttf".
font_name (str, Optional): Path of font to use. Must be a TrueType font (.ttf). Defaults to "helvetica.ttf".
color (tuple, Optional): Color to print text in, using RGB values. Defaults to (0, 0, 0).
font_size (float, Optional): Size of font to use. Defaults to 15.
x_pos (int, Optional): X coordinate to place text in (0 is left most). Defaults to 10.
Expand All @@ -34,7 +34,7 @@ class AddImageTextConverter(PromptConverter):
def __init__(
self,
img_to_add: str,
font_name: Optional[str] = "arial.ttf",
font_name: Optional[str] = "helvetica.ttf",
color: Optional[tuple[int, int, int]] = (0, 0, 0),
font_size: Optional[int] = 15,
x_pos: Optional[int] = 10,
Expand Down
4 changes: 2 additions & 2 deletions pyrit/prompt_converter/add_text_image_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AddTextImageConverter(PromptConverter):
Args:
text_to_add (str): Text to add to an image. Defaults to empty string.
font_name (str, Optional): Path of font to use. Must be a TrueType font (.ttf). Defaults to "arial.ttf".
font_name (str, Optional): Path of font to use. Must be a TrueType font (.ttf). Defaults to "helvetica.ttf".
color (tuple, Optional): Color to print text in, using RGB values. Defaults to (0, 0, 0).
font_size (float, Optional): Size of font to use. Defaults to 15.
x_pos (int, Optional): X coordinate to place text in (0 is left most). Defaults to 10.
Expand All @@ -32,7 +32,7 @@ class AddTextImageConverter(PromptConverter):
def __init__(
self,
text_to_add: str,
font_name: Optional[str] = "arial.ttf",
font_name: Optional[str] = "helvetica.ttf",
color: Optional[tuple[int, int, int]] = (0, 0, 0),
font_size: Optional[int] = 15,
x_pos: Optional[int] = 10,
Expand Down
4 changes: 2 additions & 2 deletions pyrit/prompt_converter/pdf_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PDFConverter(PromptConverter):
Args:
prompt_template (Optional[SeedPrompt], optional): A `SeedPrompt` object representing a template.
font_type (Optional[str], optional): Font type for the PDF. Defaults to "Arial".
font_type (Optional[str], optional): Font type for the PDF. Defaults to "Helvetica".
font_size (Optional[int], optional): Font size for the PDF. Defaults to 12.
font_color (Optional[tuple], optional): Font color for the PDF in RGB format. Defaults to (255, 255, 255).
page_width (Optional[int], optional): Width of the PDF page in mm. Defaults to 210 (A4 width).
Expand All @@ -40,7 +40,7 @@ class PDFConverter(PromptConverter):
def __init__(
self,
prompt_template: Optional[SeedPrompt] = None,
font_type: Optional[str] = "Arial",
font_type: Optional[str] = "Helvetica",
font_size: Optional[int] = 12,
font_color: Optional[tuple] = (255, 255, 255),
page_width: Optional[int] = 210,
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/converter/test_add_image_text_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def image_text_converter_sample_image():
def test_add_image_text_converter_initialization(image_text_converter_sample_image):
converter = AddImageTextConverter(
img_to_add=image_text_converter_sample_image,
font_name="arial.ttf",
font_name="helvetica.ttf",
color=(255, 255, 255),
font_size=20,
x_pos=10,
y_pos=10,
)
assert converter._img_to_add == "test.png"
assert converter._font_name == "arial.ttf"
assert converter._font_name == "helvetica.ttf"
assert converter._color == (255, 255, 255)
assert converter._font_size == 20
assert converter._x_pos == 10
Expand All @@ -39,14 +39,14 @@ def test_add_image_text_converter_initialization(image_text_converter_sample_ima
def test_add_image_text_converter_invalid_font(image_text_converter_sample_image):
with pytest.raises(ValueError):
AddImageTextConverter(
img_to_add=image_text_converter_sample_image, font_name="arial.otf"
img_to_add=image_text_converter_sample_image, font_name="helvetica.otf"
) # Invalid font extension
os.remove("test.png")


def test_add_image_text_converter_null_img_to_add():
with pytest.raises(ValueError):
AddImageTextConverter(img_to_add="", font_name="arial.ttf")
AddImageTextConverter(img_to_add="", font_name="helvetica.ttf")


def test_add_image_text_converter_fallback_to_default_font(image_text_converter_sample_image, caplog):
Expand All @@ -66,7 +66,7 @@ def test_add_image_text_converter_fallback_to_default_font(image_text_converter_

def test_image_text_converter_add_text_to_image(image_text_converter_sample_image):
converter = AddImageTextConverter(
img_to_add=image_text_converter_sample_image, font_name="arial.ttf", color=(255, 255, 255)
img_to_add=image_text_converter_sample_image, font_name="helvetica.ttf", color=(255, 255, 255)
)
image = Image.open("test.png")
pixels_before = list(image.getdata())
Expand All @@ -88,7 +88,7 @@ async def test_add_image_text_converter_invalid_input_text(image_text_converter_

@pytest.mark.asyncio
async def test_add_image_text_converter_invalid_file_path():
converter = AddImageTextConverter(img_to_add="nonexistent_image.png", font_name="arial.ttf")
converter = AddImageTextConverter(img_to_add="nonexistent_image.png", font_name="helvetica.ttf")
with pytest.raises(FileNotFoundError):
assert await converter.convert_async(prompt="Sample Text!", input_type="text") # type: ignore

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/converter/test_add_text_image_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def text_image_converter_sample_image_bytes():

def test_add_text_image_converter_initialization():
converter = AddTextImageConverter(
text_to_add="Sample text", font_name="arial.ttf", color=(255, 255, 255), font_size=20, x_pos=10, y_pos=10
text_to_add="Sample text", font_name="helvetica.ttf", color=(255, 255, 255), font_size=20, x_pos=10, y_pos=10
)
assert converter._text_to_add == "Sample text"
assert converter._font_name == "arial.ttf"
assert converter._font_name == "helvetica.ttf"
assert converter._color == (255, 255, 255)
assert converter._font_size == 20
assert converter._x_pos == 10
Expand All @@ -36,12 +36,12 @@ def test_add_text_image_converter_initialization():

def test_add_text_image_converter_invalid_font():
with pytest.raises(ValueError):
AddTextImageConverter(text_to_add="Sample text", font_name="arial.otf") # Invalid font extension
AddTextImageConverter(text_to_add="Sample text", font_name="helvetica.otf") # Invalid font extension


def test_add_text_image_converter_invalid_text_to_add():
with pytest.raises(ValueError):
AddTextImageConverter(text_to_add="", font_name="arial.ttf")
AddTextImageConverter(text_to_add="", font_name="helvetica.ttf")


def test_add_text_image_converter_fallback_to_default_font(text_image_converter_sample_image_bytes, caplog):
Expand All @@ -64,7 +64,7 @@ def test_add_text_image_converter_fallback_to_default_font(text_image_converter_


def test_text_image_converter_add_text_to_image(text_image_converter_sample_image_bytes):
converter = AddTextImageConverter(text_to_add="Hello, World!", font_name="arial.ttf", color=(255, 255, 255))
converter = AddTextImageConverter(text_to_add="Hello, World!", font_name="helvetica.ttf", color=(255, 255, 255))
image = Image.open(BytesIO(text_image_converter_sample_image_bytes))
pixels_before = list(image.getdata())
updated_image = converter._add_text_to_image(image)
Expand Down
19 changes: 10 additions & 9 deletions tests/unit/converter/test_pdf_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest
from fpdf import FPDF
from fpdf.enums import XPos, YPos
from pypdf import PageObject, PdfReader

from pyrit.models import DataTypeSerializer, SeedPrompt
Expand Down Expand Up @@ -168,10 +169,10 @@ def mock_pdf_path(tmp_path):
# Create a multi-page PDF
pdf = FPDF(format="A4")
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="Page 1 content", ln=True, align="L")
pdf.set_font("Helvetica", size=12)
pdf.cell(200, 10, text="Page 1 content", new_x=XPos.LMARGIN, new_y=YPos.NEXT, align="L")
pdf.add_page()
pdf.cell(200, 10, txt="Page 2 content", ln=True, align="L")
pdf.cell(200, 10, text="Page 2 content", new_x=XPos.LMARGIN, new_y=YPos.NEXT, align="L")

pdf_bytes = BytesIO()
pdf.output(pdf_bytes)
Expand Down Expand Up @@ -286,7 +287,7 @@ def test_inject_text_negative_x(dummy_page):
x=-10,
y=100,
text="Test Text",
font="Arial",
font="Helvetica",
font_size=12,
font_color=(0, 0, 0),
)
Expand All @@ -302,7 +303,7 @@ def test_inject_text_x_exceeds_page_width(dummy_page):
x=600, # 600 exceeds the A4 width of 595
y=100,
text="Test Text",
font="Arial",
font="Helvetica",
font_size=12,
font_color=(0, 0, 0),
)
Expand All @@ -318,7 +319,7 @@ def test_inject_text_negative_y(dummy_page):
x=100,
y=-20,
text="Test Text",
font="Arial",
font="Helvetica",
font_size=12,
font_color=(0, 0, 0),
)
Expand All @@ -334,7 +335,7 @@ def test_inject_text_y_exceeds_page_height(dummy_page):
x=100,
y=850, # 850 exceeds the A4 height of 842
text="Test Text",
font="Arial",
font="Helvetica",
font_size=12,
font_color=(0, 0, 0),
)
Expand All @@ -345,8 +346,8 @@ def test_pdf_reader_repeated_access():
# Create a simple PDF in memory
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="Test Content", ln=True)
pdf.set_font("Helvetica", size=12)
pdf.cell(200, 10, text="Test Content", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
pdf_bytes = BytesIO()
pdf.output(pdf_bytes)
pdf_bytes.seek(0)
Expand Down

0 comments on commit 6d07f5b

Please # to comment.