From 7abf3c71d72ce84bb39a214dd88d92ae7376b42a Mon Sep 17 00:00:00 2001 From: Martin Domke Date: Thu, 21 Mar 2024 13:49:54 +0100 Subject: [PATCH] Add CLI tests --- tests/test_cli.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++ ulid/__init__.py | 4 +-- ulid/__main__.py | 48 ++++++++++++++--------------- 3 files changed, 102 insertions(+), 27 deletions(-) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..6795a86 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,77 @@ +import pytest + +import ulid.__main__ as cli +from ulid import ULID + + +@pytest.mark.parametrize("option", ["", "uuid", "uuid4", "hex", "int", "timestamp", "datetime"]) +def test_parse_show(option: str): + ulid = ULID() + argv = ["show", f"--{option}", str(ulid)] + output = cli.main(argv) + if option == "uuid": + assert output == str(ulid.to_uuid()) + elif option == "uuid4": + assert output == str(ulid.to_uuid4()) + elif option == "hex": + assert output == ulid.hex + elif option == "int": + assert output == str(int(ulid)) + elif option == "timestamp": + assert output == str(ulid.timestamp) + elif option == "datetime": + assert output == ulid.datetime.isoformat() + else: + assert str(ulid) in output + assert ulid.hex in output + assert str(int(ulid)) in output + assert str(ulid.timestamp) in output + assert str(ulid.datetime.isoformat()) in output + + +@pytest.mark.parametrize( + "option", + [ + "", + "from-uuid", + "from-str", + "from-hex", + "from-int", + "from-timestamp", + "from-datetime", + ], +) +def test_build(option: str): + ulid = ULID() + value: str + includes_randomness = True + includes_timestamp = True + if option.endswith("uuid"): + value = str(ulid.to_uuid()) + elif option.endswith("str"): + value = str(ulid) + elif option.endswith("hex"): + value = ulid.hex + elif option.endswith("int"): + value = str(int(ulid)) + elif option.endswith("timestamp"): + value = str(ulid.timestamp) + includes_randomness = False + elif option.endswith("datetime"): + value = ulid.datetime.isoformat() + includes_randomness = False + else: + includes_timestamp = False + includes_randomness = False + + argv = ["build"] + if option: + argv += [f"--{option}", value] + output = cli.main(argv) + + if includes_randomness and includes_timestamp: + assert output == str(ulid) + + ulid_out = ULID.from_str(output) + if includes_timestamp: + assert ulid_out.datetime == ulid.datetime diff --git a/ulid/__init__.py b/ulid/__init__.py index 8f6400c..82363ec 100644 --- a/ulid/__init__.py +++ b/ulid/__init__.py @@ -16,14 +16,14 @@ from ulid import constants -if TYPE_CHECKING: +if TYPE_CHECKING: # pragma: no cover from pydantic import GetCoreSchemaHandler from pydantic import ValidatorFunctionWrapHandler from pydantic_core import CoreSchema try: from importlib.metadata import version -except ImportError: +except ImportError: # pragma: no cover from importlib_metadata import version # type: ignore diff --git a/ulid/__main__.py b/ulid/__main__.py index 327ed4f..efa082d 100644 --- a/ulid/__main__.py +++ b/ulid/__main__.py @@ -92,9 +92,9 @@ def make_parser(prog: str | None = None) -> argparse.ArgumentParser: return parser -def main(argv: Sequence[str], prog: str | None = None) -> None: +def main(argv: Sequence[str], prog: str | None = None) -> str: args = make_parser(prog).parse_args(argv) - args.func(args) + return args.func(args) def from_value_or_stdin(value: str, convert: Optional[Callable[[str], Any]] = None) -> Any: @@ -111,7 +111,7 @@ def parse_numeric(s: str) -> int | float: return float(s) -def build(args: argparse.Namespace) -> None: +def build(args: argparse.Namespace) -> str: ulid: ULID if args.from_int is not None: ulid = ULID.from_int(from_value_or_stdin(args.from_int, int)) @@ -127,40 +127,38 @@ def build(args: argparse.Namespace) -> None: ulid = ULID.from_uuid(from_value_or_stdin(args.from_uuid, UUID)) else: ulid = ULID() - print(ulid) + return str(ulid) -def show(args: argparse.Namespace) -> None: +def show(args: argparse.Namespace) -> str: ulid: ULID = ULID.from_str(from_value_or_stdin(args.ulid)) if args.uuid: - print(ulid.to_uuid()) + return str(ulid.to_uuid()) elif args.uuid4: - print(ulid.to_uuid4()) + return str(ulid.to_uuid4()) elif args.hex: - print(ulid.hex) + return ulid.hex elif args.int: - print(int(ulid)) + return str(int(ulid)) elif args.timestamp: - print(ulid.timestamp) + return str(ulid.timestamp) elif args.datetime: - print(ulid.datetime) + return ulid.datetime.isoformat() else: - print( - textwrap.dedent( - f""" - ULID: {ulid!s} - Hex: {ulid.hex} - Int: {int(ulid)} - Timestamp: {ulid.timestamp} - Datetime: {ulid.datetime} - """ - ).strip() - ) + return textwrap.dedent( + f""" + ULID: {ulid!s} + Hex: {ulid.hex} + Int: {int(ulid)} + Timestamp: {ulid.timestamp} + Datetime: {ulid.datetime.isoformat()} + """ + ).strip() -def entrypoint() -> None: - main(sys.argv[1:]) +def entrypoint() -> None: # pragma: no cover + print(main(sys.argv[1:])) -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover main(sys.argv[1:], "python -m ulid")