def setUp(self):
    """Setup function for each unit test."""

    super(GoogleAnalyticsHookTest, self).setUp()

    self.test_tracking_id = 'UA-12323-4'
    self.payload_builder = ga_hook.PayloadBuilder(self.test_tracking_id)

    self.event_test_data = {
        'ec': 'ClientID',
        'ea': 'test_event_action',
        'el': '20190423',
        'ev': 1,
        'cid': '12345.456789'
    }
    self.small_event = {
        'cid': '12345.67890',
        'ec': 'ClientID',
        'ea': 'test_event_action',
        'el': '20190423',
        'ev': 1,
        'z': '1558517072202080'
    }
    # Both of the below are approx 4K of data
    self.medium_event = {**self.small_event, 'ea': 'x' * 3800}
    self.utf8_event = {**self.small_event, 'ea': b'\xf0\xa9\xb8\xbd' * 320}

    self.test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                                 self.event_test_data)

    self.test_hook._send_http_request = mock.MagicMock(autospec=True)
    self.test_hook._send_http_request.return_value = mock.Mock(ok=True)
    self.test_hook._send_http_request.return_value.status = 200
Esempio n. 2
0
    def __init__(self,
                 *args,
                 gcs_bucket: Text,
                 gcs_prefix: Text,
                 gcs_content_type: Text,
                 ga_tracking_id: Text,
                 ga_base_params: Optional[Mapping[Text, Any]] = None,
                 ga_dry_run: Optional[bool] = False,
                 **kwargs) -> None:
        """Initializes the DataConnectorOperator with an input and output hooks.

    Args:
      *args: arguments for the operator.
      gcs_bucket: Unique name of the bucket holding the target blob.
      gcs_prefix: The path to a location within the bucket.
      gcs_content_type: Blob's content type. Either 'JSON' or 'CSV'.
      ga_tracking_id: Google Analytics' tracking id to identify a property.
      ga_base_params: Default parameters that serve as the base on which to
        build the Measurement Protocol payload.
      ga_dry_run: If True, this will not send real hits to the endpoint.
      **kwargs: Other arguments to pass through to the operator or hooks.
    """
        self.gcs_hook = gcs_hook.GoogleCloudStorageHook(
            bucket=gcs_bucket,
            prefix=gcs_prefix,
            content_type=gcs_content_type)
        self.ga_hook = ga_hook.GoogleAnalyticsHook(tracking_id=ga_tracking_id,
                                                   base_params=ga_base_params,
                                                   dry_run=ga_dry_run)
        super(GoogleCloudStorageToGoogleAnalyticsOperator,
              self).__init__(self.gcs_hook, self.ga_hook, *args, **kwargs)
Esempio n. 3
0
  def __init__(self, *args,
               bq_conn_id: Text,
               bq_dataset_id: Text,
               bq_table_id: Text,
               bq_selected_fields: Optional[Text] = None,
               ga_tracking_id: Text,
               ga_base_params: Optional[Mapping[Text, Any]] = None,
               ga_dry_run: Optional[bool] = False,
               **kwargs) -> None:
    """Initializes the DataConnectorOperator with an input and output hooks.

    Args:
      *args: arguments for the operator.
      bq_conn_id: Connection id passed to airflow's BigQueryHook.
      bq_dataset_id: Dataset id of the target table.
      bq_table_id: Table name of the target table.
      bq_selected_fields: Subset of fields to return (e.g. 'field_1,field_2').
      ga_tracking_id: Google Analytics' tracking id to identify a property.
      ga_base_params: Default parameters that serve as the base on which to
        build the Measurement Protocol payload.
      ga_dry_run: If True, this will not send real hits to the endpoint.
      **kwargs: Other arguments to pass through to the operator or hooks.
    """
    self.bq_hook = bq_hook.BigQueryHook(conn_id=bq_conn_id,
                                        dataset_id=bq_dataset_id,
                                        table_id=bq_table_id,
                                        selected_fields=bq_selected_fields)
    self.ga_hook = ga_hook.GoogleAnalyticsHook(tracking_id=ga_tracking_id,
                                               base_params=ga_base_params,
                                               dry_run=ga_dry_run)
    super(BigQueryToGoogleAnalyticsOperator, self).__init__(
        self.bq_hook, self.ga_hook, *args, **kwargs)
  def test_ga_hook_send_batch_hit_with_return_error_status_code(self, r_code):
    """Test GoogleAnalyticsHook sends batch hit with error response."""
    test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                            self.event_test_data,
                                            False)
    test_payload = self.payload_builder.generate_batch_payload(
        'event', [self.event_test_data])

    with mock.patch('urllib.request.urlopen') as urlopen_mock:
      mock_response = urlopen_mock.return_value.__enter__.return_value
      mock_response.status = r_code

      with self.assertRaises(errors.DataOutConnectorSendUnsuccessfulError):
        test_hook.send_hit(test_payload, send_type='batch')
  def test_ga_hook_send_single_hit(self):
    """Test GoogleAnalyticsHook sends single hit."""
    test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                            self.event_test_data,
                                            False)
    test_payload = self.payload_builder.generate_single_payload(
        'event', self.event_test_data)

    with mock.patch('urllib.request.urlopen') as urlopen_mock:
      mock_response = urlopen_mock.return_value.__enter__.return_value
      mock_response.status = 200
      test_hook.send_hit(test_payload)

      urlopen_mock.assert_called_once()
  def test_ga_hook_send_batch_hit_with_retrys_on_retriable_error(self):
    """Test GoogleAnalyticsHook retries when retriable error occures."""
    test_hook = ga_hook.GoogleAnalyticsHook(self.test_tracking_id,
                                            self.event_test_data,
                                            False)
    test_payload = self.payload_builder.generate_batch_payload(
        'event', [self.event_test_data])

    with mock.patch('urllib.request.urlopen') as urlopen_mock:
      urlopen_mock.return_value.__enter__.return_value.status = 429

      try:
        test_hook.send_hit(test_payload)
      except errors.DataOutConnectorSendUnsuccessfulError:
        pass

      self.assertEqual(urlopen_mock.call_count,
                       retry_utils._RETRY_UTILS_MAX_RETRIES)
 def test_ga_hook_get_invalid_tracking_id(self):
   """Test GoogleAnalyticsHook with invalid tracking id."""
   with self.assertRaises(errors.DataOutConnectorValueError):
     ga_hook.GoogleAnalyticsHook('UA-123-b', self.event_test_data)