Beispiel #1
0
    def test_add_valid_custom_period_to_report_definition(self):
        """Test that report definition with custom dateRangeType is correctly implemented
        when start_date and end_date are provided
        """

        valid_parameter = {
            "date_range_type": "CUSTOM_DATE",
            "start_date": datetime.date(2019, 1, 1),
            "end_date": datetime.date(2021, 2, 1),
        }

        report_definition = self.get_report_definition(
            valid_parameter["date_range_type"])
        expected_date_range = {
            "min": valid_parameter["start_date"].strftime(DATEFORMAT),
            "max": valid_parameter["end_date"].strftime(DATEFORMAT),
        }

        temp_kwargs = self.kwargs.copy()
        temp_kwargs.update(valid_parameter)
        GoogleAdsReader(
            **temp_kwargs).add_period_to_report_definition(report_definition)

        assert report_definition["dateRangeType"] == valid_parameter[
            "date_range_type"]
        assert report_definition["selector"][
            "dateRange"] == expected_date_range
Beispiel #2
0
 def test_refuse_incorrect_id(self, invalid_input):
     """Test the function checking the Client Customer IDs
     format in order to refuse an invalid input
     """
     expected = None
     assert GoogleAdsReader(
         **self.kwargs).valid_client_customer_id(invalid_input) == expected
Beispiel #3
0
 def test_validate_correct_id(self, valid_input):
     """Test the function checking the Client Customer IDs
     format in order to refuse an invalid input
     """
     cond = GoogleAdsReader(
         **self.kwargs).valid_client_customer_id(valid_input)
     assert cond
Beispiel #4
0
 def test_list_video_campaign_ids(self, mock_report, mock_query):
     temp_kwargs = self.kwargs.copy()
     temp_kwargs.update({"filter_on_video_campaigns": True})
     expected = set(["1234567890", "1111111111"])
     set_ids = GoogleAdsReader(**temp_kwargs).list_video_campaign_ids()
     assert len(set_ids) != 0
     assert set_ids == expected
Beispiel #5
0
 def test_invalid_report_filter(self):
     not_a_dict = ["field", "operator", "values"]
     report_definition = {"selector": {}}
     with self.assertRaises(AttributeError):
         temp_kwargs = self.kwargs.copy()
         temp_kwargs.update({"report_filter": not_a_dict})
         GoogleAdsReader(**temp_kwargs).add_report_filter(report_definition)
Beispiel #6
0
 def test_no_campaignid_for_video_report_definition(self):
     no_campaignid_field = {
         "fields": ("CampaignName", "Date", "Impressions")
     }
     temp_kwargs = self.kwargs.copy()
     temp_kwargs.update(no_campaignid_field)
     with self.assertRaises(ClickException):
         GoogleAdsReader(
             **temp_kwargs).get_video_campaign_report_definition()
Beispiel #7
0
    def test_read_data(self, mock_report, mock_query):
        reader = GoogleAdsReader(**self.kwargs)
        expected = [
            {
                "AdGroupName": "ad_group_example",
                "Date": "2019-01-01",
                "Impressions": "0"
            },
            {
                "AdGroupName": "ad_group_example",
                "Date": "2019-01-01",
                "Impressions": "4"
            },
        ]

        for data in reader.read():
            assert len(list(data.readlines())) != 0
            for record, output in zip(data.readlines(), iter(expected)):
                assert record == output
Beispiel #8
0
 def test_missing_field_report_filter(self):
     missing_field = {
         "wrong_key": "CampaignName",
         "operator": "IN",
         "values": ["example"]
     }
     report_definition = {"selector": {}}
     with self.assertRaises(ClickException):
         temp_kwargs = self.kwargs.copy()
         temp_kwargs.update({"report_filter": missing_field})
         GoogleAdsReader(**temp_kwargs).add_report_filter(report_definition)
Beispiel #9
0
 def test_add_report_filter(self):
     report_filter = {
         "field": "CampaignName",
         "operator": "IN",
         "values": ["example"]
     }
     report_definition = {"selector": {}}
     expected_output = {"selector": {"predicates": report_filter}}
     temp_kwargs = self.kwargs.copy()
     temp_kwargs.update({"report_filter": report_filter})
     GoogleAdsReader(**temp_kwargs).add_report_filter(report_definition)
     assert report_definition == expected_output
Beispiel #10
0
 def test_add_invalid_custom_period_to_report_definition(
         self, invalid_parameter, exception):
     """Test that report definition dateRangeType is replaced by default value
     when no start_date and end_date are provided
     """
     with self.assertRaises(exception):
         report_definition = self.get_report_definition(
             invalid_parameter["date_range_type"])
         temp_kwargs = self.kwargs.copy()
         temp_kwargs.update(invalid_parameter)
         GoogleAdsReader(**temp_kwargs).add_period_to_report_definition(
             report_definition)
Beispiel #11
0
 def test_add_period_to_report_definition_works_properly(self):
     consistent_report_definition = {
         "date_range_type": "YESTERDAY",
         "start_date": None,
         "end_date": None
     }
     report_definition = self.get_report_definition(
         consistent_report_definition["date_range_type"])
     temp_kwargs = self.kwargs.copy()
     temp_kwargs.update(consistent_report_definition)
     self.assertIsNone(
         GoogleAdsReader(**temp_kwargs).add_period_to_report_definition(
             report_definition))
Beispiel #12
0
    def test_read_data_and_include_account_id(self, mock_report, mock_query):
        temp_kwargs = self.kwargs.copy()
        temp_kwargs.update({"include_client_customer_id": True})
        reader = GoogleAdsReader(**temp_kwargs)

        expected = [
            {
                "AdGroupName": "ad_group_example",
                "AccountId": "123-456-7890",
                "Date": "2019-01-01",
                "Impressions": "0"
            },
            {
                "AdGroupName": "ad_group_example",
                "AccountId": "123-456-7890",
                "Date": "2019-01-01",
                "Impressions": "4"
            },
        ]

        for data in reader.read():
            assert len(list(data.readlines())) != 0
            for record, output in zip(data.readlines(), iter(expected)):
                assert record == output
Beispiel #13
0
    def test_add_standard_period_to_report_definition(self, name,
                                                      valid_parameter):
        """Test that report definition with classic dateRangeType is correctly implemented
        whether or not the user specify a date range (not taken into account)
        """
        report_definition = self.get_report_definition(
            valid_parameter["date_range_type"])

        temp_kwargs = self.kwargs.copy()
        temp_kwargs.update(valid_parameter)
        GoogleAdsReader(
            **temp_kwargs).add_period_to_report_definition(report_definition)

        assert report_definition["dateRangeType"] == valid_parameter[
            "date_range_type"]
        with self.assertRaises(KeyError):
            report_definition["start_date"]
        with self.assertRaises(KeyError):
            report_definition["end_date"]
Beispiel #14
0
 def test_get_video_report_definition_custom_date(self):
     custom_date_param = {
         "date_range_type": "CUSTOM_DATE",
         "start_date": datetime.date(2019, 1, 1),
         "end_date": datetime.date(2019, 3, 1),
     }
     temp_kwargs = self.kwargs.copy()
     temp_kwargs.update(custom_date_param)
     video_report_def = GoogleAdsReader(
         **temp_kwargs).get_video_campaign_report_definition()
     expected_output_custom_date = {
         "reportName": "video campaigns ids",
         "dateRangeType": "CUSTOM_DATE",
         "reportType": "VIDEO_PERFORMANCE_REPORT",
         "downloadFormat": "CSV",
         "selector": {
             "fields": "CampaignId",
             "dateRange": {
                 "min": "20190101",
                 "max": "20190301"
             }
         },
     }
     assert video_report_def == expected_output_custom_date
Beispiel #15
0
 def test_format_customer_id(self):
     assert GoogleAdsReader.format_customer_id(1234567890) == "123-456-7890"
     assert GoogleAdsReader.format_customer_id("abcdefjhij") is None
     assert GoogleAdsReader.format_customer_id(-1234567890) is None
     assert GoogleAdsReader.format_customer_id(None) is None
     assert GoogleAdsReader.format_customer_id(123456789) is None
Beispiel #16
0
def google_ads(**kwargs):
    return GoogleAdsReader(**extract_args("googleads_", kwargs))