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 test_create_productset_explicit_id(self, get_conn):
        # Given
        create_product_set_method = get_conn.return_value.create_product_set
        create_product_set_method.return_value = None
        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=PRODUCTSET_ID_TEST,
            product_set=product_set,
            project_id=PROJECT_ID_TEST,
            retry=None,
            timeout=None,
            metadata=None,
        )

        # Then
        # ProductSet ID was provided explicitly in the method call above, should be returned from the method
        self.assertEqual(result, PRODUCTSET_ID_TEST)
        create_product_set_method.assert_called_once_with(
            parent=parent,
            product_set=product_set,
            product_set_id=PRODUCTSET_ID_TEST,
            retry=None,
            timeout=None,
            metadata=None,
        )
 def test_create_productset_autogenerated_id_wrong_api_response(self, get_conn):
     # Given
     response_product_set = None
     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
     with self.assertRaises(AirflowException) as cm:
         self.hook.create_product_set(
             location=LOC_ID_TEST,
             product_set_id=None,
             product_set=product_set,
             project_id=PROJECT_ID_TEST,
             retry=None,
             timeout=None,
             metadata=None,
         )
     # Then
     # API response was wrong (None) and thus ProductSet ID extraction should fail.
     err = cm.exception
     self.assertIn('Unable to get name from response...', str(err))
     create_product_set_method.assert_called_once_with(
         parent=parent,
         product_set=product_set,
         product_set_id=None,
         retry=None,
         timeout=None,
         metadata=None,
     )
Пример #4
0
    def create_product(
        self,
        location: str,
        product: Union[dict, Product],
        project_id: str,
        product_id: Optional[str] = None,
        retry: Optional[Retry] = None,
        timeout: Optional[float] = None,
        metadata: Optional[Sequence[Tuple[str, str]]] = None,
    ):
        """
        For the documentation see:
        :class:`~airflow.providers.google.cloud.operators.vision.CloudVisionCreateProductOperator`
        """
        client = self.get_conn()
        parent = ProductSearchClient.location_path(project_id, location)
        self.log.info('Creating a new Product under the parent: %s', parent)
        response = client.create_product(
            parent=parent,
            product=product,
            product_id=product_id,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )
        self.log.info('Product created: %s', response.name if response else '')
        self.log.debug('Product created:\n%s', response)

        if not product_id:
            # Product id was generated by the API
            product_id = self._get_autogenerated_id(response)
            self.log.info('Extracted autogenerated Product ID from the response: %s', product_id)

        return product_id
Пример #5
0
    def create_product(self,
                       location,
                       product,
                       project_id=None,
                       product_id=None,
                       retry=None,
                       timeout=None,
                       metadata=None):
        """
        For the documentation see:
        :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductCreateOperator`
        """
        client = self.get_conn()
        parent = ProductSearchClient.location_path(project_id, location)
        self.log.info('Creating a new Product under the parent: %s', parent)
        response = client.create_product(
            parent=parent,
            product=product,
            product_id=product_id,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )
        self.log.info('Product created: %s', response.name if response else '')
        self.log.debug('Product created:\n%s', response)

        if not product_id:
            # Product id was generated by the API
            product_id = self._get_autogenerated_id(response)
            self.log.info(
                'Extracted autogenerated Product ID from the response: %s',
                product_id)

        return product_id
Пример #6
0
 def test_create_product_autogenerated_id_wrong_name_in_response(
         self, get_conn):
     # Given
     wrong_name = 'wrong_name_not_a_correct_path'
     response_product = Product(name=wrong_name)
     create_product_method = get_conn.return_value.create_product
     create_product_method.return_value = response_product
     parent = ProductSearchClient.location_path(PROJECT_ID_TEST,
                                                LOC_ID_TEST)
     product = Product()
     # When
     with self.assertRaises(AirflowException) as cm:
         self.hook.create_product(location=LOC_ID_TEST,
                                  product_id=None,
                                  product=product,
                                  project_id=PROJECT_ID_TEST)
     # Then
     # API response was wrong (wrong name format) and thus ProductSet ID extraction should fail.
     err = cm.exception
     self.assertIn('Unable to get id from name', str(err))
     create_product_method.assert_called_once_with(parent=parent,
                                                   product=product,
                                                   product_id=None,
                                                   retry=None,
                                                   timeout=None,
                                                   metadata=None)
Пример #7
0
    def create_product_set(
        self,
        location: str,
        product_set: Union[dict, ProductSet],
        project_id: str = None,
        product_set_id: str = None,
        retry: Retry = None,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = None,
    ) -> str:
        """
        For the documentation see:
        :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductSetCreateOperator`
        """
        assert project_id is not None
        client = self.get_conn()
        parent = ProductSearchClient.location_path(project_id, location)
        self.log.info('Creating a new ProductSet under the parent: %s', parent)
        response = client.create_product_set(
            parent=parent,
            product_set=product_set,
            product_set_id=product_set_id,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )
        self.log.info('ProductSet created: %s', response.name if response else '')
        self.log.debug('ProductSet created:\n%s', response)

        if not product_set_id:
            # Product set id was generated by the API
            product_set_id = self._get_autogenerated_id(response)
            self.log.info('Extracted autogenerated ProductSet ID from the response: %s', product_set_id)

        return product_set_id
Пример #8
0
    def create_product(
        self, location, product, project_id=None, product_id=None, retry=None, timeout=None, metadata=None
    ):
        """
        For the documentation see:
        :class:`~airflow.contrib.operators.gcp_vision_operator.CloudVisionProductCreateOperator`
        """
        client = self.get_conn()
        parent = ProductSearchClient.location_path(project_id, location)
        self.log.info('Creating a new Product under the parent: %s', parent)
        response = self._handle_request(
            lambda **kwargs: client.create_product(**kwargs),
            parent=parent,
            product=product,
            product_id=product_id,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )
        self.log.info('Product created: %s', response.name if response else '')
        self.log.debug('Product created:\n%s', response)

        if not product_id:
            # Product id was generated by the API
            product_id = self._get_autogenerated_id(response)
            self.log.info('Extracted autogenerated Product ID from the response: %s', product_id)

        return product_id
 def test_create_product_autogenerated_id_wrong_name_in_response(self, get_conn):
     # Given
     wrong_name = 'wrong_name_not_a_correct_path'
     response_product = Product(name=wrong_name)
     create_product_method = get_conn.return_value.create_product
     create_product_method.return_value = response_product
     parent = ProductSearchClient.location_path(PROJECT_ID_TEST, LOC_ID_TEST)
     product = Product()
     # When
     with self.assertRaises(AirflowException) as cm:
         self.hook.create_product(
             location=LOC_ID_TEST, product_id=None, product=product, project_id=PROJECT_ID_TEST
         )
     # Then
     # API response was wrong (wrong name format) and thus ProductSet ID extraction should fail.
     err = cm.exception
     self.assertIn('Unable to get id from name', str(err))
     create_product_method.assert_called_once_with(
         parent=parent, product=product, product_id=None, retry=None, timeout=None, metadata=None
     )