Skip to content

Commit

Permalink
test: add unit tests for missed code paths (#126)
Browse files Browse the repository at this point in the history
* test: add unit test case for unset token in test_entrypoint_base.py

Signed-off-by: Jennifer Power <[email protected]>

* test: adds negative test for get_trestle_model_dir in test_types.py

Signed-off-by: Jennifer Power <[email protected]>

* refactor: adds dynamic list of supported models in autosync CLI

Signed-off-by: Jennifer Power <[email protected]>

* test: adds unit test for validate_args with invalid model

Signed-off-by: Jennifer Power <[email protected]>

---------

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 authored Jan 2, 2024
1 parent 046ba73 commit c6b5b49
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
23 changes: 23 additions & 0 deletions tests/trestlebot/entrypoints/test_autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@

"""Test for Autosync CLI"""

import argparse
import logging
from typing import Any, Dict
from unittest.mock import patch

import pytest

from tests.testutils import args_dict_to_list
from trestlebot.entrypoints.autosync import AutoSyncEntrypoint
from trestlebot.entrypoints.autosync import main as cli_main
from trestlebot.entrypoints.entrypoint_base import EntrypointInvalidArgException


@pytest.fixture
Expand All @@ -48,6 +51,26 @@ def test_invalid_oscal_model(valid_args_dict: Dict[str, str]) -> None:
cli_main()


def test_validate_args_invalid_model(valid_args_dict: Dict[str, str]) -> None:
"""
Test invalid oscal model with validate args function.
This is a separate test from test_invalid_oscal_model because
it args are make invalid after the args are parsed.
"""
args_dict = valid_args_dict
with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(
EntrypointInvalidArgException,
match="Invalid args --oscal-model: Invalid value fake. "
"Please use one of catalog, profile, ssp, compdef",
):
parser = argparse.ArgumentParser()
auto_sync = AutoSyncEntrypoint(parser=parser)
args = parser.parse_args()
args.oscal_model = "fake"
auto_sync.validate_args(args)


def test_no_ssp_index(valid_args_dict: Dict[str, str], caplog: Any) -> None:
"""Test missing index file for ssp"""
args_dict = valid_args_dict
Expand Down
11 changes: 11 additions & 0 deletions tests/trestlebot/entrypoints/test_entrypoint_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ def test_set_git_provider_with_none() -> None:
args = argparse.Namespace(target_branch=None, with_token=None)
provider = EntrypointBase.set_git_provider(args=args)
assert provider is None


def test_set_provider_with_no_token() -> None:
"""Test set_git_provider function with no token"""
args = argparse.Namespace(target_branch="main", with_token=None)
with pytest.raises(
EntrypointInvalidArgException,
match="Invalid args --with-token: "
"with-token flag must be set when using target-branch",
):
EntrypointBase.set_git_provider(args=args)
11 changes: 11 additions & 0 deletions tests/trestlebot/tasks/authored/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Test author types for Trestlebot"""

import os
from unittest.mock import Mock

import pytest

Expand Down Expand Up @@ -98,3 +99,13 @@ def test_invalid_authored_type(tmp_trestle_dir: str) -> None:
match="Invalid authored type fake",
):
_ = types.get_authored_object("fake", tmp_trestle_dir, "")


def test_get_model_dir_with_invalid_type() -> None:
"""Test triggering an error with an invalid type when getting the model dir."""
with pytest.raises(
AuthoredObjectException,
match="Invalid authored object <class 'unittest.mock.Mock'>",
):
mock = Mock(spec=AuthoredObjectBase)
_ = types.get_trestle_model_dir(mock)
11 changes: 6 additions & 5 deletions trestlebot/entrypoints/autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, parser: argparse.ArgumentParser) -> None:
"""Initialize."""
# Setup base arguments
super().__init__(parser)
self.supported_models: List[str] = [model.value for model in types.AuthoredType]
self.setup_autosync_arguments()

def setup_autosync_arguments(self) -> None:
Expand All @@ -66,7 +67,7 @@ def setup_autosync_arguments(self) -> None:
"--oscal-model",
required=True,
type=str,
choices=["catalog", "profile", "compdef", "ssp"],
choices=self.supported_models,
help="OSCAL model type to run tasks on.",
)
self.parser.add_argument(
Expand Down Expand Up @@ -98,12 +99,12 @@ def setup_autosync_arguments(self) -> None:

def validate_args(self, args: argparse.Namespace) -> None:
"""Validate the arguments for the autosync entrypoint."""
authored_list: List[str] = [model.value for model in types.AuthoredType]
if args.oscal_model not in authored_list:
supported_models_str = ", ".join(self.supported_models)
if args.oscal_model not in self.supported_models:
raise EntrypointInvalidArgException(
"--oscal-model",
f"Value {args.oscal_model} is not valid."
f"Please use one of {authored_list}",
f"Invalid value {args.oscal_model}. "
f"Please use one of {supported_models_str}",
)

if not args.markdown_path:
Expand Down
4 changes: 3 additions & 1 deletion trestlebot/tasks/authored/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ def get_trestle_model_dir(authored_object: AuthoredObjectBase) -> str:
elif isinstance(authored_object, AuthoredSSP):
return const.MODEL_DIR_SSP
else:
raise AuthoredObjectException(f"Invalid authored object {authored_object}")
raise AuthoredObjectException(
f"Invalid authored object {type(authored_object)}"
)

0 comments on commit c6b5b49

Please sign in to comment.