Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring of some tests #233

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/test_checksums.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_ean14_checksum() -> None:

def test_isbn10_checksum() -> None:
isbn = get_barcode("isbn10", "376926085")
assert isbn.isbn10 == "3769260856"
assert isbn.isbn10 == "3769260856" # type: ignore[attr-defined]


def test_isbn13_checksum() -> None:
Expand Down
60 changes: 36 additions & 24 deletions tests/test_manually.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import codecs
import os
from typing import Iterator

import pytest

from barcode import get_barcode
from barcode import get_barcode_class
Expand Down Expand Up @@ -50,42 +53,51 @@
)


def test_generating_barcodes() -> None:
@pytest.mark.parametrize(("codename", "code"), TESTCODES)
def test_generating_barcodes(
codename: str, code: str, gather_image_elements_into_html: list[str]
) -> None:
os.makedirs(TESTPATH, exist_ok=True)

objects = []
image_elements = gather_image_elements_into_html

def append(x, y) -> None:
objects.append(OBJECTS.format(filename=x, name=y))
image_elements.append(OBJECTS.format(filename=x, name=y))

def append_img(x, y) -> None:
objects.append(IMAGES.format(filename=x, name=y))
image_elements.append(IMAGES.format(filename=x, name=y))

options = {}
for codename, code in TESTCODES:
bcode = get_barcode(codename, code)
bcode = get_barcode(codename, code)
if codename.startswith("i"):
options["center_text"] = False
else:
options["center_text"] = True
filename = bcode.save(os.path.join(TESTPATH, codename), options=options)
print(f"Code: {bcode.name}, Input: {code}, Output: {bcode.get_fullcode()}")
append(os.path.basename(filename), bcode.name)
if ImageWriter is not None:
bcodec = get_barcode_class(codename)
bcode = bcodec(code, writer=ImageWriter())
opts = {}
if codename.startswith("i"):
options["center_text"] = False
else:
options["center_text"] = True
filename = bcode.save(os.path.join(TESTPATH, codename), options=options)
print(f"Code: {bcode.name}, Input: {code}, Output: {bcode.get_fullcode()}")
append(os.path.basename(filename), bcode.name)
if ImageWriter is not None:
bcodec = get_barcode_class(codename)
bcode = bcodec(code, writer=ImageWriter())
opts = {}
if codename.startswith("i"):
opts["center_text"] = False
else:
opts["center_text"] = True
filename = bcode.save(os.path.join(TESTPATH, codename), options=opts)
append_img(os.path.basename(filename), bcode.name)
opts["center_text"] = False
else:
objects.append(NO_PIL)
opts["center_text"] = True
filename = bcode.save(os.path.join(TESTPATH, codename), options=opts)
append_img(os.path.basename(filename), bcode.name)
else:
image_elements.append(NO_PIL)


@pytest.fixture(scope="module")
def gather_image_elements_into_html() -> Iterator[list[str]]:
image_elements: list[str] = []
yield image_elements

# Save htmlfile with all objects
with codecs.open(HTMLFILE, "w", encoding="utf-8") as f:
obj = "\n".join(objects)
obj = "\n".join(image_elements)
f.write(HTML.format(version=version, body=obj))

print(f"\nNow open {HTMLFILE} in your browser.")
Loading