Skip to content

Commit

Permalink
test: more edge case testing and fix py version test
Browse files Browse the repository at this point in the history
  • Loading branch information
miikanissi committed Nov 27, 2024
1 parent 0c718b6 commit b7c7500
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
Binary file added tests/static/test_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions tests/test_zebrafy_graphic_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import unittest

# 2. Known third party imports:
from PIL import Image

# 3. Local imports in the relative form:
from zebrafy import GraphicField

Expand Down Expand Up @@ -70,6 +72,40 @@ def test_graphic_field_deprecated_compression_type(self):
gfc = GraphicField(self.test_image, compression_type="C")
self.assertEqual(gfc.format, "Z64")

def test_get_graphic_field(self):
"""Test get_graphic_field method."""
gf = GraphicField(self.test_image)
zpl_string = gf.get_graphic_field()
self.assertTrue(zpl_string.startswith("^GFA"))

def test_get_data_string(self):
"""Test _get_data_string method."""
gf = GraphicField(self.test_image, format="ASCII")
data_string = gf._get_data_string()
self.assertIsInstance(data_string, str)

gf = GraphicField(self.test_image, format="B64")
data_string = gf._get_data_string()
self.assertTrue(data_string.startswith(":B64:"))

gf = GraphicField(self.test_image, format="Z64")
data_string = gf._get_data_string()
self.assertTrue(data_string.startswith(":Z64:"))

def test_edge_cases(self):
"""Test edge cases with different image sizes and color modes."""
small_image = Image.new("1", (1, 1))
gf = GraphicField(small_image)
self.assertEqual(gf._get_graphic_field_count(), 1)

large_image = Image.new("1", (1000, 1000))
gf = GraphicField(large_image)
self.assertEqual(gf._get_graphic_field_count(), 125000)

color_image = Image.new("RGB", (10, 10))
gf = GraphicField(color_image)
self.assertEqual(gf._get_graphic_field_count(), 20)


if __name__ == "__main__":
unittest.main()
23 changes: 23 additions & 0 deletions tests/test_zebrafy_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,29 @@ def test_multiple_image_to_zpl(self):
complete_zpl, self._read_static_file("test_image_multiple.zpl")
)

def test_zebrafy_image_jpeg(self):
"""Test ZebrafyImage with a JPEG image."""
jpeg_image = self._read_static_file("test_image.jpg")
zebrafy_image = ZebrafyImage(jpeg_image)
self.assertEqual(zebrafy_image.format, "ASCII")

def test_zebrafy_image_boundary_threshold(self):
"""Test ZebrafyImage with boundary threshold values."""
zebrafy_image = ZebrafyImage(self.test_image)
zebrafy_image.threshold = 0
self.assertEqual(zebrafy_image.threshold, 0)
zebrafy_image.threshold = 255
self.assertEqual(zebrafy_image.threshold, 255)

def test_zebrafy_image_combined_parameters(self):
"""Test ZebrafyImage with combined parameters."""
zebrafy_image = ZebrafyImage(
self.test_image, invert=True, dither=False, threshold=100
)
self.assertTrue(zebrafy_image.invert)
self.assertFalse(zebrafy_image.dither)
self.assertEqual(zebrafy_image.threshold, 100)


if __name__ == "__main__":
unittest.main()
11 changes: 0 additions & 11 deletions tests/test_zebrafy_zpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,6 @@ def test_z64_zpl_to_pdf(self):
class TestZebrafyZPLImports(TestZebrafyCommonBase):
"""Test ZebrafyZPL imports."""

@patch("sys.version_info", (3, 9))
def test_imports_python_39_or_higher(self):
"""Test ZebrafyZPL imports for Python 3.9 or higher."""
import importlib

importlib.reload(sys.modules["zebrafy.zebrafy_zpl"])
from zebrafy.zebrafy_zpl import DimensionsType, ToImagesType

self.assertEqual(DimensionsType, tuple[int, int])
self.assertEqual(ToImagesType, list[Image.Image])

@patch("sys.version_info", (3, 8))
def test_imports_python_38_or_lower(self):
"""Test ZebrafyZPL imports for Python 3.8 or lower."""
Expand Down

0 comments on commit b7c7500

Please sign in to comment.