Skip to content

Commit

Permalink
Add test for boolean custom from examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkocher committed Aug 23, 2021
1 parent bf1f919 commit abb4b3f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
20 changes: 10 additions & 10 deletions pydantic_cli/examples/simple_with_boolean_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class State(str, Enum):


class Options(BaseModel):
class Config(DefaultConfig):
pass

# Simple Arg/Option can be added and a reasonable commandline "long" flag will be created.
input_file: str

Expand All @@ -33,30 +36,30 @@ class Options(BaseModel):
# Pydantic has a bit of an odd model on how it treats Optional[T]
# These end up being indistinguishable.
outfile: Optional[str]
outfile2: Optional[str] = None
fasta: Optional[str] = None
# This is a "required" value that can be set to None, or str
outfile3: Optional[str] = Field(...)
report_json: Optional[str] = Field(...)

# Required Boolean options/flag can be added by
enable_alpha: bool
alpha: bool

# When specifying custom descriptions or flags as a pydantic Field, the flags should be specified as:
# (short, long) or (long, ) and be the OPPOSITE of the default value provide
enable_beta: bool = Field(
beta_filter: bool = Field(
False,
description="Enable beta filter mode",
extras={"cli": ("-b", "--beta-filter")},
)

# Again, note Pydantic will treat these as indistinguishable
enable_gamma: Optional[bool]
enable_delta: Optional[bool] = None
gamma: Optional[bool]
delta: Optional[bool] = None

# You need to set this to ... to declare it as "Required". The pydantic docs recommend using
# Field(...) instead of ... to avoid issues with mypy.
# pydantic-cli doesn't have a good mechanism for declaring this 3-state value of None, True, False.
# using a boolean commandline flag (e.g., --enable-logging, or --disable-logging)
enable_zeta: Optional[bool] = Field(
zeta_mode: Optional[bool] = Field(
..., description="Enable Zeta mode to experimental filtering mode."
)

Expand All @@ -77,9 +80,6 @@ class Options(BaseModel):
# there's an ambiguity. "1" will be cast to an int, which might not be the desired/expected results
filter_mode: Union[str, int]

class Config(DefaultConfig):
pass


def example_runner(opts: Options) -> int:
print(f"Mock example running with {opts}")
Expand Down
31 changes: 31 additions & 0 deletions pydantic_cli/tests/test_examples_simple_with_boolean_custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pydantic_cli.examples.simple_with_boolean_custom import Options, example_runner

from . import _TestHarness, HarnessConfig


class TestExamples(_TestHarness[Options]):
CONFIG = HarnessConfig(Options, example_runner)

def test_simple_01(self):
self.run_config(
[
"--input_file",
"/path/to/file.txt",
"--input_file2",
"/path/2.txt",
"-f",
"/path/to/file.h5",
"--report_json",
"output.json",
"--fasta",
"output.fasta",
"--enable-alpha",
"--enable-zeta_mode",
"--epsilon",
"--states",
"RUNNING",
"FAILED",
"--filter_mode",
"1",
]
)

0 comments on commit abb4b3f

Please sign in to comment.