@@ -86,6 +86,116 @@ 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_empty_string_inside_string_fails (self ):
101
+ with self .assertRaises (ValueError ):
102
+ field_path = self ._make_one ('a..b' )
103
+
104
+ def test_none_fails (self ):
105
+ with self .assertRaises (ValueError ):
106
+ field_path = self ._make_one ('a' , None , 'b' )
107
+
108
+ def test_integer_fails (self ):
109
+ with self .assertRaises (ValueError ):
110
+ field_path = self ._make_one ('a' , 3 , 'b' )
111
+
112
+ def test_iterable_fails (self ):
113
+ with self .assertRaises (ValueError ):
114
+ field_path = self ._make_one ('a' , ['a' ], 'b' )
115
+
116
+ def test_invalid_chars (self ):
117
+ parts = '~*/[].'
118
+ for part in parts :
119
+ with self .assertRaises (ValueError ):
120
+ field_path = self ._make_one (part )
121
+
122
+ def test_list (self ):
123
+ parts = ['a' , 'b' , 'c' ]
124
+ field_path = self ._make_one (parts )
125
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
126
+
127
+ def test_tuple (self ):
128
+ parts = ('a' , 'b' , 'c' )
129
+ field_path = self ._make_one (parts )
130
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
131
+
132
+ def test_constructor_string (self ):
133
+ field_path = self ._make_one ('a.b.c' )
134
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
135
+
136
+ def test_constructor_iterable (self ):
137
+ field_path = self ._make_one ('a' , 'b' , 'c' )
138
+ self .assertEqual (field_path .parts , ('a' , 'b' , 'c' ))
139
+
140
+ def test_to_api_repr_a (self ):
141
+ parts = 'a'
142
+ field_path = self ._make_one (parts )
143
+ self .assertEqual ('a' , field_path .to_api_repr ())
144
+
145
+ def test_to_api_repr_tick (self ):
146
+ parts = '`'
147
+ field_path = self ._make_one (parts )
148
+ self .assertEqual ('`\``' , field_path .to_api_repr ())
149
+
150
+ def test_to_api_repr_slash (self ):
151
+ parts = '\\ '
152
+ field_path = self ._make_one (parts )
153
+ self .assertEqual ('`\\ `' , field_path .to_api_repr ())
154
+
155
+ def test_to_api_repr_double_slash (self ):
156
+ parts = '\\ \\ '
157
+ field_path = self ._make_one (parts )
158
+ self .assertEqual ('`\\ \\ `' , field_path .to_api_repr ())
159
+
160
+ def test_to_api_repr_underscore_valid (self ):
161
+ parts = '_33132'
162
+ field_path = self ._make_one (parts )
163
+ self .assertEqual ('_33132' , field_path .to_api_repr ())
164
+
165
+ def test_to_api_repr_number_invalid (self ):
166
+ parts = '03'
167
+ field_path = self ._make_one (parts )
168
+ self .assertEqual ('`03`' , field_path .to_api_repr ())
169
+
170
+ def test_to_api_repr_valid_part (self ):
171
+ parts = 'a0332432'
172
+ field_path = self ._make_one (parts )
173
+ self .assertEqual ('a0332432' , field_path .to_api_repr ())
174
+
175
+ def test_to_api_repr_chain (self ):
176
+ parts = 'a' , '`' , '\\ ' , '_3' , '03' , 'a03' , '\\ \\ '
177
+ field_path = self ._make_one (parts )
178
+ self .assertEqual ('a.`\``.`\\ `._3.`03`.a03.`\\ \\ `' ,
179
+ field_path .to_api_repr ())
180
+
181
+ def test_key (self ):
182
+ parts = 'a'
183
+ field_path = self ._make_one ('a321' , 'b456' )
184
+ field_path_same_str = self ._make_one ('a321.b456' )
185
+ field_path_same_iter = self ._make_one (['a321' , 'b456' ])
186
+ field_path_different = self ._make_one ('a321' , 'b457' )
187
+ keys = {field_path : '' ,
188
+ field_path_same_str : '' ,
189
+ field_path_same_iter : '' ,
190
+ field_path_different : ''
191
+ }
192
+ for key in keys :
193
+ if key == field_path_different :
194
+ self .assertNotEqual (key , field_path )
195
+ else :
196
+ self .assertEqual (key , field_path )
197
+
198
+
89
199
class TestFieldPathHelper (unittest .TestCase ):
90
200
91
201
@staticmethod
0 commit comments