def testSummarizeTemplateBadStacking(self): """Tests summarizing a template ad that doesn't stack correctly.""" # A variety of invalid bounds, each item in the list is a 2 item list, one # for each ad. bad_bounds_list = [ # Overlapping vertically. [{'left': 1, 'right': 15, 'top': 15, 'bottom': 0}, {'left': 1, 'right': 15, 'top': 30, 'bottom': 14}], # Overlapping horizontally. [{'left': 1, 'right': 15, 'top': 15, 'bottom': 0}, {'left': 14, 'right': 25, 'top': 15, 'bottom': 0}], # Overlapping both vertically and horizontally. [{'left': 1, 'right': 15, 'top': 15, 'bottom': 0}, {'left': 14, 'right': 25, 'top': 25, 'bottom': 10}], ] for bounds in bad_bounds_list: bid_response, record = self.CreateSuccessfulTemplateRecord() for i, ad_bounds in enumerate(bounds): bid_response.ad[0].template_parameter[i].left = ad_bounds['left'] bid_response.ad[0].template_parameter[i].right = ad_bounds['right'] bid_response.ad[0].template_parameter[i].top = ad_bounds['top'] bid_response.ad[0].template_parameter[i].bottom = ad_bounds['bottom'] record.payload = bid_response.SerializeToString() self.records = [record] self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) # For some cases we have a stacking error. self.assertEqual(1, len(record.problems))
def PrintSummary(logger): """ @note: 打印统计的信息 @param: 日志统计的对象 """ logger.Done() summarizer = log.LogSummarizer(logger) summarizer.Summarize() timestamp = str(datetime.datetime.now()) timestamp = timestamp.replace(' ', '-', timestamp.count(' ')) timestamp = timestamp.replace(':', '', timestamp.count(':')) good_log_filename = GOOD_LOG_TEMPLATE % timestamp good_log = open(good_log_filename, 'w') problematic_log_filename = PROBLEMATIC_LOG_TEMPLATE % timestamp problematic_log = open(problematic_log_filename, 'w') invalid_log_filename = INVALID_LOG_TEMPLATE % timestamp invalid_log = open(invalid_log_filename, 'w') error_log_filename = ERROR_LOG_TEMPLATE % (timestamp) error_log = open(error_log_filename, 'w') summarizer.WriteLogFiles(good_log, problematic_log, invalid_log, error_log) good_log.close() problematic_log.close() invalid_log.close() error_log.close() summarizer.PrintReport() # Cleanup by deleting empty files. for file_name in [ good_log_filename, problematic_log_filename, invalid_log_filename, error_log_filename ]: if not os.path.getsize(file_name): os.remove(file_name)
def testSummarizeTemplateBadBoundsBounds(self): """Tests summarizing a template ad with a missing left bound.""" # A variety of invalid bounds. bad_bounds_list = [ # Left < 0. {'left': -1, 'right': 15, 'top': 15, 'bottom': 0}, # Right > slot width. {'left': 1, 'right': 100, 'top': 15, 'bottom': 0}, # Bottom < 0. {'left': 1, 'right': 15, 'top': 15, 'bottom': -1}, # Top > height.. {'left': 1, 'right': 15, 'top': 600, 'bottom': 0}, # Width < 10. {'left': 1, 'right': 5, 'top': 15, 'bottom': 1}, {'left': 1, 'right': 10, 'top': 15, 'bottom': 1}, # Height < 10 {'left': 1, 'right': 15, 'top': 5, 'bottom': 1}, {'left': 1, 'right': 15, 'top': 10, 'bottom': 1}, ] for bounds in bad_bounds_list: bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].template_parameter[0].left = bounds['left'] bid_response.ad[0].template_parameter[0].right = bounds['right'] bid_response.ad[0].template_parameter[0].top = bounds['top'] bid_response.ad[0].template_parameter[0].bottom = bounds['bottom'] record.payload = bid_response.SerializeToString() self.records = [record] self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) # For some cases we have a stacking error. self.assertTrue(len(record.problems) in [1, 2])
def testValidateHtmlSnippetSubstitutions(self): """Tests that ValidateHtmlSnippet does correct macro substitutions.""" snippet = ('%%CLICK_URL_UNESC%%\n' '%%CLICK_URL_ESC%%\n' '%%SITE%%\n' '%%CACHEBUSTER%%\n' '%%WINNING_PRICE%%\n' '%%WINNING_PRICE_ESC%%\n') bid_response, record = self.CreateSuccessfulRecord() record.bid_request.url = 'http://some.url.com/page?q=v' bid_response.ad[0].html_snippet = snippet record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.ValidateHtmlSnippet(bid_response.ad[0], 0, # ad index record) self.assertEqual(0, len(record.problems)) self.assertTrue(0 in record.html_snippets) substituted_snippet = record.html_snippets[0] pattern_str = '%s\n%s\n%s\n%s\n%s\n%s\n' % ( re.escape(log.LogSummarizer.CLICK_URL_UNESC), re.escape(log.LogSummarizer.CLICK_URL_ESC), re.escape('some.url.com'), # The domain we set in the request. '\d+', # Cachebuster. '[\d\.]+', # Winning price. '[\d\.]+', # Winning price. ) pattern_obj = re.compile(pattern_str, re.MULTILINE) self.assertFalse(pattern_obj.match(substituted_snippet) is None)
def testSummarizeWithGoodVideoAdRecord(self): """Tests summarizing with one good record.""" _, record = self.CreateSuccessfulVideoRecord() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNGoodRequests(1) self.assertTrue(record in self.summarizer._good) self.assertEqual(0, len(record.problems))
def testSummarizeOneGoodTemplateAd(self): """Tests summarizing a response with one template ad.""" _, record = self.CreateSuccessfulTemplateRecord() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNGoodRequests(1) self.assertTrue(record in self.summarizer._good) self.assertEqual(0, len(record.problems))
def testSummarizeWithoutAds(self): """Tests that lack of ads is considered a valid response.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ClearField('ad') record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNGoodRequests(1, no_bids=1) self.assertTrue(record in self.summarizer._good)
def testSummarizeUnparseableResonse(self): """Tests summarizing with multiple good records.""" _, record = self.CreateSuccessfulRecord() record.payload = 'garbage' self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNInvalidResponses(1) self.assertTrue(record in self.summarizer._invalid) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateWithNonIntParameters(self): """Tests summarizing a template ad with non-integer parameters.""" bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].snippet_template = 'Bad %%Px%% and %%Py%% params!' record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeInvalidAdSlot(self): """Tests summarizing an adslot with an invalid id.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].adslot[0].id = record.bid_request.adslot[0].id + 1 record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeZeroMinCpm(self): """Tests summarizing an adslot with zero min CPM.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].adslot[0].min_cpm_micros = 0 record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeManyGoodRecords(self): """Tests summarizing with multiple good records.""" for _ in range(10): _, record = self.CreateSuccessfulRecord() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNGoodRequests(10) for record in self.records: self.assertTrue(record in self.summarizer._good) self.assertEqual(0, len(record.problems))
def testSummarizeWithoutProcessingTime(self): """Tests that lack of processing time generates an error message.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ClearField('processing_time_ms') record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateWithBuyerCreativeIdInAd(self): """Tests summarizing a template ad a buyer_creative_id in the parent.""" bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].buyer_creative_id = '123' record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeHasUnescapedClickMacro(self): """Tests summarizing when a snippet has the unescaped click url macro.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].html_snippet = '%%CLICK_URL_UNESC%%' record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNGoodRequests(1) self.assertTrue(record in self.summarizer._good) self.assertEqual(0, len(record.problems))
def testSummarizeMissingClickMacros(self): """Tests summarizing an ad with a snippet missing the click url macros.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].html_snippet = 'No click url macro.' record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeEmptySnippet(self): """Tests summarizing an ad with an empty HTML snippet.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].html_snippet = "" record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeInvalidVideoUrl(self): """Tests summarizing an invalid video url.""" bid_response, record = self.CreateSuccessfulVideoRecord() bid_response.ad[0].video_url = 'invalid video url' record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeSnippetAndHosted(self): """Tests summarizing an ad with both a creative_id and an HTML snippet.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].creative_id = 12345 record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateWithParameterMismatch(self): """Tests summarizing a template ad with mismatched parameters.""" bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].snippet_template = '%%P0%%, %%P1%% and %%P2%%!' record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateWithMisnumberedParameters(self): """Tests summarizing a template ad with non-consecutive parameters.""" bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].snippet_template = 'Two %%P0%% params %%P2%%!' record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateWithClickThroughUrlInAd(self): """Tests summarizing a template ad with landing page url in the parent.""" bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].click_through_url.append('http://url.com') record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateMissingBuyerCreativeId(self): """Tests summarizing a template ad missing a buyer_creative_id.""" bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].template_parameter[0].ClearField('buyer_creative_id') record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeNonOkHttpStatus(self): """Tests summarizing a response with a non-200 HTTP status.""" _, record = self.CreateSuccessfulRecord() record.status = 404 self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.assertEqual(0, self.summarizer._responses_ok) self.assertEqual(1, len(self.summarizer._error)) self.assertTrue(record in self.summarizer._error) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateMissingClickThroughUrl(self): """Tests summarizing a template ad missing a landing page url.""" bid_response, record = self.CreateSuccessfulTemplateRecord() bid_response.ad[0].template_parameter[0].ClearField('click_through_url') record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeTemplateAdForVideoRequest(self): """Tests summarizing a template ad for a video ad request..""" bid_response, record = self.CreateSuccessfulTemplateRecord() record.bid_request.video.videoad_start_delay = 1000 record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeNoAdSlot(self): """Tests summarizing an ad without any ad slots.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].ClearField('adslot') record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1, no_bids=1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testSummarizeSnippetAndVideo(self): """Tests summarizing an ad with both a video url and an HTML snippet.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].video_url = "http://video.com" record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))
def testWriteLogsWhileEmpty(self): """Tests writing logs with empty record lists.""" self.SetupLogs() self.summarizer = log.LogSummarizer(self.records) self.summarizer.WriteLogFiles(self.good_log, self.problematic_log, self.invalid_log, self.error_log, self.snippet_log) self.CheckLogHasNLines(self.good_log, 0, exact=True) self.CheckLogHasNLines(self.problematic_log, 0, exact=True) self.CheckLogHasNLines(self.invalid_log, 0, exact=True) self.CheckLogHasNLines(self.error_log, 0, exact=True)
def testSummarizeEmptyClickThroughUrl(self): """Tests summarizing an ad with an empty click through url.""" bid_response, record = self.CreateSuccessfulRecord() bid_response.ad[0].click_through_url[0] = "" record.payload = bid_response.SerializeToString() self.records.append(record) self.summarizer = log.LogSummarizer(self.records) self.summarizer.Summarize() self.CheckNProblematicResponses(1) self.assertTrue(record in self.summarizer._problematic) self.assertEqual(1, len(record.problems))