def testIteratingBeforeDone(self): """Tests that iterating before calling Done raises a LoggerException.""" self.assertRaises(log.LoggerException, self.Iterate) bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id113' self.logger.LogSynchronousRequest(bid_request, 123, 'Payload') self.assertRaises(log.LoggerException, self.Iterate)
def CreateSuccessfulRecord(self): """Returns a Record with a successful request/response pair. Returns: A (BidResopnse, Record) pair. The bid_response is returned for convenience so a test method can make a slight modification and serialize it into record.bid_response without parsing record.bid_response first. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id111' request_adslot = bid_request.adslot.add() request_adslot.id = 123 request_adslot.height.append(468) request_adslot.width.append(60) status = 200 bid_response = realtime_bidding_pb2.BidResponse() bid_response.processing_time_ms = 10 ad = bid_response.ad.add() ad.click_through_url.append('http://url.com') ad.html_snippet = ('A snippet. <a href=\'%%CLICK_URL_UNESC%%' 'http://www.google.com\'>link</a>') adslot = ad.adslot.add() adslot.id = 123 adslot.max_cpm_micros = 5000000 record = log.Record(bid_request, status, bid_response.SerializeToString()) return bid_response, record
def testLoggingAfterDone(self): """Tests that logging after calling Done raises a LoggerException.""" bid_request = realtime_bidding_pb2.BidRequest() self.logger.Done() self.assertFalse(self.logger.LogSynchronousRequest(bid_request, 200, 'payload'))
def testLogSynchronousRequest(self): """Tests logging a request/response synchronously.""" self.assertEqual(0, len(self.logger._records)) bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id111' self.logger.LogSynchronousRequest(bid_request, 200, 'Hello') self.assertEqual(1, len(self.logger._records)) self.assertEqual(bid_request, self.logger._records[0].bid_request) self.assertEqual(200, self.logger._records[0].status) self.assertEqual('Hello', self.logger._records[0].payload) self.assertFalse(self.logger._done)
def __init__(self, request_to_return=None): """Initializes a mock generator. Args: request_to_return: The request to return on each invocation, or None to use the default. """ self.request = request_to_return if not self.request: self.request = realtime_bidding_pb2.BidRequest() self.request.id = '1234'
def testSummarizePingRequest(self): """Tests summarizing a ping request.""" bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id111' bid_request.is_ping = True bid_response = realtime_bidding_pb2.BidResponse() bid_response.processing_time_ms = 0 record = log.Record(bid_request, 200, bid_response.SerializeToString()) self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNGoodRequests(1)
def testSummarizePingRequestHasAds(self): """Tests summarizing a ping request for which ads are returned.""" bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id111' bid_request.is_ping = True _, record = self.CreateSuccessfulRecord() record.bid_request = bid_request self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1)
def GeneratePingRequest(self): """Generates a special ping request. A ping request only has the id and is_ping fields set. Returns: An instance of realtime_bidding_pb2.BidRequest. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = self._GenerateId(BID_REQUEST_ID_LENGTH) bid_request.is_ping = True return bid_request
def GenerateBidRequest(self): """Generates a random BidRequest. Randomly picks device info from available set, sets user agent and screen sizes before calling parent generate bid request. Returns: An instance of realtime_bidding_pb2.BidRequest. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.is_test = True bid_request.id = self._GenerateId(BID_REQUEST_ID_LENGTH) self._GeneratePageInfo(bid_request) # Pick a mobile device at random. (platform, os_major_version, os_minor_version, os_micro_version, device_type, is_app_request, is_interstitial, orientation, self._slot_width, self._slot_height, bid_request.user_agent) = random.choice(MOBILE_DEVICE_INFO) # Add mobile fields mobile = bid_request.mobile mobile.carrier_id = random.choice(MOBILE_CARRIERS) mobile.platform = platform mobile.os_version.os_version_major = os_major_version mobile.os_version.os_version_minor = os_minor_version mobile.os_version.os_version_micro = os_micro_version mobile.mobile_device_type = device_type mobile.is_app = is_app_request mobile.is_interstitial_request = is_interstitial mobile.screen_orientation = orientation if is_app_request: category_ids = None if platform == 'android': category_ids = self._GenerateSet( MOBILE_ANDROID_CATEGORY_IDS, random.randint(1, NUM_CATEGORIES)) mobile.app_id = random.choice(ANDROID_APP_IDS) else: category_ids = self._GenerateSet( MOBILE_IOS_CATEGORY_IDS, random.randint(1, NUM_CATEGORIES)) mobile.app_id = random.choice(IOS_APP_IDS) for category_id in category_ids: mobile.app_category_ids.append(category_id) self._GenerateUserInfo(bid_request) self._GenerateAdSlot(bid_request) self.add_HyperlocalSet(bid_request) self.add_idfa(bid_request) self.add_UserDemographic(bid_request) return bid_request
def testIterationAfterDoneWithLogRecords(self): """Test iterating over a locked logger with records.""" for i in range(10): bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id11%d' % i self.logger.LogSynchronousRequest(bid_request, 123, 'Payload') self.logger.Done() records = [record for record in self.logger] self.assertEqual(10, len(records)) for record in records: self.assertTrue(record.bid_request.HasField('id')) self.assertEqual(123, record.status) self.assertEqual('Payload', record.payload)
def GeneratePingRequest(self): """Generates a special ping request. A ping request only has the deprecated_protocol_version, id and is_ping fields set. Returns: An instance of realtime_bidding_pb2.BidRequest. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.DEPRECATED_protocol_version = PROTOCOL_VERSION bid_request.id = self._GenerateId(BID_REQUEST_ID_LENGTH) bid_request.is_ping = True return bid_request
def testGenerateAdGroupIdsFromList(self): """Tests generating AdGroup IDs from a list.""" adgroup_ids = [5642578842, 5663180187, 5663180325] self.generator = generator.DefaultBidGenerator( adgroup_ids_list=adgroup_ids) bid_request = realtime_bidding_pb2.BidRequest() self.generator._GenerateAdSlot(bid_request) self.assertEqual(1, len(bid_request.adslot)) adslot = bid_request.adslot[0] self.assertTrue(adslot.matching_ad_data) for matching_ad_data in adslot.matching_ad_data: self.assertTrue( matching_ad_data.adgroup_id in adgroup_ids, '%s not in %s' % (matching_ad_data.adgroup_id, adgroup_ids))
def GenerateBidRequest(self): """Generates a random BidRequest. Returns: An instance of realtime_bidding_pb2.BidRequest. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.is_test = True bid_request.id = self._GenerateId(BID_REQUEST_ID_LENGTH) bid_request.user_agent = random.choice(USER_AGENTS) self._GeneratePageInfo(bid_request) self._GenerateUserInfo(bid_request) self._GenerateAdSlot(bid_request) return bid_request
def testGenerateGoogleIdFromList(self): """Tests generating a Google ID from a list.""" # Real sample values from the Cookie Matching service. google_ids = [ 'CAESEBs2zXrUdzP08NfyRcwuOic', 'CAESEGDp9dG8BR26gKybOFwKVnQ', 'CAESELGsXo_3kPoHM2EdtzhqvSs', 'CAESEPOq9BSD5GWBmb716V8R-uo', 'CAESEJaOYa0vEUojtGMTlkf9UoM', 'CAESECwK0aD3FY0QIaU6cA1ln2A', ] self.generator = generator.RandomBidGenerator(google_ids) bid_request = realtime_bidding_pb2.BidRequest() self.generator._GenerateGoogleID(bid_request) self.assertTrue(bid_request.HasField('google_user_id')) self.assertTrue(bid_request.google_user_id in google_ids)
def GenerateBidRequest(self): """Generates a random BidRequest. Returns: An instance of realtime_bidding_pb2.BidRequest. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.DEPRECATED_protocol_version = PROTOCOL_VERSION #bid_request.is_test = True #bid_request.experimental_deadline = True #bid_request.is_ping = True bid_request.id = self._GenerateId(BID_REQUEST_ID_LENGTH) self._GeneratePageInfo(bid_request) self._GenerateUserInfo(bid_request) self._GenerateAdSlot(bid_request) return bid_request
def CreateSuccessfulTemplateRecord(self): """Returns a Record with a successful request/response for a template ad. Returns: A (BidResopnse, Record) pair. The bid_response is returned for convenience so a test method can make a slight modification and serialize it into record.bid_response without parsing record.bid_response first. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id111' request_adslot = bid_request.adslot.add() request_adslot.id = 123 request_adslot.height.append(468) request_adslot.width.append(60) status = 200 bid_response = realtime_bidding_pb2.BidResponse() bid_response.processing_time_ms = 10 ad = bid_response.ad.add() ad.snippet_template = 'Template %%P0%%, %%P1%%.' param = ad.template_parameter.add() param.parameter_value = 'value0' param.left = 1 param.right = 15 param.top = 20 param.bottom = 1 param.click_through_url = 'http://url1.com' param.buyer_creative_id = '12345' param = ad.template_parameter.add() param.parameter_value = 'value1' param.left = 1 param.right = 15 param.top = 35 param.bottom = 21 param.click_through_url = 'http://url2.com' param.buyer_creative_id = '54321' adslot = ad.adslot.add() adslot.id = 123 adslot.max_cpm_micros = 5000000 record = log.Record(bid_request, status, bid_response.SerializeToString()) return bid_response, record
def CreateSuccessfulVideoRecord(self): """Returns a Record with a successful video request/response pair. Returns: A (BidResopnse, Record) pair. The bid_response is returned for convenience so a test method can make a slight modification and serialize it into record.bid_response without parsing record.bid_response first. """ bid_request = realtime_bidding_pb2.BidRequest() bid_request.id = 'id111' bid_request.video.videoad_start_delay = 1000 request_adslot = bid_request.adslot.add() request_adslot.id = 123 status = 200 bid_response = realtime_bidding_pb2.BidResponse() bid_response.processing_time_ms = 10 ad = bid_response.ad.add() ad.click_through_url.append('http://url.com') ad.video_url = 'http://my.video.url.com' adslot = ad.adslot.add() adslot.id = 123 adslot.max_cpm_micros = 5000000 record = log.Record(bid_request, status, bid_response.SerializeToString()) return bid_response, record
def deserialize_bid_request(self, serialized_protocol_buffer): bid_request = realtime_bidding_pb2.BidRequest() bid_request.ParseFromString(serialized_protocol_buffer) return (bid_request)
def testGenerateRandomGoogleId(self): """Tests generating a random Google ID.""" self.generator = generator.RandomBidGenerator() bid_request = realtime_bidding_pb2.BidRequest() self.generator._GenerateGoogleID(bid_request) self.assertTrue(bid_request.HasField('google_user_id'))