@@ -86,6 +86,130 @@ def test___ne__type_differ(self):
86
86
self .assertIs (geo_pt1 .__ne__ (geo_pt2 ), NotImplemented )
87
87
88
88
89
+ class TestFieldPath (unittest .TestCase ):
90
+
91
+ @staticmethod
92
+ def _get_target_class ():
93
+ from google .cloud .firestore_v1beta1 ._helpers import FieldPath
94
+ return FieldPath
95
+
96
+ def _make_one (self , * args , ** kwargs ):
97
+ klass = self ._get_target_class ()
98
+ return klass (* args , ** kwargs )
99
+
100
+ def test_none_fails (self ):
101
+ with self .assertRaises (ValueError ):
102
+ field_path = self ._make_one ('a' , None , 'b' )
103
+
104
+ def test_integer_fails (self ):
105
+ with self .assertRaises (ValueError ):
106
+ field_path = self ._make_one ('a' , 3 , 'b' )
107
+
108
+ def test_iterable_fails (self ):
109
+ with self .assertRaises (ValueError ):
110
+ field_path = self ._make_one ('a' , ['a' ], 'b' )
111
+
112
+ def test_invalid_chars_in_constructor (self ):
113
+ parts = '~*/[].'
114
+ for part in parts :
115
+ field_path = self ._make_one (part )
116
+ self .assertEqual (field_path .parts , (part , ))
117
+
118
+ def test_component (self ):
119
+ field_path = self ._make_one ('a..b' )
120
+ self .assertEquals (field_path .parts , ('a..b' ,))
121
+
122
+ def test_constructor_iterable (self ):
123
+ field_path = self ._make_one ('a' , 'b' , 'c' )
124
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
125
+
126
+ def test_to_api_repr_a (self ):
127
+ parts = 'a'
128
+ field_path = self ._make_one (parts )
129
+ self .assertEqual ('a' , field_path .to_api_repr ())
130
+
131
+ def test_to_api_repr_tick (self ):
132
+ parts = '`'
133
+ field_path = self ._make_one (parts )
134
+ self .assertEqual ('`\``' , field_path .to_api_repr ())
135
+
136
+ def test_to_api_repr_slash (self ):
137
+ parts = '\\ '
138
+ field_path = self ._make_one (parts )
139
+ self .assertEqual (r'`\\`' , field_path .to_api_repr ())
140
+
141
+ def test_to_api_repr_double_slash (self ):
142
+ parts = r'\\'
143
+ field_path = self ._make_one (parts )
144
+ self .assertEqual (r'`\\\\`' , field_path .to_api_repr ())
145
+
146
+ def test_to_api_repr_underscore_valid (self ):
147
+ parts = '_33132'
148
+ field_path = self ._make_one (parts )
149
+ self .assertEqual ('_33132' , field_path .to_api_repr ())
150
+
151
+ def test_to_api_repr_number_invalid (self ):
152
+ parts = '03'
153
+ field_path = self ._make_one (parts )
154
+ self .assertEqual ('`03`' , field_path .to_api_repr ())
155
+
156
+ def test_to_api_repr_valid_part (self ):
157
+ parts = 'a0332432'
158
+ field_path = self ._make_one (parts )
159
+ self .assertEqual ('a0332432' , field_path .to_api_repr ())
160
+
161
+ def test_to_api_repr_chain (self ):
162
+ parts = 'a' , '`' , '\\ ' , '_3' , '03' , 'a03' , '\\ \\ '
163
+ field_path = self ._make_one (* parts )
164
+ self .assertEqual (r'a.`\``.`\\`._3.`03`.a03.`\\\\`' ,
165
+ field_path .to_api_repr ())
166
+
167
+ def test_from_string (self ):
168
+ field_path = self ._get_target_class ().from_string ('a.b.c' )
169
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
170
+
171
+ def test_list_splat (self ):
172
+ parts = ['a' , 'b' , 'c' ]
173
+ field_path = self ._make_one (* parts )
174
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
175
+
176
+ def test_tuple_splat (self ):
177
+ parts = ('a' , 'b' , 'c' )
178
+ field_path = self ._make_one (* parts )
179
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
180
+
181
+ def test_invalid_chars_from_string_fails (self ):
182
+ parts = '~*/[].'
183
+ for part in parts :
184
+ with self .assertRaises (ValueError ):
185
+ field_path = self ._get_target_class ().from_string (part )
186
+
187
+ def test_list_fails (self ):
188
+ parts = ['a' , 'b' , 'c' ]
189
+ with self .assertRaises (ValueError ):
190
+ field_path = self ._make_one (parts )
191
+
192
+ def test_tuple_fails (self ):
193
+ parts = ('a' , 'b' , 'c' )
194
+ with self .assertRaises (ValueError ):
195
+ field_path = self ._make_one (parts )
196
+
197
+ def test_key (self ):
198
+ parts = 'a'
199
+ field_path = self ._make_one ('a321' , 'b456' )
200
+ field_path_same = self ._get_target_class ().from_string ('a321.b456' )
201
+ field_path_different = self ._make_one ('a321' , 'b457' )
202
+ keys = {field_path : '' ,
203
+ field_path_same : '' ,
204
+ field_path_different : ''
205
+ }
206
+ for key in keys :
207
+ if key == field_path_different :
208
+ self .assertNotEqual (key , field_path )
209
+ else :
210
+ self .assertEqual (key , field_path )
211
+
212
+
89
213
class TestFieldPathHelper (unittest .TestCase ):
90
214
91
215
@staticmethod
0 commit comments