Skip to content

Commit e8daffa

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

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed
 

‎git_hg_sync/events.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
from pydantic import BaseModel
1+
from typing import Self
2+
from pydantic import (
3+
BaseModel,
4+
Field,
5+
ValidationError,
6+
ValidationInfo,
7+
field_validator,
8+
model_validator,
9+
)
210

311

412
class Push(BaseModel):
513
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)
14+
branches: dict[str, str] | None = Field(
15+
default_factory=dict
16+
) # Mapping between branch names (key) and corresponding commit sha (value)
17+
tags: dict[str, str] | None = Field(
18+
default_factory=dict
19+
) # Mapping between tag names (key) and corresponding commit sha (value)
1220
time: int
1321
pushid: int
1422
user: str
1523
push_json_url: str
1624

25+
@model_validator(mode="after")
26+
def check_branch_tags(self) -> Self:
27+
"""Check that at least one of branches or tags is not empty."""
28+
if not self.branches and not self.tags:
29+
raise ValueError("Either (non-empty) branches or tags is required")
30+
return self
31+
1732

1833
class Tag(BaseModel):
1934
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

+24
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ def test_parse_invalid_data_types(raw_push_entity: dict) -> None:
4343
PulseWorker.parse_entity(raw_push_entity)
4444

4545

46+
@pytest.mark.parametrize(
47+
"remove, exception",
48+
[
49+
([], None),
50+
(["branches"], None),
51+
(["tags"], None),
52+
(["branches", "tags"], ValidationError),
53+
],
54+
)
55+
def test_parse_branch_and_or_tags(
56+
raw_push_entity: dict, remove: list[str], exception: Exception | None
57+
) -> None:
58+
entity = raw_push_entity
59+
for f in remove:
60+
del entity[f]
61+
62+
if exception:
63+
with pytest.raises(exception):
64+
PulseWorker.parse_entity(entity)
65+
else:
66+
push_entity = PulseWorker.parse_entity(entity)
67+
assert isinstance(push_entity, Push)
68+
69+
4670
def test_sigint_signal_interception() -> None:
4771
config_file = HERE / "data" / "config.toml"
4872
module_path = HERE.parent / "git_hg_sync" / "__main__.py"

0 commit comments

Comments
 (0)
Failed to load comments.