From 2c4f4d53a8634f6bc187e2c14dc79993db937253 Mon Sep 17 00:00:00 2001 From: Neil Johnson Date: Thu, 11 Aug 2022 11:49:45 -0400 Subject: [PATCH 1/2] Implement logging; expand tests for make_ballots --- src/electos/ballotmaker/cli.py | 14 ++++++++++---- src/electos/ballotmaker/make_ballots.py | 7 +++++++ src/electos/ballotmaker/validate_edf.py | 7 ++++++- tests/test_cli.py | 2 +- tests/test_make_ballots.py | 11 +++++++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/electos/ballotmaker/cli.py b/src/electos/ballotmaker/cli.py index 7006395..ec9a832 100644 --- a/src/electos/ballotmaker/cli.py +++ b/src/electos/ballotmaker/cli.py @@ -1,4 +1,6 @@ """ The BallotMaker Command Line Interface (CLI)""" +import logging +import sys from os import strerror from pathlib import Path from typing import Optional @@ -12,6 +14,10 @@ STYLE_HELP = "Stylesheet file for ballot generation" VERSION_HELP = "Print the version number." +# configure logging +logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) +log = logging.getLogger(__name__) + def version_callback(value: bool): if value: @@ -45,8 +51,8 @@ def make( """Make ballots from EDF file""" make_ballots_result = make_ballots.make_ballots(edf, output_dir, style) if make_ballots_result != NO_ERRORS: - typer.echo( - f"Error {make_ballots_result} making ballots: {strerror(make_ballots_result)}" + log.error( + f"Code {make_ballots_result} in make - {strerror(make_ballots_result)}" ) return make_ballots_result @@ -61,8 +67,8 @@ def validate( """Validate data in EDF file""" validate_edf_result = validate_edf.validate_edf(edf) if validate_edf_result != NO_ERRORS: - typer.echo( - f"Error {validate_edf_result} validating EDF: {strerror(validate_edf_result)}" + log.error( + f"Code {validate_edf_result} in validate - {strerror(validate_edf_result)}" ) return validate_edf_result diff --git a/src/electos/ballotmaker/make_ballots.py b/src/electos/ballotmaker/make_ballots.py index dbf8efb..4d5286e 100644 --- a/src/electos/ballotmaker/make_ballots.py +++ b/src/electos/ballotmaker/make_ballots.py @@ -1,7 +1,10 @@ +import logging from pathlib import Path from electos.ballotmaker.constants import NO_ERRORS, NO_FILE +log = logging.getLogger(__name__) + def make_ballots( _edf: Path, _output_dir: Path = None, _styles: Path = None @@ -15,6 +18,10 @@ def make_ballots( """ # is the EDF a JSON file? if _edf is None: + log.debug("No EDF file provided.") + return NO_FILE + if Path.is_file(_edf) is False: + log.debug(f"{_edf} is not a file") return NO_FILE # was a valid output directory provided? # was a styles file provided? diff --git a/src/electos/ballotmaker/validate_edf.py b/src/electos/ballotmaker/validate_edf.py index f6ed7e9..df44f13 100644 --- a/src/electos/ballotmaker/validate_edf.py +++ b/src/electos/ballotmaker/validate_edf.py @@ -1,8 +1,11 @@ +import logging from pathlib import Path from electos.ballotmaker.constants import NO_DATA, NO_ERRORS, NO_FILE from electos.ballotmaker.read_edf import read_edf +log = logging.getLogger(__name__) + def validate_edf( _edf: Path, @@ -13,10 +16,12 @@ def validate_edf( """ # is the EDF a file? if _edf is None: + log.debug("No EDF file provided.") return NO_FILE if Path.is_file(_edf) is False: + log.debug(f"{_edf} is not a file") return NO_FILE ballot_style_count = read_edf(_edf) - print(f"Found {ballot_style_count} ballots in {_edf}") + log.info(f"Found {ballot_style_count} ballots in {_edf}") return NO_ERRORS diff --git a/tests/test_cli.py b/tests/test_cli.py index 36279af..2dc2a83 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -35,7 +35,7 @@ def test_make(): # bypass mandatory CLI option to force error assert cli.make(edf=None) == NO_FILE # any old path will satisfy current tests - assert cli.make(imaginary_file) == NO_ERRORS + assert cli.make(imaginary_file) == NO_FILE # check CLI errors: no options for make result = runner.invoke(cli.app, ["make"]) assert result.exit_code == NO_FILE diff --git a/tests/test_make_ballots.py b/tests/test_make_ballots.py index 3fe9e5a..4ac6a72 100644 --- a/tests/test_make_ballots.py +++ b/tests/test_make_ballots.py @@ -3,10 +3,17 @@ from electos.ballotmaker import make_ballots from electos.ballotmaker.constants import NO_ERRORS, NO_FILE -test_file = Path("empty.json") +not_a_file = Path("not_a_file.json") +test_dir = Path(__file__).parent.resolve() +test_file = Path("june_test_case.json") +full_test_path = Path(test_dir, test_file) def test_make_ballots(): # force a no file error with Path = None assert make_ballots.make_ballots(_edf=None) == NO_FILE - assert make_ballots.make_ballots(test_file) == NO_ERRORS + # ensure not_a_file is actually not a file + assert Path.is_file(not_a_file) == False + assert make_ballots.make_ballots(not_a_file) == NO_FILE + assert Path.is_file(full_test_path) + assert make_ballots.make_ballots(full_test_path) == NO_ERRORS From 3cde731d8637c015a56880bed92e712a281f7d26 Mon Sep 17 00:00:00 2001 From: Neil Johnson Date: Fri, 12 Aug 2022 10:15:10 -0400 Subject: [PATCH 2/2] Use preferred is_file and import sytax --- src/electos/ballotmaker/make_ballots.py | 8 ++++---- src/electos/ballotmaker/validate_edf.py | 4 ++-- tests/test_make_ballots.py | 16 ++++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/electos/ballotmaker/make_ballots.py b/src/electos/ballotmaker/make_ballots.py index 4d5286e..678863a 100644 --- a/src/electos/ballotmaker/make_ballots.py +++ b/src/electos/ballotmaker/make_ballots.py @@ -16,12 +16,12 @@ def make_ballots( Output directory for generated PDF files Styles file for ballot formatting """ - # is the EDF a JSON file? + # is the EDF a file? if _edf is None: - log.debug("No EDF file provided.") + log.debug("No EDF file provided to make ballots.") return NO_FILE - if Path.is_file(_edf) is False: - log.debug(f"{_edf} is not a file") + if not _edf.is_file(): + log.debug(f"Can't make ballots, EDF {_edf} is not a file") return NO_FILE # was a valid output directory provided? # was a styles file provided? diff --git a/src/electos/ballotmaker/validate_edf.py b/src/electos/ballotmaker/validate_edf.py index 0e20843..8740a24 100644 --- a/src/electos/ballotmaker/validate_edf.py +++ b/src/electos/ballotmaker/validate_edf.py @@ -16,10 +16,10 @@ def validate_edf( """ # is the EDF a file? if _edf is None: - log.debug("No EDF file provided.") + log.debug("No EDF file provided for validation.") return NO_FILE if not _edf.is_file(): - log.debug(f"{_edf} is not a file") + log.debug(f"Can't validate, EDF {_edf} is not a file") return NO_FILE ballot_style_count = read_edf(_edf) diff --git a/tests/test_make_ballots.py b/tests/test_make_ballots.py index 4ac6a72..7163e5f 100644 --- a/tests/test_make_ballots.py +++ b/tests/test_make_ballots.py @@ -1,9 +1,9 @@ from pathlib import Path -from electos.ballotmaker import make_ballots from electos.ballotmaker.constants import NO_ERRORS, NO_FILE +from electos.ballotmaker.make_ballots import make_ballots -not_a_file = Path("not_a_file.json") +imaginary_file = Path("imaginary_file.json") test_dir = Path(__file__).parent.resolve() test_file = Path("june_test_case.json") full_test_path = Path(test_dir, test_file) @@ -11,9 +11,9 @@ def test_make_ballots(): # force a no file error with Path = None - assert make_ballots.make_ballots(_edf=None) == NO_FILE - # ensure not_a_file is actually not a file - assert Path.is_file(not_a_file) == False - assert make_ballots.make_ballots(not_a_file) == NO_FILE - assert Path.is_file(full_test_path) - assert make_ballots.make_ballots(full_test_path) == NO_ERRORS + assert make_ballots(_edf=None) == NO_FILE + # ensure imaginary_file is actually not a file + assert not imaginary_file.is_file() + assert make_ballots(imaginary_file) == NO_FILE + assert full_test_path.is_file() + assert make_ballots(full_test_path) == NO_ERRORS