Skip to content

Commit f6cb38f

Browse files
committedMar 18, 2025
events: add branches/tags validation
1 parent a3ea7b7 commit f6cb38f

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed
 

‎git_hg_sync/events.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
from pydantic import BaseModel
1+
from typing import Self
2+
from pydantic import BaseModel, Field, ValidationError, ValidationInfo, field_validator, model_validator
23

34

45
class Push(BaseModel):
56
repo_url: str
6-
branches: dict[
7-
str, str
8-
] # Mapping between branch names (key) and corresponding commit sha (value)
9-
tags: dict[
10-
str, str
11-
] # Mapping between tag names (key) and corresponding commit sha (value)
7+
branches: dict[str, str] | None = Field(
8+
default_factory=dict
9+
) # Mapping between branch names (key) and corresponding commit sha (value)
10+
tags: dict[str, str] | None = Field(
11+
default_factory=dict
12+
) # Mapping between tag names (key) and corresponding commit sha (value)
1213
time: int
1314
pushid: int
1415
user: str
1516
push_json_url: str
1617

18+
@model_validator(mode="after")
19+
def check_branch_tags(self) -> Self:
20+
"""Check that at least one of branches or tags is not empty."""
21+
if not self.branches and not self.tags:
22+
raise ValueError("Either (non-empty) branches or tags is required")
23+
return self
24+
1725

1826
class Tag(BaseModel):
1927
repo_url: str

‎tests/test_integration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def test_send_and_receive(pulse_config: PulseConfig) -> None:
2424
"repo_url": "repo.git",
2525
"branches": {},
2626
"tags": {},
27-
"commit": "sha",
2827
"time": 0,
2928
"pushid": 0,
3029
"user": "user",
@@ -114,3 +113,4 @@ def test_full_app(
114113

115114
# test
116115
assert "BAR CONTENT" in hg_cat(hg_remote_repo_path, "bar.txt", "default")
116+
assert "FIREFOX_128_0esr_RELEASE" in hg_cat(hg_remote_repo_path, ".hgtags", "tags")

‎tests/test_pulse_worker.py

+18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ def test_parse_invalid_data_types(raw_push_entity: dict) -> None:
4242
raw_push_entity["branches"] = ["main"]
4343
PulseWorker.parse_entity(raw_push_entity)
4444

45+
@pytest.mark.parametrize('remove, exception', [
46+
([], None),
47+
(['branches'], None),
48+
(['tags'], None ),
49+
(['branches', 'tags'], ValidationError),
50+
])
51+
def test_parse_branch_and_or_tags(raw_push_entity: dict, remove: list[str], exception:
52+
Exception | None) -> None:
53+
entity = raw_push_entity
54+
for f in remove:
55+
del entity[f]
56+
57+
if exception:
58+
with pytest.raises(exception):
59+
PulseWorker.parse_entity(entity)
60+
else:
61+
push_entity = PulseWorker.parse_entity(entity)
62+
assert isinstance(push_entity, Push)
4563

4664
def test_sigint_signal_interception() -> None:
4765
config_file = HERE / "data" / "config.toml"

0 commit comments

Comments
 (0)
Failed to load comments.