Skip to content

Commit 03dbc40

Browse files
committed
Adding retry to test_detect_web_images_from_gcs() system test.
Flaky in: https://circleci.com/gh/GoogleCloudPlatform/google-cloud-python/1694
1 parent 9870057 commit 03dbc40

File tree

1 file changed

+76
-23
lines changed

1 file changed

+76
-23
lines changed

vision/tests/system.py

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""System tests for Vision API."""
1616

17+
import functools
1718
import os
1819
import unittest
1920

@@ -27,6 +28,7 @@
2728
from google.cloud.vision.feature import FeatureTypes
2829

2930
from test_utils.retry import RetryErrors
31+
from test_utils.retry import RetryResult
3032
from test_utils.system import unique_resource_id
3133

3234

@@ -672,44 +674,90 @@ def tearDown(self):
672674
for value in self.to_delete_by_case:
673675
value.delete()
674676

675-
def _assert_web_entity(self, web_entity):
677+
def _check_web_entity(self, web_entity):
676678
from google.cloud.vision.web import WebEntity
677679

678-
self.assertIsInstance(web_entity, WebEntity)
679-
self.assertIsInstance(web_entity.entity_id, six.text_type)
680-
self.assertIsInstance(web_entity.score, float)
681-
self.assertIsInstance(web_entity.description, six.text_type)
680+
if not isinstance(web_entity, WebEntity):
681+
return False
682+
if not isinstance(web_entity.entity_id, six.text_type):
683+
return False
684+
if not isinstance(web_entity.score, float):
685+
return False
686+
if not isinstance(web_entity.description, six.text_type):
687+
return False
682688

683-
def _assert_web_image(self, web_image):
689+
return True
690+
691+
def _assert_web_entity(self, web_entity):
692+
return_value = self._check_web_entity(web_entity)
693+
self.assertTrue(return_value)
694+
695+
def _check_web_image(self, web_image):
684696
from google.cloud.vision.web import WebImage
685697

686-
self.assertIsInstance(web_image, WebImage)
687-
self.assertIsInstance(web_image.url, six.text_type)
688-
self.assertIsInstance(web_image.score, float)
698+
if not isinstance(web_image, WebImage):
699+
return False
689700

690-
def _assert_web_page(self, web_page):
701+
if not isinstance(web_image.url, six.text_type):
702+
return False
703+
704+
if not isinstance(web_image.score, float):
705+
return False
706+
707+
return True
708+
709+
def _assert_web_image(self, web_image):
710+
return_value = self._check_web_image(web_image)
711+
self.assertTrue(return_value)
712+
713+
def _check_web_page(self, web_page):
691714
from google.cloud.vision.web import WebPage
692715

693-
self.assertIsInstance(web_page, WebPage)
694-
self.assertIsInstance(web_page.url, six.text_type)
695-
self.assertIsInstance(web_page.score, float)
716+
if not isinstance(web_page, WebPage):
717+
return False
696718

697-
def _assert_web_images(self, web_images, limit):
698-
self.assertEqual(len(web_images.web_entities), limit)
719+
if not isinstance(web_page.url, six.text_type):
720+
return False
721+
722+
if not isinstance(web_page.score, float):
723+
return False
724+
725+
return True
726+
727+
def _assert_web_page(self, web_page):
728+
return_value = self._check_web_page(web_page)
729+
self.assertTrue(return_value)
730+
731+
def _check_web_images(self, web_images, limit):
732+
if len(web_images.web_entities) != limit:
733+
return False
699734
for web_entity in web_images.web_entities:
700-
self._assert_web_entity(web_entity)
735+
if not self._check_web_entity(web_entity):
736+
return False
701737

702-
self.assertEqual(len(web_images.full_matching_images), limit)
738+
if len(web_images.full_matching_images) != limit:
739+
return False
703740
for web_image in web_images.full_matching_images:
704-
self._assert_web_image(web_image)
741+
if not self._check_web_image(web_image):
742+
return False
705743

706-
self.assertEqual(len(web_images.partial_matching_images), limit)
744+
if len(web_images.partial_matching_images) != limit:
745+
return False
707746
for web_image in web_images.partial_matching_images:
708-
self._assert_web_image(web_image)
747+
if not self._check_web_image(web_image):
748+
return False
709749

710-
self.assertEqual(len(web_images.pages_with_matching_images), limit)
750+
if len(web_images.pages_with_matching_images) != limit:
751+
return False
711752
for web_page in web_images.pages_with_matching_images:
712-
self._assert_web_page(web_page)
753+
if not self._check_web_page(web_page):
754+
return False
755+
756+
return True
757+
758+
def _assert_web_images(self, web_images, limit):
759+
return_value = self._check_web_images(web_images, limit)
760+
self.assertTrue(return_value)
713761

714762
@RetryErrors(unittest.TestCase.failureException)
715763
def test_detect_web_images_from_content(self):
@@ -733,7 +781,12 @@ def test_detect_web_images_from_gcs(self):
733781

734782
image = client.image(source_uri=source_uri)
735783
limit = 5
736-
web_images = image.detect_web(limit=limit)
784+
785+
images_good = functools.partial(self._check_web_images, limit=limit)
786+
images_good.__name__ = 'images_good' # partial() has no name.
787+
retry = RetryResult(images_good)
788+
web_images = retry(image.detect_web)(limit=limit)
789+
737790
self._assert_web_images(web_images, limit)
738791

739792
def test_detect_web_images_from_filename(self):

0 commit comments

Comments
 (0)