Skip to content

Commit

Permalink
Add case-insensitive validation for CodeTF enum values (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella authored Oct 29, 2024
1 parent de8f2ca commit e2c69d4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/codemodder/codetf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@
from codemodder.context import CodemodExecutionContext


class Action(Enum):
class CaseInsensitiveEnum(str, Enum):
@classmethod
def _missing_(cls, value: object):
if not isinstance(value, str):
return super()._missing_(value)

return cls.__members__.get(value.upper())


class Action(CaseInsensitiveEnum):
ADD = "add"
REMOVE = "remove"


class PackageResult(Enum):
class PackageResult(CaseInsensitiveEnum):
COMPLETED = "completed"
FAILED = "failed"
SKIPPED = "skipped"


class DiffSide(Enum):
class DiffSide(CaseInsensitiveEnum):
LEFT = "left"
RIGHT = "right"

Expand Down
37 changes: 37 additions & 0 deletions tests/test_codetf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jsonschema
import pytest
import requests
from pydantic import ValidationError

from codemodder.codetf import Change, ChangeSet, CodeTF, DiffSide, Reference, Result

Expand Down Expand Up @@ -130,3 +131,39 @@ def test_write_codetf_with_results(tmpdir, mocker, codetf_schema):
def test_reference_use_url_for_description():
ref = Reference(url="https://example.com")
assert ref.description == "https://example.com"


def test_case_insensitive_change_validation():
json = {
"lineNumber": 1,
"description": "Change 1 to 2",
"diffSide": "RIGHT",
"packageActions": [
{
"action": "ADD",
"package": "foo",
"result": "COMPLETED",
}
],
}

Change.model_validate(json)


@pytest.mark.parametrize("bad_value", ["MIDDLE", "middle"])
def test_still_invalidates_bad_value(bad_value):
json = {
"lineNumber": 1,
"description": "Change 1 to 2",
"diffSide": bad_value,
"packageActions": [
{
"action": "ADD",
"package": "foo",
"result": "COMPLETED",
}
],
}

with pytest.raises(ValidationError):
Change.model_validate(json)

0 comments on commit e2c69d4

Please sign in to comment.