Skip to content

Commit 096bc3e

Browse files
committed
better tests
1 parent 016f216 commit 096bc3e

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

databricks/sdk/common/types/fieldmask.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ class FieldMask(object):
66
# The original implementation only works with proto generated classes.
77
# Since our classes are not generated from proto files, we need to implement it manually.
88

9-
def __init__(self):
10-
"""Initialize FieldMask with empty paths."""
11-
self.paths = []
9+
def __init__(self, field_mask=None):
10+
"""Initializes the FieldMask."""
11+
if field_mask:
12+
self.paths = field_mask
1213

1314
def ToJsonString(self) -> str:
1415
"""Converts FieldMask to string."""

tests/test_internal.py

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,148 +61,136 @@ def test_escape_multi_segment_path_parameter():
6161

6262

6363
@pytest.mark.parametrize(
64-
"input_dict,field_name,expected_result,expected_json,description",
64+
"input_dict,field_name,expected_result,expected_timestamp,description",
6565
[
66-
({"field": "2023-01-01T12:00:00Z"}, "field", "timestamp_object", "2023-01-01T12:00:00Z", "valid timestamp"),
66+
({"field": "2023-01-01T12:00:00Z"}, "field", "timestamp_object", Timestamp(seconds=1672574400), "valid timestamp"),
6767
({}, "field", None, None, "missing field"),
6868
({"field": None}, "field", None, None, "None value"),
6969
({"field": ""}, "field", None, None, "empty value"),
7070
],
7171
)
72-
def test_timestamp(input_dict, field_name, expected_result, expected_json, description):
72+
def test_timestamp(input_dict, field_name, expected_result, expected_timestamp, description):
7373
"""Test _timestamp function with various input scenarios."""
7474
result = _timestamp(input_dict, field_name)
7575

7676
if expected_result is None:
7777
assert result is None
7878
else:
7979
assert isinstance(result, Timestamp)
80-
ts = Timestamp()
81-
ts.FromJsonString(expected_json)
82-
assert result == ts
80+
assert result == expected_timestamp
8381

8482

8583
@pytest.mark.parametrize(
86-
"input_dict,field_name,expected_json_list,description",
84+
"input_dict,field_name,expected_timestamp_list,description",
8785
[
8886
(
8987
{"field": ["2023-01-01T12:00:00Z", "2023-01-02T12:00:00Z"]},
9088
"field",
91-
["2023-01-01T12:00:00Z", "2023-01-02T12:00:00Z"],
89+
[Timestamp(seconds=1672574400), Timestamp(seconds=1672660800)],
9290
"valid repeated timestamps",
9391
),
9492
({}, "field", [], "missing field"),
9593
({"field": None}, "field", None, "None value"),
9694
({"field": []}, "field", [], "empty list"),
9795
],
9896
)
99-
def test_repeated_timestamp(input_dict, field_name, expected_json_list, description):
97+
def test_repeated_timestamp(input_dict, field_name, expected_timestamp_list, description):
10098
"""Test _repeated_timestamp function with various input scenarios."""
10199
result = _repeated_timestamp(input_dict, field_name)
102100

103-
if expected_json_list is None or len(expected_json_list) == 0:
101+
if expected_timestamp_list is None or len(expected_timestamp_list) == 0:
104102
assert result is None
105103
else:
106-
assert len(result) == len(expected_json_list)
104+
assert len(result) == len(expected_timestamp_list)
107105
assert all(isinstance(ts, Timestamp) for ts in result)
108-
for i, expected_json in enumerate(expected_json_list):
109-
ts = Timestamp()
110-
ts.FromJsonString(expected_json)
111-
assert result[i] == ts
106+
for i, expected_timestamp in enumerate(expected_timestamp_list):
107+
assert result[i] == expected_timestamp
112108

113109

114110
@pytest.mark.parametrize(
115-
"input_dict,field_name,expected_result,expected_json,description",
111+
"input_dict,field_name,expected_result,expected_duration,description",
116112
[
117-
({"field": "3600s"}, "field", "duration_object", "3600s", "valid duration"),
113+
({"field": "3600s"}, "field", "duration_object", Duration(seconds=3600), "valid duration"),
118114
({}, "field", None, None, "missing field"),
119115
({"field": None}, "field", None, None, "None value"),
120116
({"field": ""}, "field", None, None, "empty value"),
121117
],
122118
)
123-
def test_duration(input_dict, field_name, expected_result, expected_json, description):
119+
def test_duration(input_dict, field_name, expected_result, expected_duration, description):
124120
"""Test _duration function with various input scenarios."""
125121
result = _duration(input_dict, field_name)
126122

127123
if expected_result is None:
128124
assert result is None
129125
else:
130126
assert isinstance(result, Duration)
131-
dur = Duration()
132-
dur.FromJsonString(expected_json)
133-
assert result == dur
127+
assert result == expected_duration
134128

135129

136130
@pytest.mark.parametrize(
137-
"input_dict,field_name,expected_json_list,description",
131+
"input_dict,field_name,expected_duration_list,description",
138132
[
139-
({"field": ["3600s", "7200s"]}, "field", ["3600s", "7200s"], "valid repeated durations"),
133+
({"field": ["3600s", "7200s"]}, "field", [Duration(seconds=3600), Duration(seconds=7200)], "valid repeated durations"),
140134
({}, "field", [], "missing field"),
141135
({"field": None}, "field", None, "None value"),
142136
({"field": []}, "field", [], "empty list"),
143137
],
144138
)
145-
def test_repeated_duration(input_dict, field_name, expected_json_list, description):
139+
def test_repeated_duration(input_dict, field_name, expected_duration_list, description):
146140
"""Test _repeated_duration function with various input scenarios."""
147141
result = _repeated_duration(input_dict, field_name)
148142

149-
if expected_json_list is None or len(expected_json_list) == 0:
143+
if expected_duration_list is None or len(expected_duration_list) == 0:
150144
assert result is None
151145
else:
152-
assert len(result) == len(expected_json_list)
146+
assert len(result) == len(expected_duration_list)
153147
assert all(isinstance(dur, Duration) for dur in result)
154-
for i, expected_json in enumerate(expected_json_list):
155-
dur = Duration()
156-
dur.FromJsonString(expected_json)
157-
assert result[i] == dur
148+
for i, expected_duration in enumerate(expected_duration_list):
149+
assert result[i] == expected_duration
158150

159151

160152
@pytest.mark.parametrize(
161-
"input_dict,field_name,expected_result,expected_json,description",
153+
"input_dict,field_name,expected_result,expected_fieldmask,description",
162154
[
163-
({"field": "path1,path2"}, "field", "fieldmask_object", "path1,path2", "valid fieldmask"),
155+
({"field": "path1,path2"}, "field", "fieldmask_object", FieldMask(field_mask=["path1", "path2"]), "valid fieldmask"),
164156
({}, "field", None, None, "missing field"),
165157
({"field": None}, "field", None, None, "None value"),
166158
({"field": ""}, "field", None, None, "empty value"),
167159
],
168160
)
169-
def test_fieldmask(input_dict, field_name, expected_result, expected_json, description):
161+
def test_fieldmask(input_dict, field_name, expected_result, expected_fieldmask, description):
170162
"""Test _fieldmask function with various input scenarios."""
171163
result = _fieldmask(input_dict, field_name)
172164

173165
if expected_result is None:
174166
assert result is None
175167
else:
176168
assert isinstance(result, FieldMask)
177-
fm = FieldMask()
178-
fm.FromJsonString(expected_json)
179-
assert result == fm
169+
assert result == expected_fieldmask
180170

181171

182172
@pytest.mark.parametrize(
183-
"input_dict,field_name,expected_json_list,description",
173+
"input_dict,field_name,expected_fieldmask_list,description",
184174
[
185175
(
186176
{"field": ["path1,path2", "path3,path4"]},
187177
"field",
188-
["path1,path2", "path3,path4"],
178+
[FieldMask(field_mask=["path1", "path2"]), FieldMask(field_mask=["path3", "path4"])],
189179
"valid repeated fieldmasks",
190180
),
191181
({}, "field", [], "missing field"),
192182
({"field": None}, "field", None, "None value"),
193183
({"field": []}, "field", [], "empty list"),
194184
],
195185
)
196-
def test_repeated_fieldmask(input_dict, field_name, expected_json_list, description):
186+
def test_repeated_fieldmask(input_dict, field_name, expected_fieldmask_list, description):
197187
"""Test _repeated_fieldmask function with various input scenarios."""
198188
result = _repeated_fieldmask(input_dict, field_name)
199189

200-
if expected_json_list is None or len(expected_json_list) == 0:
190+
if expected_fieldmask_list is None or len(expected_fieldmask_list) == 0:
201191
assert result is None
202192
else:
203-
assert len(result) == len(expected_json_list)
193+
assert len(result) == len(expected_fieldmask_list)
204194
assert all(isinstance(fm, FieldMask) for fm in result)
205-
for i, expected_json in enumerate(expected_json_list):
206-
fm = FieldMask()
207-
fm.FromJsonString(expected_json)
208-
assert result[i] == fm
195+
for i, expected_fieldmask in enumerate(expected_fieldmask_list):
196+
assert result[i] == expected_fieldmask

0 commit comments

Comments
 (0)