Skip to content

Commit

Permalink
Ensure calendar slugs are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Apr 26, 2024
1 parent b4ef131 commit 85e1107
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions calmerge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ def validate_offset_days(cls, offset_days: list[int]) -> list[int]:
class Config(BaseModel):
calendars: list[CalendarConfig] = Field(alias="calendar", default_factory=list)

@field_validator("calendars")
@classmethod
def validate_unique_calendar_slugs(
cls, calendars: list[CalendarConfig]
) -> list[CalendarConfig]:
calendar_slugs = {calendar.slug for calendar in calendars}

if len(calendar_slugs) != len(calendars):
raise PydanticCustomError("calendar_slugs", "Calendar slugs must be unique")

return calendars

@classmethod
def from_file(cls, path: Path) -> "Config":
with path.open(mode="rb") as f:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def test_invalid_offset_days() -> None:
)


def test_duplicate_calendar_slug() -> None:
calendar_config = CalendarConfig(slug="test", urls=["https://example.com"]) # type: ignore [list-item]

with pytest.raises(ValidationError) as e:
Config(calendar=[calendar_config] * 5)

assert e.value.errors()[0]["msg"] == "Calendar slugs must be unique"


def test_urls_expand_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("FOO", "BAR")

Expand Down

0 comments on commit 85e1107

Please sign in to comment.