Skip to content

Commit

Permalink
feat: updates TrestleRule and tests for breaking changes
Browse files Browse the repository at this point in the history
Applys changes neccessary to upgrade to
compliance-trestle v3 and pydantic v2

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 19, 2024
1 parent 54621b4 commit 20ade32
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
6 changes: 3 additions & 3 deletions tests/trestlebot/tasks/authored/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from trestle.common.model_utils import ModelUtils
from trestle.core.models.file_content_type import FileContentType
from trestle.oscal.profile import Method, Profile
from trestle.oscal.profile import CombinationMethodValidValues, Profile

from tests import testutils
from trestlebot.tasks.authored.profile import AuthoredProfile
Expand Down Expand Up @@ -37,7 +37,7 @@ def test_create_new_default(tmp_trestle_dir: str) -> None:

assert prof.merge is not None
assert prof.merge.combine is not None
assert prof.merge.combine.method is Method.merge
assert prof.merge.combine.method is CombinationMethodValidValues.merge

assert prof.imports is not None
assert prof.imports[0].include_all is not None
Expand All @@ -62,7 +62,7 @@ def test_create_new_default_existing(tmp_trestle_dir: str) -> None:

assert prof.merge is not None
assert prof.merge.combine is not None
assert prof.merge.combine.method is Method.merge
assert prof.merge.combine.method is CombinationMethodValidValues.merge

assert prof.imports is not None
assert prof.imports[0].include_all is not None
Expand Down
2 changes: 1 addition & 1 deletion tests/trestlebot/tasks/test_assemble_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from trestle.common.model_utils import ModelUtils
from trestle.core.commands.author.catalog import CatalogGenerate
from trestle.core.commands.author.component import ComponentGenerate
from trestle.core.commands.author.profile import ProfileGenerate
from trestle.core.commands.author.prof import ProfileGenerate
from trestle.core.commands.author.ssp import SSPGenerate
from trestle.core.models.file_content_type import FileContentType
from trestle.oscal import catalog as oscal_cat
Expand Down
8 changes: 4 additions & 4 deletions tests/trestlebot/transformers/test_yaml_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_rules_transform_with_invalid_rule() -> None:
transformer = ToRulesYAMLTransformer()

with pytest.raises(
RulesTransformerException, match=".*value is not a valid dict.*"
RulesTransformerException, match=".*Input should be a valid dictionary.*"
):
transformer.transform(rule_file_info)

Expand Down Expand Up @@ -106,9 +106,9 @@ def test_rules_transform_with_additional_validation() -> None:
transformer = ToRulesYAMLTransformer()

expected_error = """2 error(s) found:
Location: description, Type: value_error.missing, Message: field required
Location: default-value, Type: value_error, Message: Default value 5% must be in the alternative \
values dict_values(['10%', '10%', '20%'])"""
Location: description, Type: missing, Message: Field required
Location: default-value, Type: value_error, Message: Value error, Default value 5% must be in the \
alternative values dict_values(['10%', '10%', '20%'])"""

with pytest.raises(
RulesTransformerException,
Expand Down
2 changes: 1 addition & 1 deletion trestlebot/tasks/authored/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def create_new_default(self, import_path: str, profile_name: str) -> None:
# Set up default values for merge settings.
merge_object: prof.Merge = gens.generate_sample_model(prof.Merge)
combine_object: prof.Combine = gens.generate_sample_model(prof.Combine)
combine_object.method = prof.Method.merge
combine_object.method = prof.CombinationMethodValidValues.merge
merge_object.combine = combine_object
merge_object.as_is = True

Expand Down
31 changes: 18 additions & 13 deletions trestlebot/transformers/trestle_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
"""


from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

from pydantic import BaseModel, Field, ValidationError, validator
from pydantic import (
BaseModel,
ConfigDict,
Field,
ValidationError,
ValidationInfo,
field_validator,
)

from trestlebot import const

Expand All @@ -25,13 +32,13 @@ class Parameter(BaseModel):
alternative_values: Dict[str, str] = Field(..., alias="alternative-values")
default_value: str = Field(..., alias="default-value")

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)

@validator("default_value", pre=False)
def check_default_value(cls, value: str, values: Dict[str, Any]) -> str:
@field_validator("default_value", mode="after")
@classmethod
def check_default_value(cls, value: str, info: ValidationInfo) -> str:
"""Check if default value is in the alternative values."""
alternative_values: Dict[str, str] = values.get("alternative_values", {})
alternative_values: Dict[str, str] = info.data.get("alternative_values", {})
if not alternative_values:
raise ValueError("Alternative values must be provided")

Expand Down Expand Up @@ -72,8 +79,7 @@ class Profile(BaseModel):
href: str
include_controls: List[Control] = Field(..., alias="include-controls")

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)


class ComponentInfo(BaseModel):
Expand All @@ -90,8 +96,7 @@ class Check(BaseModel):
name: str
description: str

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)


class TrestleRule(BaseModel):
Expand All @@ -101,8 +106,8 @@ class TrestleRule(BaseModel):
description: str
component: ComponentInfo
profile: Profile
check: Optional[Check]
parameter: Optional[Parameter]
check: Optional[Check] = Field(default=None)
parameter: Optional[Parameter] = Field(default=None)


def get_default_rule() -> TrestleRule:
Expand Down

0 comments on commit 20ade32

Please sign in to comment.