Skip to content

Commit 760be81

Browse files
committed
helpers
1 parent 2588545 commit 760be81

File tree

2 files changed

+201
-3
lines changed

2 files changed

+201
-3
lines changed

databricks/sdk/service/_internal.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import datetime
22
import urllib.parse
3-
from typing import Callable, Dict, Generic, Optional, Type, TypeVar
3+
from typing import Callable, Dict, Generic, List, Optional, Type, TypeVar
4+
from google.protobuf.timestamp_pb2 import Timestamp
5+
from google.protobuf.duration_pb2 import Duration
6+
from databricks.sdk.common.types.fieldmask import FieldMask
47

58

69
def _from_dict(d: Dict[str, any], field: str, cls: Type) -> any:
@@ -46,6 +49,59 @@ def _escape_multi_segment_path_parameter(param: str) -> str:
4649
return urllib.parse.quote(param)
4750

4851

52+
def _timestamp(d: Dict[str, any], field: str) -> Optional[Timestamp]:
53+
if field not in d or not d[field]:
54+
return None
55+
ts = Timestamp()
56+
ts.FromJsonString(d[field])
57+
return ts
58+
59+
def _repeated_timestamp(d: Dict[str, any], field: str) -> Optional[List[Timestamp]]:
60+
if field not in d or not d[field]:
61+
return None
62+
result = []
63+
for v in d[field]:
64+
ts = Timestamp()
65+
ts.FromJsonString(v)
66+
result.append(ts)
67+
return result
68+
69+
def _duration(d: Dict[str, any], field: str) -> Optional[Duration]:
70+
if field not in d or not d[field]:
71+
return None
72+
dur = Duration()
73+
dur.FromJsonString(d[field])
74+
return dur
75+
76+
def _repeated_duration(d: Dict[str, any], field: str) -> Optional[List[Duration]]:
77+
if field not in d or not d[field]:
78+
return None
79+
result = []
80+
for v in d[field]:
81+
dur = Duration()
82+
dur.FromJsonString(v)
83+
result.append(dur)
84+
return result
85+
86+
def _fieldmask(d: Dict[str, any], field: str) -> Optional[FieldMask]:
87+
if field not in d or not d[field]:
88+
return None
89+
fm = FieldMask()
90+
fm.FromJsonString(d[field])
91+
return fm
92+
93+
def _repeated_fieldmask(d: Dict[str, any], field: str) -> Optional[List[FieldMask]]:
94+
if field not in d or not d[field]:
95+
return None
96+
result = []
97+
for v in d[field]:
98+
fm = FieldMask()
99+
fm.FromJsonString(v)
100+
result.append(fm)
101+
return result
102+
103+
104+
49105
ReturnType = TypeVar("ReturnType")
50106

51107

tests/test_internal.py

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from dataclasses import dataclass
22
from enum import Enum
33

4+
from google.protobuf.duration_pb2 import Duration
5+
from google.protobuf.timestamp_pb2 import Timestamp
6+
from databricks.sdk.common.types.fieldmask import FieldMask
7+
48
from databricks.sdk.service._internal import (
5-
_enum, _escape_multi_segment_path_parameter, _from_dict, _repeated_dict,
6-
_repeated_enum)
9+
_duration, _enum, _escape_multi_segment_path_parameter, _fieldmask, _from_dict, _repeated_dict,
10+
_repeated_duration, _repeated_enum, _repeated_fieldmask, _repeated_timestamp, _timestamp)
711

812

913
class A(Enum):
@@ -52,3 +56,141 @@ def test_escape_multi_segment_path_parameter():
5256
assert _escape_multi_segment_path_parameter("a/b") == "a/b"
5357
assert _escape_multi_segment_path_parameter("a?b") == "a%3Fb"
5458
assert _escape_multi_segment_path_parameter("a#b") == "a%23b"
59+
60+
61+
def test_timestamp():
62+
# Test valid timestamp
63+
result = _timestamp({"field": "2023-01-01T12:00:00Z"}, "field")
64+
assert isinstance(result, Timestamp)
65+
assert result.ToJsonString() == "2023-01-01T12:00:00Z"
66+
67+
68+
def test_timestamp_missing_field():
69+
# Test missing field
70+
assert _timestamp({}, "field") is None
71+
72+
73+
def test_timestamp_none_value():
74+
# Test None value
75+
assert _timestamp({"field": None}, "field") is None
76+
77+
78+
def test_timestamp_empty_value():
79+
# Test empty value
80+
assert _timestamp({"field": ""}, "field") is None
81+
82+
83+
def test_repeated_timestamp():
84+
# Test valid repeated timestamps
85+
result = _repeated_timestamp({"field": ["2023-01-01T12:00:00Z", "2023-01-02T12:00:00Z"]}, "field")
86+
assert len(result) == 2
87+
assert all(isinstance(ts, Timestamp) for ts in result)
88+
assert result[0].ToJsonString() == "2023-01-01T12:00:00Z"
89+
assert result[1].ToJsonString() == "2023-01-02T12:00:00Z"
90+
91+
92+
def test_repeated_timestamp_missing_field():
93+
# Test missing field
94+
assert _repeated_timestamp({}, "field") is None
95+
96+
97+
def test_repeated_timestamp_none_value():
98+
# Test None value
99+
assert _repeated_timestamp({"field": None}, "field") is None
100+
101+
102+
def test_repeated_timestamp_empty_list():
103+
# Test empty list
104+
assert _repeated_timestamp({"field": []}, "field") is None
105+
106+
107+
def test_duration():
108+
# Test valid duration
109+
result = _duration({"field": "3600s"}, "field")
110+
assert isinstance(result, Duration)
111+
assert result.ToJsonString() == "3600s"
112+
113+
114+
def test_duration_missing_field():
115+
# Test missing field
116+
assert _duration({}, "field") is None
117+
118+
119+
def test_duration_none_value():
120+
# Test None value
121+
assert _duration({"field": None}, "field") is None
122+
123+
124+
def test_duration_empty_value():
125+
# Test empty value
126+
assert _duration({"field": ""}, "field") is None
127+
128+
129+
def test_repeated_duration():
130+
# Test valid repeated durations
131+
result = _repeated_duration({"field": ["3600s", "7200s"]}, "field")
132+
assert len(result) == 2
133+
assert all(isinstance(dur, Duration) for dur in result)
134+
assert result[0].ToJsonString() == "3600s"
135+
assert result[1].ToJsonString() == "7200s"
136+
137+
138+
def test_repeated_duration_missing_field():
139+
# Test missing field
140+
assert _repeated_duration({}, "field") is None
141+
142+
143+
def test_repeated_duration_none_value():
144+
# Test None value
145+
assert _repeated_duration({"field": None}, "field") is None
146+
147+
148+
def test_repeated_duration_empty_list():
149+
# Test empty list
150+
assert _repeated_duration({"field": []}, "field") is None
151+
152+
153+
def test_fieldmask():
154+
# Test valid fieldmask
155+
result = _fieldmask({"field": "path1,path2"}, "field")
156+
assert isinstance(result, FieldMask)
157+
assert result.ToJsonString() == "path1,path2"
158+
159+
160+
def test_fieldmask_missing_field():
161+
# Test missing field
162+
assert _fieldmask({}, "field") is None
163+
164+
165+
def test_fieldmask_none_value():
166+
# Test None value
167+
assert _fieldmask({"field": None}, "field") is None
168+
169+
170+
def test_fieldmask_empty_value():
171+
# Test empty value
172+
assert _fieldmask({"field": ""}, "field") is None
173+
174+
175+
def test_repeated_fieldmask():
176+
# Test valid repeated fieldmasks
177+
result = _repeated_fieldmask({"field": ["path1,path2", "path3,path4"]}, "field")
178+
assert len(result) == 2
179+
assert all(isinstance(fm, FieldMask) for fm in result)
180+
assert result[0].ToJsonString() == "path1,path2"
181+
assert result[1].ToJsonString() == "path3,path4"
182+
183+
184+
def test_repeated_fieldmask_missing_field():
185+
# Test missing field
186+
assert _repeated_fieldmask({}, "field") is None
187+
188+
189+
def test_repeated_fieldmask_none_value():
190+
# Test None value
191+
assert _repeated_fieldmask({"field": None}, "field") is None
192+
193+
194+
def test_repeated_fieldmask_empty_list():
195+
# Test empty list
196+
assert _repeated_fieldmask({"field": []}, "field") is None

0 commit comments

Comments
 (0)