@@ -124,75 +124,97 @@ def test_detect_logos_gcs(self):
124124@unittest .skipUnless (PROJECT_ID , "PROJECT_ID not set in environment." )
125125class TestVisionClientProductSearch (VisionSystemTestBase ):
126126 def setUp (self ):
127+ VisionSystemTestBase .setUp (self )
128+ self .reference_images_to_delete = []
129+ self .products_to_delete = []
127130 self .product_sets_to_delete = []
128131 self .location = "us-west1"
129132 self .location_path = self .ps_client .location_path (
130133 project = PROJECT_ID , location = self .location
131134 )
132135
133136 def tearDown (self ):
137+ VisionSystemTestBase .tearDown (self )
138+ for reference_image in self .reference_images_to_delete :
139+ self .ps_client .delete_reference_image (name = reference_image )
140+ for product in self .products_to_delete :
141+ self .ps_client .delete_product (name = product )
134142 for product_set in self .product_sets_to_delete :
135143 self .ps_client .delete_product_set (name = product_set )
136144
137145 def test_create_product_set (self ):
146+ # Create a ProductSet.
138147 product_set = vision .types .ProductSet (display_name = "display name" )
139148 product_set_id = "set" + unique_resource_id ()
140149 product_set_path = self .ps_client .product_set_path (
141150 project = PROJECT_ID , location = self .location , product_set = product_set_id
142151 )
143- self .product_sets_to_delete .append (product_set_path )
144152 response = self .ps_client .create_product_set (
145153 parent = self .location_path ,
146154 product_set = product_set ,
147155 product_set_id = product_set_id ,
148156 )
157+ self .product_sets_to_delete .append (response .name )
158+ # Verify the ProductSet was successfully created.
149159 self .assertEqual (response .name , product_set_path )
150160
151161 def test_get_product_set (self ):
162+ # Create a ProductSet.
152163 product_set = vision .types .ProductSet (display_name = "display name" )
153164 product_set_id = "set" + unique_resource_id ()
154165 product_set_path = self .ps_client .product_set_path (
155166 project = PROJECT_ID , location = self .location , product_set = product_set_id
156167 )
157- self .product_sets_to_delete .append (product_set_path )
158168 response = self .ps_client .create_product_set (
159169 parent = self .location_path ,
160170 product_set = product_set ,
161171 product_set_id = product_set_id ,
162172 )
173+ self .product_sets_to_delete .append (response .name )
163174 self .assertEqual (response .name , product_set_path )
175+ # Get the ProductSet.
164176 get_response = self .ps_client .get_product_set (name = product_set_path )
165177 self .assertEqual (get_response .name , product_set_path )
166178
167179 def test_list_product_sets (self ):
180+ # Create a ProductSet.
168181 product_set = vision .types .ProductSet (display_name = "display name" )
169182 product_set_id = "set" + unique_resource_id ()
170183 product_set_path = self .ps_client .product_set_path (
171184 project = PROJECT_ID , location = self .location , product_set = product_set_id
172185 )
173- self .product_sets_to_delete .append (product_set_path )
174186 response = self .ps_client .create_product_set (
175187 parent = self .location_path ,
176188 product_set = product_set ,
177189 product_set_id = product_set_id ,
178190 )
191+ self .product_sets_to_delete .append (response .name )
179192 self .assertEqual (response .name , product_set_path )
180- product_sets = self .ps_client .list_product_sets (parent = self .location_path )
181- self .assertGreater (len (list (product_sets )), 0 )
193+ # Verify ProductSets can be listed.
194+ product_sets_iterator = self .ps_client .list_product_sets (
195+ parent = self .location_path
196+ )
197+ product_sets_exist = False
198+ for product_set in product_sets_iterator :
199+ product_sets_exist = True
200+ break
201+ self .assertTrue (product_sets_exist )
182202
183203 def test_update_product_set (self ):
204+ # Create a ProductSet.
184205 product_set = vision .types .ProductSet (display_name = "display name" )
185206 product_set_id = "set" + unique_resource_id ()
186207 product_set_path = self .ps_client .product_set_path (
187208 project = PROJECT_ID , location = self .location , product_set = product_set_id
188209 )
189- self .product_sets_to_delete .append (product_set_path )
190210 response = self .ps_client .create_product_set (
191211 parent = self .location_path ,
192212 product_set = product_set ,
193213 product_set_id = product_set_id ,
194214 )
215+ self .product_sets_to_delete .append (response .name )
195216 self .assertEqual (response .name , product_set_path )
217+ # Update the ProductSet.
196218 new_display_name = "updated name"
197219 updated_product_set_request = vision .types .ProductSet (
198220 name = product_set_path , display_name = new_display_name
@@ -202,3 +224,175 @@ def test_update_product_set(self):
202224 product_set = updated_product_set_request , update_mask = update_mask
203225 )
204226 self .assertEqual (updated_product_set .display_name , new_display_name )
227+
228+ def test_create_product (self ):
229+ # Create a Product.
230+ product = vision .types .Product (
231+ display_name = "product display name" , product_category = "apparel"
232+ )
233+ product_id = "product" + unique_resource_id ()
234+ product_path = self .ps_client .product_path (
235+ project = PROJECT_ID , location = self .location , product = product_id
236+ )
237+ response = self .ps_client .create_product (
238+ parent = self .location_path , product = product , product_id = product_id
239+ )
240+ self .products_to_delete .append (response .name )
241+ # Verify the Product was successfully created.
242+ self .assertEqual (response .name , product_path )
243+
244+ def test_get_product (self ):
245+ # Create a Product.
246+ product = vision .types .Product (
247+ display_name = "product display name" , product_category = "apparel"
248+ )
249+ product_id = "product" + unique_resource_id ()
250+ product_path = self .ps_client .product_path (
251+ project = PROJECT_ID , location = self .location , product = product_id
252+ )
253+ response = self .ps_client .create_product (
254+ parent = self .location_path , product = product , product_id = product_id
255+ )
256+ self .products_to_delete .append (response .name )
257+ self .assertEqual (response .name , product_path )
258+ # Get the Product.
259+ get_response = self .ps_client .get_product (name = product_path )
260+ self .assertEqual (get_response .name , product_path )
261+
262+ def test_update_product (self ):
263+ # Create a Product.
264+ product = vision .types .Product (
265+ display_name = "product display name" , product_category = "apparel"
266+ )
267+ product_id = "product" + unique_resource_id ()
268+ product_path = self .ps_client .product_path (
269+ project = PROJECT_ID , location = self .location , product = product_id
270+ )
271+ response = self .ps_client .create_product (
272+ parent = self .location_path , product = product , product_id = product_id
273+ )
274+ self .products_to_delete .append (response .name )
275+ self .assertEqual (response .name , product_path )
276+ # Update the Product.
277+ new_display_name = "updated product name"
278+ updated_product_request = vision .types .Product (
279+ name = product_path , display_name = new_display_name
280+ )
281+ update_mask = vision .types .FieldMask (paths = ["display_name" ])
282+ updated_product = self .ps_client .update_product (
283+ product = updated_product_request , update_mask = update_mask
284+ )
285+ self .assertEqual (updated_product .display_name , new_display_name )
286+
287+ def test_list_products (self ):
288+ # Create a Product.
289+ product = vision .types .Product (
290+ display_name = "product display name" , product_category = "apparel"
291+ )
292+ product_id = "product" + unique_resource_id ()
293+ product_path = self .ps_client .product_path (
294+ project = PROJECT_ID , location = self .location , product = product_id
295+ )
296+ response = self .ps_client .create_product (
297+ parent = self .location_path , product = product , product_id = product_id
298+ )
299+ self .products_to_delete .append (response .name )
300+ self .assertEqual (response .name , product_path )
301+ # Verify Products can be listed.
302+ products_iterator = self .ps_client .list_products (parent = self .location_path )
303+ products_exist = False
304+ for product in products_iterator :
305+ products_exist = True
306+ break
307+ self .assertTrue (products_exist )
308+
309+ def test_list_products_in_product_set (self ):
310+ # Create a ProductSet.
311+ product_set = vision .types .ProductSet (display_name = "display name" )
312+ product_set_id = "set" + unique_resource_id ()
313+ product_set_path = self .ps_client .product_set_path (
314+ project = PROJECT_ID , location = self .location , product_set = product_set_id
315+ )
316+ response = self .ps_client .create_product_set (
317+ parent = self .location_path ,
318+ product_set = product_set ,
319+ product_set_id = product_set_id ,
320+ )
321+ self .product_sets_to_delete .append (response .name )
322+ self .assertEqual (response .name , product_set_path )
323+ # Create a Product.
324+ product = vision .types .Product (
325+ display_name = "product display name" , product_category = "apparel"
326+ )
327+ product_id = "product" + unique_resource_id ()
328+ product_path = self .ps_client .product_path (
329+ project = PROJECT_ID , location = self .location , product = product_id
330+ )
331+ response = self .ps_client .create_product (
332+ parent = self .location_path , product = product , product_id = product_id
333+ )
334+ self .products_to_delete .append (response .name )
335+ self .assertEqual (response .name , product_path )
336+ # Add the Product to the ProductSet.
337+ self .ps_client .add_product_to_product_set (
338+ name = product_set_path , product = product_path
339+ )
340+ # List the Products in the ProductSet.
341+ listed_products = list (
342+ self .ps_client .list_products_in_product_set (name = product_set_path )
343+ )
344+ self .assertEqual (len (listed_products ), 1 )
345+ self .assertEqual (listed_products [0 ].name , product_path )
346+ # Remove the Product from the ProductSet.
347+ self .ps_client .remove_product_from_product_set (
348+ name = product_set_path , product = product_path
349+ )
350+
351+ def test_reference_image (self ):
352+ # Create a Product.
353+ product = vision .types .Product (
354+ display_name = "product display name" , product_category = "apparel"
355+ )
356+ product_id = "product" + unique_resource_id ()
357+ product_path = self .ps_client .product_path (
358+ project = PROJECT_ID , location = self .location , product = product_id
359+ )
360+ response = self .ps_client .create_product (
361+ parent = self .location_path , product = product , product_id = product_id
362+ )
363+ self .products_to_delete .append (response .name )
364+ self .assertEqual (response .name , product_path )
365+ # Upload image to gcs.
366+ blob_name = "faces.jpg"
367+ blob = self .test_bucket .blob (blob_name )
368+ self .to_delete_by_case .append (blob )
369+ with io .open (FACE_FILE , "rb" ) as image_file :
370+ blob .upload_from_file (image_file )
371+ gcs_uri = "gs://{bucket}/{blob}" .format (
372+ bucket = self .test_bucket .name , blob = blob_name
373+ )
374+ # Create a ReferenceImage.
375+ reference_image_id = "reference_image" + unique_resource_id ()
376+ reference_image_path = self .ps_client .reference_image_path (
377+ project = PROJECT_ID ,
378+ location = self .location ,
379+ product = product_id ,
380+ reference_image = reference_image_id ,
381+ )
382+ reference_image = vision .types .ReferenceImage (uri = gcs_uri )
383+ response = self .ps_client .create_reference_image (
384+ parent = product_path ,
385+ reference_image = reference_image ,
386+ reference_image_id = reference_image_id ,
387+ )
388+ self .reference_images_to_delete .append (response .name )
389+ self .assertEqual (response .name , reference_image_path )
390+ # Get the ReferenceImage.
391+ get_response = self .ps_client .get_reference_image (name = reference_image_path )
392+ self .assertEqual (get_response .name , reference_image_path )
393+ # List the ReferenceImages in the Product.
394+ listed_reference_images = list (
395+ self .ps_client .list_reference_images (parent = product_path )
396+ )
397+ self .assertEqual (len (listed_reference_images ), 1 )
398+ self .assertEqual (listed_reference_images [0 ].name , reference_image_path )
0 commit comments