Skip to content

Commit

Permalink
Add CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdomke committed Mar 21, 2024
1 parent c86cb9d commit 7abf3c7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 27 deletions.
77 changes: 77 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions ulid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
48 changes: 23 additions & 25 deletions ulid/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
Expand All @@ -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")

0 comments on commit 7abf3c7

Please sign in to comment.