diff --git a/tests/test_endpoints_ads.py b/tests/test_endpoints_ads.py index 3b43756..f8d8f6a 100644 --- a/tests/test_endpoints_ads.py +++ b/tests/test_endpoints_ads.py @@ -82,6 +82,14 @@ class TwythonEndpointsTestCase(unittest.TestCase): available_platforms = self.api.get_available_platforms() self.assertTrue(len(available_platforms) >= 0) + def test_get_available_locations(self): + params = { + 'location_type': 'CITY', + 'country_code': 'US' + } + available_locations = self.api.get_available_locations(**params) + self.assertTrue(len(available_locations) > 0) + def test_get_campaigns(self): campaigns = self.api.get_campaigns(test_account_id) self.assertTrue(len(campaigns) >= 0) @@ -103,9 +111,10 @@ class TwythonEndpointsTestCase(unittest.TestCase): is_deleted = self.api.delete_campaign(test_account_id, campaign_id) self.assertTrue(is_deleted) - def test_create_line_item(self): + def test_create_and_delete_line_item(self): campaign_id = self._create_test_campaign() - self._create_test_line_item(campaign_id) + line_item_id = self._create_test_line_item(campaign_id) + self._delete_test_line_item(line_item_id) self._delete_test_campaign(campaign_id) def _create_test_line_item(self, campaign_id): @@ -116,6 +125,10 @@ class TwythonEndpointsTestCase(unittest.TestCase): self.assertIsNotNone(line_item_id) return line_item_id + def _delete_test_line_item(self, line_item_id): + is_deleted = self.api.delete_line_item(test_account_id, line_item_id) + self.assertTrue(is_deleted) + def test_upload_image(self): response = self._upload_test_image() self.assertIsNotNone(response['media_id']) @@ -193,4 +206,29 @@ class TwythonEndpointsTestCase(unittest.TestCase): self.assertTrue(result_unpromotion['deleted']) self.assertEqual(result_unpromotion['id'], promotion_id) self._delete_test_campaign(campaign_id) - self._delete_test_website_card(card_id) \ No newline at end of file + self._delete_test_website_card(card_id) + + def test_add_targeting_criteria(self): + campaign_id = self._create_test_campaign() + line_item_id = self._create_test_line_item(campaign_id) + criteria_ios_id = self._create_test_targeting_criteria(line_item_id, 'PLATFORM', '0') + criteria_android_id = self._create_test_targeting_criteria(line_item_id, 'PLATFORM', '1') + criteria_desktop_id = self._create_test_targeting_criteria(line_item_id, 'PLATFORM', '4') + criteria_new_york_id= self._create_test_targeting_criteria(line_item_id, 'LOCATION', 'b6c2e04f1673337f') + # since all the targeting criteria share the same id, we only have to do the removal once. + self.api.remove_targeting_criteria(test_account_id, criteria_ios_id) + self.api.remove_targeting_criteria(test_account_id, criteria_android_id) + self.api.remove_targeting_criteria(test_account_id, criteria_desktop_id) + self.api.remove_targeting_criteria(test_account_id, criteria_new_york_id) + self._delete_test_line_item(line_item_id) + self._delete_test_campaign(campaign_id) + + def _create_test_targeting_criteria(self, line_item_id, targeting_type, targeting_value): + test_targeting_criteria_ios = { + 'targeting_type': targeting_type, + 'targeting_value': targeting_value + } + response_add = self.api.add_targeting_criteria(test_account_id, line_item_id, **test_targeting_criteria_ios) + self.assertEqual(response_add['account_id'], test_account_id) + self.assertEquals(response_add['line_item_id'], line_item_id) + return response_add['id'] diff --git a/twython/endpoints_ads.py b/twython/endpoints_ads.py index 6a90941..13fe4da 100644 --- a/twython/endpoints_ads.py +++ b/twython/endpoints_ads.py @@ -47,7 +47,11 @@ class EndpointsAdsMixin(object): return response['data'] def get_available_platforms(self, **params): - response = self.get('targeting_criteria/platforms') + response = self.get('targeting_criteria/platforms', params=params) + return response['data'] + + def get_available_locations(self, **params): + response = self.get('targeting_criteria/locations', params=params) return response['data'] def get_campaigns(self, account_id, **params): @@ -72,6 +76,10 @@ class EndpointsAdsMixin(object): response = self.post('accounts/%s/line_items' % account_id, params=params_extended) return response['data'] + def delete_line_item(self, account_id, line_item_id): + response = self.delete('accounts/%s/line_items/%s' % (account_id, line_item_id)) + return response['data']['deleted'] + def get_website_cards(self, account_id, **params): response = self.get('accounts/%s/cards/website' % account_id, params=params) return response['data'] @@ -103,4 +111,14 @@ class EndpointsAdsMixin(object): def unpromote_tweet(self, account_id, promotion_id): response = self.delete('accounts/%s/promoted_tweets/%s' % (account_id, promotion_id)) - return response['data'] \ No newline at end of file + return response['data'] + + def add_targeting_criteria(self, account_id, line_item_id, **params): + params_extended = params.copy() + params_extended['line_item_id'] = line_item_id + response = self.post('accounts/%s/targeting_criteria' % account_id, params=params_extended) + return response['data'] + + def remove_targeting_criteria(self, account_id, criteria_id): + response = self.delete('accounts/%s/targeting_criteria/%s' % (account_id, criteria_id)) + return response['data']