Skip to content

Commit 3871a28

Browse files
committed
Add FieldMask class
1 parent 61c4bdc commit 3871a28

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

databricks/sdk/common/types/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
class FieldMask(object):
4+
"""Class for FieldMask message type."""
5+
6+
def ToJsonString(self):
7+
"""Converts FieldMask to string."""
8+
return ','.join(self.paths)
9+
10+
def FromJsonString(self, value):
11+
"""Converts string to FieldMask."""
12+
if not isinstance(value, str):
13+
raise ValueError('FieldMask JSON value not a string: {!r}'.format(value))
14+
if value:
15+
self.paths = value.split(',')
16+
else:
17+
self.paths = []

tests/test_fieldmask.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import pytest
2+
3+
from databricks.sdk.common.types.fieldmask import FieldMask
4+
5+
6+
def test_to_json_string_basic():
7+
"""Test ToJsonString with basic list of paths."""
8+
field_mask = FieldMask()
9+
field_mask.paths = ['field1', 'field2', 'field3']
10+
11+
result = field_mask.ToJsonString()
12+
13+
assert result == 'field1,field2,field3'
14+
15+
16+
def test_to_json_string_single_path():
17+
"""Test ToJsonString with single path."""
18+
field_mask = FieldMask()
19+
field_mask.paths = ['single_field']
20+
21+
result = field_mask.ToJsonString()
22+
23+
assert result == 'single_field'
24+
25+
26+
def test_to_json_string_empty_paths():
27+
"""Test ToJsonString with empty paths list."""
28+
field_mask = FieldMask()
29+
field_mask.paths = []
30+
31+
result = field_mask.ToJsonString()
32+
33+
assert result == ''
34+
35+
36+
def test_to_json_string_nested_paths():
37+
"""Test ToJsonString with nested field paths."""
38+
field_mask = FieldMask()
39+
field_mask.paths = ['user.name', 'user.email', 'address.street']
40+
41+
result = field_mask.ToJsonString()
42+
43+
assert result == 'user.name,user.email,address.street'
44+
45+
46+
def test_from_json_string_basic():
47+
"""Test FromJsonString with basic comma-separated string."""
48+
field_mask = FieldMask()
49+
50+
field_mask.FromJsonString('field1,field2,field3')
51+
52+
assert field_mask.paths == ['field1', 'field2', 'field3']
53+
54+
55+
def test_from_json_string_single_field():
56+
"""Test FromJsonString with single field."""
57+
field_mask = FieldMask()
58+
59+
field_mask.FromJsonString('single_field')
60+
61+
assert field_mask.paths == ['single_field']
62+
63+
64+
def test_from_json_string_empty_string():
65+
"""Test FromJsonString with empty string."""
66+
field_mask = FieldMask()
67+
68+
field_mask.FromJsonString('')
69+
70+
assert field_mask.paths == []
71+
72+
73+
def test_from_json_string_nested_paths():
74+
"""Test FromJsonString with nested field paths."""
75+
field_mask = FieldMask()
76+
77+
field_mask.FromJsonString('user.name,user.email,address.street')
78+
79+
assert field_mask.paths == ['user.name', 'user.email', 'address.street']
80+
81+
82+
def test_from_json_string_with_spaces():
83+
"""Test FromJsonString with spaces around commas."""
84+
field_mask = FieldMask()
85+
86+
field_mask.FromJsonString('field1, field2 , field3')
87+
88+
assert field_mask.paths == ['field1', ' field2 ', ' field3']
89+
90+
91+
def test_from_json_string_non_string_input():
92+
"""Test FromJsonString raises ValueError for non-string input."""
93+
field_mask = FieldMask()
94+
95+
with pytest.raises(ValueError) as exc_info:
96+
field_mask.FromJsonString(123)
97+
98+
assert "FieldMask JSON value not a string: 123" in str(exc_info.value)
99+
100+
101+
def test_from_json_string_none_input():
102+
"""Test FromJsonString raises ValueError for None input."""
103+
field_mask = FieldMask()
104+
105+
with pytest.raises(ValueError) as exc_info:
106+
field_mask.FromJsonString(None)
107+
108+
assert "FieldMask JSON value not a string: None" in str(exc_info.value)
109+
110+
111+
def test_from_json_string_list_input():
112+
"""Test FromJsonString raises ValueError for list input."""
113+
field_mask = FieldMask()
114+
115+
with pytest.raises(ValueError) as exc_info:
116+
field_mask.FromJsonString(['field1', 'field2'])
117+
118+
assert "FieldMask JSON value not a string:" in str(exc_info.value)
119+
120+
121+
def test_from_json_string_dict_input():
122+
"""Test FromJsonString raises ValueError for dict input."""
123+
field_mask = FieldMask()
124+
125+
with pytest.raises(ValueError) as exc_info:
126+
field_mask.FromJsonString({'field': 'value'})
127+
128+
assert "FieldMask JSON value not a string:" in str(exc_info.value)
129+
130+
131+
def test_roundtrip_conversion():
132+
"""Test that ToJsonString and FromJsonString are inverse operations."""
133+
field_mask = FieldMask()
134+
original_paths = ['user.name', 'user.email', 'profile.settings']
135+
field_mask.paths = original_paths
136+
137+
# Convert to string and back.
138+
json_string = field_mask.ToJsonString()
139+
field_mask.FromJsonString(json_string)
140+
141+
assert field_mask.paths == original_paths
142+
143+
144+
def test_roundtrip_conversion_single_field():
145+
"""Test roundtrip conversion with single field."""
146+
field_mask = FieldMask()
147+
original_paths = ['single_field']
148+
field_mask.paths = original_paths
149+
150+
# Convert to string and back.
151+
json_string = field_mask.ToJsonString()
152+
field_mask.FromJsonString(json_string)
153+
154+
assert field_mask.paths == original_paths
155+
156+
157+
def test_roundtrip_conversion_empty():
158+
"""Test roundtrip conversion with empty paths."""
159+
field_mask = FieldMask()
160+
original_paths = []
161+
field_mask.paths = original_paths
162+
163+
# Convert to string and back.
164+
json_string = field_mask.ToJsonString()
165+
field_mask.FromJsonString(json_string)
166+
167+
assert field_mask.paths == original_paths

0 commit comments

Comments
 (0)