Skip to content

Commit

Permalink
Add tests for helpers and __all__ in caep package
Browse files Browse the repository at this point in the history
  • Loading branch information
Helge Aksdal committed Mar 28, 2023
1 parent 67349be commit 425544f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions caep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
from .helpers import raise_if_some_and_not_all, script_name # noqa: F401
from .schema import load as load # noqa: F401
from .xdg import get_cache_dir, get_config_dir # noqa: F401

__all__ = [
"handle_args",
"get_cache_dir",
"get_config_dir",
"load",
"raise_if_some_and_not_all",
"script_name",
]
47 changes: 47 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Any, Dict, Optional

import pytest
from pydantic import BaseModel # noqa: E0611
from pydantic import Field, root_validator
from test_schema import parse_args

import caep


class ExampleConfig(BaseModel):
username: Optional[str] = Field(description="Username")
password: Optional[str] = Field(description="Password")
parent_id: Optional[str] = Field(description="Parent ID")

@root_validator
def check_arguments(cls, values: Dict[str, Any]) -> Dict[str, Any]:
"""If one argument is set, they should all be set"""

caep.raise_if_some_and_not_all(values, ["username", "password", "parent_id"])

return values


def test_raise_if_some_and_not_all() -> None:
"""raise_if_some_and_not_all success test"""
commandline = "--username pytest --password supersecret --parent-id testing".split()

config = parse_args(ExampleConfig, commandline)

assert config.username == "pytest"
assert config.password == "supersecret"
assert config.parent_id == "testing"


def test_raise_if_some_and_not_all_fail_to_validate() -> None:
"""raise_if_some_and_not_all failure test"""
commandline = "--username pytest --password supersecret".split()

with pytest.raises(caep.helpers.ArgumentError):
parse_args(ExampleConfig, commandline)


def test_script_name() -> None:
"""script_name corresponds with name of script"""

assert caep.script_name() == "test-helpers"

0 comments on commit 425544f

Please sign in to comment.