3 files changed +47
-8
lines changed Original file line number Diff line number Diff line change 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
+ )
2
10
3
11
4
12
class Push (BaseModel ):
5
13
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)
12
20
time : int
13
21
pushid : int
14
22
user : str
15
23
push_json_url : str
16
24
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
+
17
32
18
33
class Tag (BaseModel ):
19
34
repo_url : str
Original file line number Diff line number Diff line change @@ -24,7 +24,6 @@ def test_send_and_receive(pulse_config: PulseConfig) -> None:
24
24
"repo_url" : "repo.git" ,
25
25
"branches" : {},
26
26
"tags" : {},
27
- "commit" : "sha" ,
28
27
"time" : 0 ,
29
28
"pushid" : 0 ,
30
29
"user" : "user" ,
@@ -114,3 +113,4 @@ def test_full_app(
114
113
115
114
# test
116
115
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" )
Original file line number Diff line number Diff line change @@ -43,6 +43,30 @@ def test_parse_invalid_data_types(raw_push_entity: dict) -> None:
43
43
PulseWorker .parse_entity (raw_push_entity )
44
44
45
45
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
+
46
70
def test_sigint_signal_interception () -> None :
47
71
config_file = HERE / "data" / "config.toml"
48
72
module_path = HERE .parent / "git_hg_sync" / "__main__.py"
0 commit comments