Skip to content

Commit c5ba60e

Browse files
authored
fix!: require attribute and amount names to be unique (#265)
fix: require attribute and amount names to be unique Signed-off-by: Stephen Crowe <6042774+crowecawcaw@users.noreply.github.com>
1 parent 26916af commit c5ba60e

2 files changed

Lines changed: 63 additions & 11 deletions

File tree

‎src/openjd/model/v2023_09/_model.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,7 +2585,7 @@ def _validate_amounts(
25852585
return v
25862586
if len(v) == 0:
25872587
raise ValueError("List must contain at least one element or not be defined.")
2588-
return v
2588+
return validate_unique_elements(v, item_value=lambda v: v.name.lower(), property="name")
25892589

25902590
@field_validator("attributes")
25912591
@classmethod
@@ -2596,7 +2596,7 @@ def _validate_attributes(
25962596
return v
25972597
if len(v) == 0:
25982598
raise ValueError("List must contain at least one element or not be defined.")
2599-
return v
2599+
return validate_unique_elements(v, item_value=lambda v: v.name.lower(), property="name")
26002600

26012601
@model_validator(mode="after")
26022602
def _validate(self) -> Self:

‎test/openjd/model/v2023_09/test_step_host_requirements.py‎

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,24 +457,26 @@ def test_parse_success(self, data: dict[str, Any]) -> None:
457457
# no exception was raised.
458458

459459
@pytest.mark.parametrize(
460-
"data,error_count",
460+
"data,error_count,error_loc",
461461
[
462-
pytest.param({}, 1, id="missing amounts & attributes"),
463-
pytest.param({"unknown": "value"}, 1, id="unknown field"),
464-
pytest.param({"amounts": []}, 1, id="too few amounts"),
465-
pytest.param({"attributes": []}, 1, id="too few attributes"),
462+
pytest.param({}, 1, (), id="missing amounts & attributes"),
463+
pytest.param({"unknown": "value"}, 1, ("unknown",), id="unknown field"),
464+
pytest.param({"amounts": []}, 1, ("amounts",), id="too few amounts"),
465+
pytest.param({"attributes": []}, 1, ("attributes",), id="too few attributes"),
466466
pytest.param(
467467
{"amounts": [{"name": f"amount.mycap{i}", "min": 1} for i in range(0, 51)]},
468468
1,
469+
(),
469470
id="too many as only amounts",
470471
),
471472
pytest.param(
472473
{
473474
"attributes": [
474-
{"name": f"attr.mycap{i}|", "anyOf": ["foo"]} for i in range(0, 51)
475+
{"name": f"attr.mycap{i}", "anyOf": ["foo"]} for i in range(0, 51)
475476
]
476477
},
477478
1,
479+
(),
478480
id="too many as only attributes",
479481
),
480482
pytest.param(
@@ -485,16 +487,66 @@ def test_parse_success(self, data: dict[str, Any]) -> None:
485487
],
486488
},
487489
1,
490+
(),
488491
id="too many as combination",
489492
),
493+
pytest.param(
494+
{
495+
"amounts": [
496+
{"name": "amount.mycap", "min": 1},
497+
{"name": "amount.mycap", "min": 2},
498+
]
499+
},
500+
1,
501+
("amounts",),
502+
id="duplicate amount names",
503+
),
504+
pytest.param(
505+
{
506+
"amounts": [
507+
{"name": "amount.mycap", "min": 1},
508+
{"name": "amount.MYCAP", "min": 2},
509+
]
510+
},
511+
1,
512+
("amounts",),
513+
id="duplicate amount names case insensitive",
514+
),
515+
pytest.param(
516+
{
517+
"attributes": [
518+
{"name": "attr.mycap", "anyOf": ["foo"]},
519+
{"name": "attr.mycap", "anyOf": ["bar"]},
520+
]
521+
},
522+
1,
523+
("attributes",),
524+
id="duplicate attribute names",
525+
),
526+
pytest.param(
527+
{
528+
"attributes": [
529+
{"name": "attr.mycap", "anyOf": ["foo"]},
530+
{"name": "attr.MYCAP", "anyOf": ["bar"]},
531+
]
532+
},
533+
1,
534+
("attributes",),
535+
id="duplicate attribute names case insensitive",
536+
),
490537
],
491538
)
492-
def test_parse_fails(self, data: dict[str, Any], error_count: int) -> None:
493-
# Failure case testing for Open Job Description AmountRequirementTemplate.
539+
def test_parse_fails(self, data: dict[str, Any], error_count: int, error_loc: tuple) -> None:
540+
# Failure case testing for Open Job Description HostRequirementsTemplate.
494541

495542
# WHEN
496543
with pytest.raises(ValidationError) as excinfo:
497-
_parse_model(model=AmountRequirementTemplate, obj=data)
544+
_parse_model(model=HostRequirementsTemplate, obj=data)
498545

499546
# THEN
500547
assert len(excinfo.value.errors()) == error_count, str(excinfo.value)
548+
if error_loc:
549+
actual_loc = excinfo.value.errors()[0]["loc"]
550+
assert (
551+
actual_loc[: len(error_loc)] == error_loc
552+
), f"Expected {error_loc}, got {actual_loc}"

0 commit comments

Comments
 (0)