def test_update_productset_explicit_name_different_from_constructed( self, get_conn): # Given update_product_set_method = get_conn.return_value.update_product_set update_product_set_method.return_value = None explicit_ps_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST_2, LOC_ID_TEST_2, PRODUCTSET_ID_TEST_2) product_set = ProductSet(name=explicit_ps_name) template_ps_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST, LOC_ID_TEST, PRODUCTSET_ID_TEST) # When # Location and product_set_id are passed in addition to a ProductSet with an explicit name, # but both names differ (constructed != explicit). # Should throw AirflowException in this case. with self.assertRaises(AirflowException) as cm: self.hook.update_product_set( location=LOC_ID_TEST, product_set_id=PRODUCTSET_ID_TEST, product_set=product_set, update_mask=None, project_id=PROJECT_ID_TEST, retry=None, timeout=None, metadata=None, ) err = cm.exception # self.assertIn("The required parameter 'project_id' is missing", str(err)) self.assertTrue(err) self.assertIn( ERR_DIFF_NAMES.format(explicit_name=explicit_ps_name, constructed_name=template_ps_name, label="ProductSet", id_label="productset_id"), str(err)) update_product_set_method.assert_not_called()
def add_product_to_product_set( self, product_set_id: str, product_id: str, location: str = None, project_id: str = None, retry: Retry = None, timeout: float = None, metadata: Sequence[Tuple[str, str]] = None, ) -> None: """ For the documentation see: :py:class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionAddProductToProductSetOperator` """ assert project_id is not None client = self.get_conn() product_name = ProductSearchClient.product_path(project_id, location, product_id) product_set_name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Add Product[name=%s] to Product Set[name=%s]', product_name, product_set_name) client.add_product_to_product_set( name=product_set_name, product=product_name, retry=retry, timeout=timeout, metadata=metadata ) self.log.info('Product added to Product Set')
def remove_product_from_product_set( self, product_set_id: str, product_id: str, location: Optional[str] = None, project_id: Optional[str] = None, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None, ) -> None: """ For the documentation see: :py:class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionRemoveProductFromProductSetOperator` # pylint: disable=line-too-long # noqa """ if not project_id: raise ValueError("Project ID should be set.") client = self.get_conn() product_name = ProductSearchClient.product_path( project_id, location, product_id) product_set_name = ProductSearchClient.product_set_path( project_id, location, product_set_id) self.log.info('Remove Product[name=%s] from Product Set[name=%s]', product_name, product_set_name) client.remove_product_from_product_set(name=product_set_name, product=product_name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('Product removed from Product Set')
def add_product_to_product_set( self, product_set_id: str, product_id: str, project_id: str, location: Optional[str] = None, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None, ) -> None: """ For the documentation see: :py:class:`~airflow.providers.google.cloud.operators.vision.CloudVisionAddProductToProductSetOperator` """ client = self.get_conn() product_name = ProductSearchClient.product_path(project_id, location, product_id) product_set_name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Add Product[name=%s] to Product Set[name=%s]', product_name, product_set_name) client.add_product_to_product_set( name=product_set_name, product=product_name, retry=retry, timeout=timeout, metadata=metadata ) self.log.info('Product added to Product Set')
def test_create_productset_autogenerated_id(self, get_conn): # Given autogenerated_id = 'autogen-id' response_product_set = ProductSet( name=ProductSearchClient.product_set_path(PROJECT_ID_TEST, LOC_ID_TEST, autogenerated_id) ) create_product_set_method = get_conn.return_value.create_product_set create_product_set_method.return_value = response_product_set parent = ProductSearchClient.location_path(PROJECT_ID_TEST, LOC_ID_TEST) product_set = ProductSet() # When result = self.hook.create_product_set( location=LOC_ID_TEST, product_set_id=None, product_set=product_set, project_id=PROJECT_ID_TEST ) # Then # ProductSet ID was not provided in the method call above. Should be extracted from the API response # and returned. self.assertEqual(result, autogenerated_id) create_product_set_method.assert_called_once_with( parent=parent, product_set=product_set, product_set_id=None, retry=None, timeout=None, metadata=None, )
def _get_entity_name(is_product, project_id, location, entity_id): if is_product: return ProductSearchClient.product_path(project_id, location, entity_id) else: return ProductSearchClient.product_set_path( project_id, location, entity_id)
def remove_product_from_product_set( self, product_set_id: str, product_id: str, project_id: str, location: Optional[str] = None, retry: Union[Retry, _MethodDefault] = DEFAULT, timeout: Optional[float] = None, metadata: Sequence[Tuple[str, str]] = (), ) -> None: """ For the documentation see: :py:class:`~airflow.providers.google.cloud.operators.vision.CloudVisionRemoveProductFromProductSetOperator` """ client = self.get_conn() product_name = ProductSearchClient.product_path(project_id, location, product_id) product_set_name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Remove Product[name=%s] from Product Set[name=%s]', product_name, product_set_name) client.remove_product_from_product_set( name=product_set_name, product=product_name, retry=retry, timeout=timeout, metadata=metadata ) self.log.info('Product removed from Product Set')
def test_update_productset_no_explicit_name(self, get_conn): # Given product_set = ProductSet() update_product_set_method = get_conn.return_value.update_product_set update_product_set_method.return_value = product_set productset_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST, LOC_ID_TEST, PRODUCTSET_ID_TEST ) # When result = self.hook.update_product_set( location=LOC_ID_TEST, product_set_id=PRODUCTSET_ID_TEST, product_set=product_set, update_mask=None, project_id=PROJECT_ID_TEST, retry=None, timeout=None, metadata=None, ) # Then self.assertEqual(result, MessageToDict(product_set)) update_product_set_method.assert_called_once_with( product_set=ProductSet(name=productset_name), metadata=None, retry=None, timeout=None, update_mask=None, )
def get_product_set( self, location: str, product_set_id: str, project_id: Optional[str] = None, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None) -> Dict: """ For the documentation see: :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetGetOperator` """ if not project_id: raise ValueError("Project ID should be set.") client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Retrieving ProductSet: %s', name) response = client.get_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet retrieved.') self.log.debug('ProductSet retrieved:\n%s', response) return MessageToDict(response)
def test_update_productset_explicit_name_missing_params_for_constructed_name( self, location, product_set_id, get_conn ): # Given explicit_ps_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST_2, LOC_ID_TEST_2, PRODUCTSET_ID_TEST_2 ) product_set = ProductSet(name=explicit_ps_name) update_product_set_method = get_conn.return_value.update_product_set update_product_set_method.return_value = product_set # When result = self.hook.update_product_set( location=location, product_set_id=product_set_id, product_set=product_set, update_mask=None, project_id=PROJECT_ID_TEST, retry=None, timeout=None, metadata=None, ) # Then self.assertEqual(result, MessageToDict(product_set)) update_product_set_method.assert_called_once_with( product_set=ProductSet(name=explicit_ps_name), metadata=None, retry=None, timeout=None, update_mask=None, )
def add_product_to_product_set( self, product_set_id: str, product_id: str, location: Optional[str] = None, project_id: Optional[str] = None, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None, ) -> None: """ For the documentation see: :py:class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionAddProductToProductSetOperator` """ if not project_id: raise ValueError("Project ID should be set.") client = self.get_conn() product_name = ProductSearchClient.product_path( project_id, location, product_id) product_set_name = ProductSearchClient.product_set_path( project_id, location, product_set_id) self.log.info('Add Product[name=%s] to Product Set[name=%s]', product_name, product_set_name) client.add_product_to_product_set(name=product_set_name, product=product_name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('Product added to Product Set')
def remove_product_from_product_set( self, product_set_id, product_id, location=None, project_id=None, retry=None, timeout=None, metadata=None, ): """ For the documentation see: :py:class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionRemoveProductFromProductSetOperator` """ client = self.get_conn() product_name = ProductSearchClient.product_path(project_id, location, product_id) product_set_name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Remove Product[name=%s] from Product Set[name=%s]', product_name, product_set_name) client.remove_product_from_product_set( name=product_set_name, product=product_name, retry=retry, timeout=timeout, metadata=metadata ) self.log.info('Product removed from Product Set')
def remove_product_from_product_set( self, product_set_id, product_id, location=None, project_id=None, retry=None, timeout=None, metadata=None, ): """ For the documentation see: :py:class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionRemoveProductFromProductSetOperator` """ client = self.get_conn() product_name = ProductSearchClient.product_path( project_id, location, product_id) product_set_name = ProductSearchClient.product_set_path( project_id, location, product_set_id) self.log.info('Remove Product[name=%s] from Product Set[name=%s]', product_name, product_set_name) client.remove_product_from_product_set(name=product_set_name, product=product_name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('Product removed from Product Set')
def get_product_set(self, location, product_set_id, project_id=None, retry=None, timeout=None, metadata=None): """ For the documentation see: :py:class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetGetOperator` """ client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Retrieving ProductSet: %s', name) response = self._handle_request( lambda **kwargs: client.get_product_set(**kwargs), name=name, retry=retry, timeout=timeout, metadata=metadata, ) self.log.info('ProductSet retrieved.') self.log.debug('ProductSet retrieved:\n%s', response) return MessageToDict(response)
def delete_product_set( self, location, product_set_id, project_id=None, retry=None, timeout=None, metadata=None ): """ For the documentation see: :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetDeleteOperator` """ client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Deleting ProductSet: %s', name) client.delete_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet with the name [%s] deleted.', name)
def delete_product_set( self, location, product_set_id, project_id=None, retry=None, timeout=None, metadata=None ): """ For the documentation see: :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetDeleteOperator` """ client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Deleting ProductSet: %s', name) client.delete_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet with the name [%s] deleted.', name)
def test_delete_productset(self, get_conn): # Given delete_product_set_method = get_conn.return_value.delete_product_set delete_product_set_method.return_value = None name = ProductSearchClient.product_set_path(PROJECT_ID_TEST, LOC_ID_TEST, PRODUCTSET_ID_TEST) # When response = self.hook.delete_product_set( location=LOC_ID_TEST, product_set_id=PRODUCTSET_ID_TEST, project_id=PROJECT_ID_TEST ) # Then self.assertIsNone(response) delete_product_set_method.assert_called_once_with(name=name, retry=None, timeout=None, metadata=None)
def test_update_productset_explicit_name_different_from_constructed( self, get_conn): # Given update_product_set_method = get_conn.return_value.update_product_set update_product_set_method.return_value = None hook = self.vision_hook_default_project_id explicit_ps_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST_2, LOC_ID_TEST_2, PRODUCTSET_ID_TEST_2) product_set = ProductSet(name=explicit_ps_name) template_ps_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST, LOC_ID_TEST, PRODUCTSET_ID_TEST) # When # Location and product_set_id are passed in addition to a ProductSet with an explicit name, # but both names differ (constructed != explicit). # Should throw AirflowException in this case. with self.assertRaises(AirflowException) as cm: hook.update_product_set( location=LOC_ID_TEST, product_set_id=PRODUCTSET_ID_TEST, product_set=product_set, update_mask=None, project_id=PROJECT_ID_TEST, retry=None, timeout=None, metadata=None, ) err = cm.exception # self.assertIn("The required parameter 'project_id' is missing", str(err)) self.assertTrue(err) self.assertIn( "The ProductSet name provided in the object ({}) is different than the name " "created from the input parameters ({}). Please either: 1) Remove the ProductSet " "name, 2) Remove the location and productset_id parameters, 3) Unify the " "ProductSet name and input parameters.".format( explicit_ps_name, template_ps_name), str(err), ) update_product_set_method.assert_not_called()
def test_update_productset_explicit_name_different_from_constructed(self, get_conn): # Given update_product_set_method = get_conn.return_value.update_product_set update_product_set_method.return_value = None hook = self.vision_hook_default_project_id explicit_ps_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST_2, LOC_ID_TEST_2, PRODUCTSET_ID_TEST_2 ) product_set = ProductSet(name=explicit_ps_name) template_ps_name = ProductSearchClient.product_set_path( PROJECT_ID_TEST, LOC_ID_TEST, PRODUCTSET_ID_TEST ) # When # Location and product_set_id are passed in addition to a ProductSet with an explicit name, # but both names differ (constructed != explicit). # Should throw AirflowException in this case. with self.assertRaises(AirflowException) as cm: hook.update_product_set( location=LOC_ID_TEST, product_set_id=PRODUCTSET_ID_TEST, product_set=product_set, update_mask=None, project_id=PROJECT_ID_TEST, retry=None, timeout=None, metadata=None, ) err = cm.exception # self.assertIn("The required parameter 'project_id' is missing", str(err)) self.assertTrue(err) self.assertIn( "The ProductSet name provided in the object ({}) is different than the name " "created from the input parameters ({}). Please either: 1) Remove the ProductSet " "name, 2) Remove the location and productset_id parameters, 3) Unify the " "ProductSet name and input parameters.".format(explicit_ps_name, template_ps_name), str(err), ) update_product_set_method.assert_not_called()
def test_get_productset(self, get_conn): # Given name = ProductSearchClient.product_set_path(PROJECT_ID_TEST, LOC_ID_TEST, PRODUCTSET_ID_TEST) response_product_set = ProductSet(name=name) get_product_set_method = get_conn.return_value.get_product_set get_product_set_method.return_value = response_product_set # When response = self.hook.get_product_set( location=LOC_ID_TEST, product_set_id=PRODUCTSET_ID_TEST, project_id=PROJECT_ID_TEST ) # Then self.assertTrue(response) self.assertEqual(response, MessageToDict(response_product_set)) get_product_set_method.assert_called_once_with(name=name, retry=None, timeout=None, metadata=None)
def get_product_set( self, location, product_set_id, project_id=None, retry=None, timeout=None, metadata=None ): """ For the documentation see: :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetGetOperator` """ client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Retrieving ProductSet: %s', name) response = client.get_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet retrieved.') self.log.debug('ProductSet retrieved:\n%s', response) return MessageToDict(response)
def delete_product_set( self, location: str, product_set_id: str, project_id: str, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None, ) -> None: """ For the documentation see: :class:`~airflow.providers.google.cloud.operators.vision.CloudVisionDeleteProductSetOperator` """ client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Deleting ProductSet: %s', name) client.delete_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet with the name [%s] deleted.', name)
def get_product_set( self, location: str, product_set_id: str, project_id: str, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None ) -> Dict: """ For the documentation see: :class:`~airflow.providers.google.cloud.operators.vision.CloudVisionGetProductSetOperator` """ client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Retrieving ProductSet: %s', name) response = client.get_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet retrieved.') self.log.debug('ProductSet retrieved:\n%s', response) return MessageToDict(response)
def delete_product_set( self, location: str, product_set_id: str, project_id: Optional[str] = None, retry: Retry = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None ): """ For the documentation see: :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetDeleteOperator` """ assert project_id is not None client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Deleting ProductSet: %s', name) client.delete_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet with the name [%s] deleted.', name)
def get_product_set( self, location: str, product_set_id: str, project_id: str = None, retry: Retry = None, timeout: float = None, metadata: Sequence[Tuple[str, str]] = None ) -> Dict: """ For the documentation see: :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetGetOperator` """ assert project_id is not None client = self.get_conn() name = ProductSearchClient.product_set_path(project_id, location, product_set_id) self.log.info('Retrieving ProductSet: %s', name) response = client.get_product_set(name=name, retry=retry, timeout=timeout, metadata=metadata) self.log.info('ProductSet retrieved.') self.log.debug('ProductSet retrieved:\n%s', response) return MessageToDict(response)
def _get_entity_name(is_product, project_id, location, entity_id): if is_product: return ProductSearchClient.product_path(project_id, location, entity_id) else: return ProductSearchClient.product_set_path(project_id, location, entity_id)