def setUp(self): """Prepare unittest.""" print self.id() if not self.__class__.service: self.__class__.service = client.GetAdGroupAdService( self.__class__.SERVER, self.__class__.VERSION, HTTP_PROXY) if self.__class__.campaign_id is '0' or self.__class__.ad_group_id is '0': campaign_service = client.GetCampaignService( self.__class__.SERVER, self.__class__.VERSION, HTTP_PROXY) operations = [{ 'operator': 'ADD', 'operand': { 'name': 'Campaign #%s' % Utils.GetUniqueName(), 'status': 'PAUSED', 'biddingStrategy': { 'type': 'ManualCPC' }, 'budget': { 'period': 'DAILY', 'amount': { 'microAmount': '1000000' }, 'deliveryMethod': 'STANDARD' } } }] self.__class__.campaign_id = \ campaign_service.Mutate(operations)[0]['value'][0]['id'] ad_group_service = client.GetAdGroupService( self.__class__.SERVER, self.__class__.VERSION, HTTP_PROXY) operations = [{ 'operator': 'ADD', 'operand': { 'campaignId': self.__class__.campaign_id, 'name': 'AdGroup #%s' % Utils.GetUniqueName(), 'status': 'ENABLED', 'bids': { 'type': 'ManualCPCAdGroupBids', 'keywordMaxCpc': { 'amount': { 'microAmount': '1000000' } } } } }] self.__class__.ad_group_id = ad_group_service.Mutate( operations)[0]['value'][0]['id']
def testGetLanguageTargetSearchParameter(self): """Test whether we can request language target search parameter.""" selector = { 'searchParameters': [{ 'type': 'LanguageTargetSearchParameter', 'languageTargets': [{ 'languageCode': 'zh_CN' }, { 'languageCode': 'ja' }] }, { 'type': 'RelatedToKeywordSearchParameter', 'keywords': [{ 'text': 'global economy', 'matchType': 'EXACT' }] }], 'ideaType': 'KEYWORD', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def testAddImageAd(self): """Test whether we can add an image ad.""" operations = [{ 'operator': 'ADD', 'operand': { 'type': 'AdGroupAd', 'adGroupId': self.__class__.ad_group_id, 'ad': { 'type': 'ImageAd', 'image': { 'dimensions': [{ 'key': 'FULL', 'value': { 'width': '300', 'height': '250' } }], 'name': 'image.jpg', 'data': self.__class__.IMAGE_DATA }, 'name': 'Test image', 'url': 'http://www.example.com', 'displayUrl': 'www.example.com' } } }] self.assert_( isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.ADD', client.GetLastOperations(), True), client.GetLastUnits())
def testAddAdScheduleTarget(self): """Test whether we can add an ad schedule target to campaign.""" operations = [{ 'operator': 'SET', 'operand': { 'type': 'AdScheduleTargetList', 'campaignId': self.__class__.campaign_id, 'targets': [{ 'type': 'AdScheduleTarget', 'dayOfWeek': 'MONDAY', 'startHour': '8', 'startMinute': 'ZERO', 'endHour': '17', 'endMinute': 'ZERO', 'bidMultiplier': '1.0', }] } }] self.assert_( isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.SET', client.GetLastOperations(), True), client.GetLastUnits())
def Get(self, selector): """Return a list of bulk mutate jobs. List of bulk mutate jobs specified by a job selector. Args: selector: dict filter to run campaign criteria through. Returns: tuple list of bulk mutate jobs meeting all the criteria of the selector. """ method_name = 'getBulkMutateJob' if self.__config['soap_lib'] == SOAPPY: msg = ('The \'%s\' request via %s is currently not supported for ' 'use with SOAPpy toolkit.' % (Utils.GetCurrentFuncName(), self.__op_config['version'])) raise ApiVersionNotSupportedError(msg) elif self.__config['soap_lib'] == ZSI: web_services = self.__web_services self.__sanity_check.ValidateSelector(selector, web_services) request = eval('web_services.%sRequest()' % method_name) return self.__service.CallMethod(method_name, (({ 'selector': selector }, )), 'BulkMutateJob', self.__loc, request)
def testUpdateCampaign(self): """Test whether we can update an existing campaign.""" if self.__class__.campaign1 == None: self.testAddCampaigns() operations = [{ 'operator': 'SET', 'operand': { 'id': self.__class__.campaign1['id'], 'status': self.__class__.campaign1['status'], 'budget': { 'period': 'DAILY', 'amount': { 'microAmount': '3000000' }, 'deliveryMethod': 'STANDARD' } } }] self.assert_(isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.SET', client.GetLastOperations(), True), client.GetLastUnits())
def testAddDemographicTarget(self): """Test whether we can add an age target to campaign.""" operations = [{ 'operator': 'SET', 'operand': { 'type': 'DemographicTargetList', 'campaignId': self.__class__.campaign_id, 'targets': [{ 'type': 'AgeTarget', 'age': 'AGE_18_24' }, { 'type': 'GenderTarget', 'gender': 'FEMALE' }] } }] self.assert_( isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.SET', client.GetLastOperations(), True), client.GetLastUnits())
def testGetKeywordCategoryIdSearchParameter(self): """Test whether we can request keyword category id search parameter.""" selector = { 'searchParameters': [{ 'type': 'KeywordCategoryIdSearchParameter', 'categoryId': '5' }, { 'type': 'RelatedToKeywordSearchParameter', 'keywords': [{ 'text': 'rent video', 'matchType': 'EXACT' }] }], 'ideaType': 'KEYWORD', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def __SetDebug(self, new_state): """Temporarily change debug mode for a given Client instance. Args: new_state: bool new state of the debug mode. """ self.__config['debug'] = Utils.BoolTypeConvert(new_state)
def testGetCompetitionSearchParameter(self): """Test whether we can request competition search parameter.""" selector = { 'searchParameters': [{ 'type': 'CompetitionSearchParameter', 'levels': ['MEDIUM', 'HIGH'] }, { 'type': 'RelatedToKeywordSearchParameter', 'keywords': [{ 'text': 'cash for clunkers', 'matchType': 'BROAD' }] }], 'ideaType': 'KEYWORD', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def testGlobalMonthlySearchesSearchParameter(self): """Test whether we can request global monthly search parameter.""" selector = { 'searchParameters': [{ 'type': 'GlobalMonthlySearchesSearchParameter', 'operation': { 'minimum': '1000', 'maximum': '10000' } }, { 'type': 'RelatedToKeywordSearchParameter', 'keywords': [{ 'text': 'dvd player', 'matchType': 'EXACT' }], }], 'ideaType': 'KEYWORD', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def testGetAdTypeSearchParameter(self): """Test whether we can request ad type search parameter.""" selector = { 'searchParameters': [{ 'type': 'AdTypeSearchParameter', 'adTypes': ['DISPLAY'] }, { 'type': 'RelatedToUrlSearchParameter', 'urls': ['http://news.google.com'] }], 'ideaType': 'PLACEMENT', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def testGetBulkKeywordIdeas(self): """Test whether we can request bulk keyword ideas.""" selector = { 'searchParameters': [{ 'type': 'RelatedToKeywordSearchParameter', 'keywords': [{ 'text': 'presidential vote', 'matchType': 'EXACT' }] }, { 'type': 'RelatedToUrlSearchParameter', 'urls': ['http://finance.google.com'], 'includeSubUrls': 'false' }], 'ideaType': 'KEYWORD', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_( isinstance(self.__class__.service.GetBulkKeywordIdeas(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def testGetPlacementTypeSearchParameter(self): """Test whether we can request placement type search parameter.""" selector = { 'searchParameters': [{ 'type': 'PlacementTypeSearchParameter', 'placementTypes': ['VIDEO', 'GAME'] }, { 'type': 'RelatedToKeywordSearchParameter', 'keywords': [{ 'text': 'iron man', 'matchType': 'EXACT' }] }], 'ideaType': 'PLACEMENT', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def testCreateAdParam(self): """Test whether we can create a new ad param.""" operations = [{ 'operator': 'SET', 'operand': { 'adGroupId': self.__class__.ad_group_id, 'criterionId': self.__class__.criterion_id, 'insertionText': '$1,699', 'paramIndex': '1' } }, { 'operator': 'SET', 'operand': { 'adGroupId': self.__class__.ad_group_id, 'criterionId': self.__class__.criterion_id, 'insertionText': '139', 'paramIndex': '2' } }] self.assert_( isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.SET', client.GetLastOperations(), True), client.GetLastUnits()) self.__class__.has_param = True
def __SetUseStrict(self, new_state): """Temporarily change strictness mode for a given Client instance. Args: new_state: bool new state of the strictness mode. """ self.__config['use_strict'] = Utils.BoolTypeConvert(new_state)
def testGetGeoLocationInfo(self): """Test whether we can fetch geo location information for the given address.""" selector = { 'addresses': [{ 'streetAddress': '1600 Amphitheatre Parkway', 'cityName': 'Mountain View', 'provinceCode': 'US-CA', 'provinceName': 'California', 'postalCode': '94043', 'countryCode': 'US' }, { 'streetAddress': '76 Ninth Avenue', 'cityName': 'New York', 'provinceCode': 'US-NY', 'provinceName': 'New York', 'postalCode': '10011', 'countryCode': 'US' }, { 'streetAddress': '五四大街1号, Beijing东城区', 'countryCode': 'CN' }] } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def __SetXmlParser(self, new_state): """Temporarily change xml parser in use for a given Client instance. Args: new_state: bool new state of the xml parser to use. """ self.__config['xml_parser'] = Utils.BoolTypeConvert(new_state)
def GetBulkKeywordIdeas(self, selector): """Return a list of pages with ideas. List of pages specified by a targeting idea selector. This method is specialized for returning bulk keyword ideas. Args: selector: dict filter to run targeting ideas through. Returns: tuple list of pages with targeting ideas of the selector. """ method_name = 'getBulkKeywordIdeas' if self.__config['soap_lib'] == SOAPPY: msg = ('The \'%s\' request via %s is currently not supported for ' 'use with SOAPpy toolkit.' % (Utils.GetCurrentFuncName(), self.__op_config['version'])) raise ApiVersionNotSupportedError(msg) elif self.__config['soap_lib'] == ZSI: web_services = self.__web_services self.__sanity_check.ValidateSelector(selector, web_services) request = eval('web_services.%sRequest()' % method_name) return self.__service.CallMethod(method_name, (({ 'selector': selector }, )), 'TargetingIdea', self.__loc, request)
def GetCampaignCriterionService(self, server='https://adwords.google.com', version=None, http_proxy=None): """Call API method in CampaignCriterionService. Args: [optional] server: str API server to access for this API call. Possible values are: 'https://adwords.google.com' for live site and 'https://adwords-sandbox.google.com' for sandbox. The default behavior is to access live site. version: str API version to use. http_proxy: str HTTP proxy to use. Returns: CampaignCriterionService new instance of CampaignCriterionService object. """ headers = self.__GetAuthCredentialsForAccessLevel() if version is None: version = MIN_API_VERSION if Utils.BoolTypeConvert(self.__config['use_strict']): SanityCheck.ValidateServer(server, version) # Load additional configuration data. op_config = { 'server': server, 'version': version, 'group': 'cm', 'http_proxy': http_proxy } return CampaignCriterionService(headers, self.__config, op_config, self.__lock, self.__logger)
def testAddNetworkLanguageTargets(self): """Test whether we can add a network and language targets to campaign.""" operations = [{ 'operator': 'SET', 'operand': { 'type': 'LanguageTargetList', 'campaignId': self.__class__.campaign_id, 'targets': [{ 'type': 'LanguageTarget', 'languageCode': 'en' }] } }, { 'operator': 'SET', 'operand': { 'type': 'NetworkTargetList', 'campaignId': self.__class__.campaign_id, 'targets': [{ 'type': 'NetworkTarget', 'networkCoverageType': 'GOOGLE_SEARCH' }, { 'type': 'NetworkTarget', 'networkCoverageType': 'CONTENT_NETWORK' }] } }] self.assert_( isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.SET', client.GetLastOperations(), True), client.GetLastUnits())
def testAddKeywordCrossAdGroup(self): """Test whether we can add cross ad group keywords.""" operations = [{ 'operator': 'ADD', 'operand': { 'type': 'BiddableAdGroupCriterion', 'adGroupId': self.__class__.kw_ad_group_id, 'criterion': { 'type': 'Keyword', 'matchType': 'BROAD', 'text': 'mars cruise' } } }, { 'operator': 'ADD', 'operand': { 'type': 'BiddableAdGroupCriterion', 'adGroupId': self.__class__.place_ad_group_id, 'criterion': { 'type': 'Placement', 'url': 'www.example.com' } } }] self.assert_(isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.ADD', client.GetLastOperations(), True), client.GetLastUnits())
def testAddAdExtensionOverride(self): """Test whether we can add ad extension override to a given campaign.""" operations = [{ 'operator': 'ADD', 'operand': { 'type': 'CampaignAdExtension', 'campaignId': self.__class__.campaign_id, 'adExtension': { 'type': 'LocationExtension', 'address': self.__class__.geo_location['address'], 'geoPoint': self.__class__.geo_location['geoPoint'], 'encodedLocation': self.__class__.geo_location['encodedLocation'], 'source': 'ADWORDS_FRONTEND' } } }] self.assert_(isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.ADD', client.GetLastOperations(), True), client.GetLastUnits())
def testUpdateCriterionKeywordBids(self): """Test whether we can update a keyword's bids.""" if self.__class__.kw == None: self.testAddCriterionKeyword() operations = [{ 'operator': 'SET', 'operand': { 'type': 'BiddableAdGroupCriterion', 'adGroupId': self.__class__.kw['adGroupId'], 'criterion': { 'id': self.__class__.kw['criterion']['id'], }, 'bids': { 'type': 'ManualCPCAdGroupCriterionBids', 'maxCpc': { 'amount': { 'microAmount': '500000' } } } } }] self.assert_(isinstance(self.__class__.service.Mutate(operations), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.SET', client.GetLastOperations(), True), client.GetLastUnits())
def testEstimateCampaignList(self): """Test wheter we can estimate campaign list.""" requests = [{ 'adGroupRequests': [{ 'keywordRequests': [{ 'maxCpc': '1000000', 'negative': 'False', 'text': 'Flowers', 'type': 'Broad' }], 'maxCpc': '1000000' }], 'geoTargeting': { 'cityTargets': { 'cities': ['New York, NY US'], } }, 'languageTargeting': ['en'], 'networkTargeting': ['GoogleSearch', 'SearchNetwork'] }] self.assert_( isinstance(self.__class__.service.EstimateCampaignList(requests), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'estimateCampaignList', client.GetLastOperations(), True), client.GetLastUnits())
def Get(self, selector): """Return the API usage information. Usage information is based on the selection criteria of the selector. Args: selector: dict filter to run API usage through. Returns: tuple API usage information. """ method_name = 'getInfo' if self.__config['soap_lib'] == SOAPPY: msg = ('The \'%s\' request via %s is currently not supported for ' 'use with SOAPpy toolkit.' % (Utils.GetCurrentFuncName(), self.__op_config['version'])) raise ApiVersionNotSupportedError(msg) elif self.__config['soap_lib'] == ZSI: web_services = self.__web_services self.__sanity_check.ValidateSelector(selector, web_services) request = eval('web_services.%sRequest()' % method_name) return self.__service.CallMethod(method_name, (({ 'selector': selector }, )), 'Info', self.__loc, request)
def testAddTextAd(self): """Test whether we can add a text ad.""" operations = [{ 'operator': 'ADD', 'operand': { 'type': 'AdGroupAd', 'adGroupId': self.__class__.ad_group_id, 'ad': { 'type': 'TextAd', 'url': 'http://www.example.com', 'displayUrl': 'example.com', 'status': 'ENABLED', 'description1': 'Visit the Red Planet in style.', 'description2': 'Low-gravity fun for everyone!', 'headline': 'Luxury Cruise to Mars' } } }] ads = self.__class__.service.Mutate(operations) self.__class__.ad = ads[0]['value'][0] self.assert_(isinstance(ads, tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'mutate.ADD', client.GetLastOperations(), True), client.GetLastUnits())
def setUp(self): """Prepare unittest.""" print self.id() if not self.__class__.service: self.__class__.service = client.GetAdGroupService( self.__class__.SERVER, self.__class__.VERSION, HTTP_PROXY) if (self.__class__.cpc_campaign_id is '0'): campaign_service = client.GetCampaignService( self.__class__.SERVER, self.__class__.VERSION, HTTP_PROXY) operations = [{ 'operator': 'ADD', 'operand': { 'name': 'Campaign #%s' % Utils.GetUniqueName(), 'status': 'PAUSED', 'biddingStrategy': { 'type': 'ManualCPC' }, 'endDate': '20110101', 'budget': { 'period': 'DAILY', 'amount': { 'microAmount': '2000000' }, 'deliveryMethod': 'STANDARD' } } }] self.__class__.cpc_campaign_id = \ campaign_service.Mutate(operations)[0]['value'][0]['id']
def testGetIdeaTextMatchesSearchParameter(self): """Test whether we can request idea text matches.""" selector = { 'searchParameters': [{ 'type': 'IdeaTextMatchesSearchParameter', 'included': ['red flowers'] }, { 'type': 'RelatedToKeywordSearchParameter', 'keywords': [{ 'text': 'flowers', 'matchType': 'BROAD' }] }], 'ideaType': 'KEYWORD', 'requestType': 'IDEAS', 'paging': { 'startIndex': '0', 'numberResults': '1' } } self.assert_(isinstance(self.__class__.service.Get(selector), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'get', client.GetLastOperations(), True), client.GetLastUnits())
def testGetMccAlerts(self): """Test whether we can retrieve MCC alerts.""" self.assert_(isinstance(self.__class__.service.GetMccAlerts(), tuple)) self.assertEqual( Utils.GetMethodCost(self.__class__.VERSION, self.__class__.service.__class__.__name__, 'getMccAlerts', client.GetLastOperations(), True), client.GetLastUnits())