Skip to content

Commit

Permalink
fix: fixes if statement in get_authored_types and adds unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 5, 2023
1 parent 7ca89d5 commit 17b0497
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
100 changes: 100 additions & 0 deletions tests/trestlebot/tasks/authored/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/python

# Copyright 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Test author types for Trestlebot"""

import os

import pytest

from tests import testutils
from trestlebot.tasks.authored import types
from trestlebot.tasks.authored.base_authored import (
AuthoredObjectException,
AuthorObjectBase,
)
from trestlebot.tasks.authored.catalog import AuthoredCatalog
from trestlebot.tasks.authored.compdef import AuthoredComponentsDefinition
from trestlebot.tasks.authored.profile import AuthoredProfile
from trestlebot.tasks.authored.ssp import AuthoredSSP


test_prof = "simplified_nist_profile"
test_comp = "test_comp"
test_ssp_output = "test-ssp"
markdown_dir = "md_ssp"


def test_get_authored_catalog(tmp_trestle_dir: str) -> None:
"""Test get authored type for catalogs"""

authored_object: AuthorObjectBase = types.get_authored_object(
types.AuthoredType.CATALOG.value, tmp_trestle_dir, ""
)

assert authored_object.get_trestle_root() == tmp_trestle_dir
assert isinstance(authored_object, AuthoredCatalog)


def test_get_authored_profile(tmp_trestle_dir: str) -> None:
"""Test get authored type for catalogs"""

authored_object: AuthorObjectBase = types.get_authored_object(
types.AuthoredType.PROFILE.value, tmp_trestle_dir, ""
)

assert authored_object.get_trestle_root() == tmp_trestle_dir
assert isinstance(authored_object, AuthoredProfile)


def test_get_authored_compdef(tmp_trestle_dir: str) -> None:
"""Test get authored type for catalogs"""

# Test with profile
authored_object: AuthorObjectBase = types.get_authored_object(
types.AuthoredType.COMPDEF.value, tmp_trestle_dir, ""
)

assert authored_object.get_trestle_root() == tmp_trestle_dir
assert isinstance(authored_object, AuthoredComponentsDefinition)


def test_get_authored_ssp(tmp_trestle_dir: str) -> None:
"""Test get authored type for catalogs"""
ssp_index_path = os.path.join(tmp_trestle_dir, "ssp-index.json")
testutils.write_index_json(ssp_index_path, test_ssp_output, test_prof, [test_comp])

with pytest.raises(
FileNotFoundError,
):
_ = types.get_authored_object(types.AuthoredType.SSP.value, tmp_trestle_dir, "")

# Test with profile
authored_object: AuthorObjectBase = types.get_authored_object(
types.AuthoredType.SSP.value, tmp_trestle_dir, ssp_index_path
)

assert authored_object.get_trestle_root() == tmp_trestle_dir
assert isinstance(authored_object, AuthoredSSP)


def test_invalid_authored_type(tmp_trestle_dir: str) -> None:
"""Test triggering an error with an invalid type"""
with pytest.raises(
AuthoredObjectException,
match="Invalid authored type fake",
):
_ = types.get_authored_object("fake", tmp_trestle_dir, "")
2 changes: 1 addition & 1 deletion trestlebot/tasks/authored/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_authored_object(
return AuthoredProfile(working_dir)
elif input_type == AuthoredType.COMPDEF.value:
return AuthoredComponentsDefinition(working_dir)
elif input_type is AuthoredType.SSP.value:
elif input_type == AuthoredType.SSP.value:
ssp_index: SSPIndex = SSPIndex(ssp_index_path)
return AuthoredSSP(working_dir, ssp_index)
else:
Expand Down

0 comments on commit 17b0497

Please sign in to comment.