def runTagCheckDouble(api_value, web_page_data1, web_page_data2, tag): """ Using the provided web page data sources, find the content for the tag. Then compare the content values with the expected api value. Return True or False. This function expects a content value and a two web page data sources. Use runTagCheck if expecting only one content value and one web page data source. Use runTagListCheck if expecting multiple content values (in a list). Use runTagListCheckDouble if there are two web page data sources AND content values are in a list. """ # Function returns string in upper case branch_io_tag_from_html_url1 = functions.get_meta_tag_content( web_page_data1, "", tag) branch_io_tag_from_html_url2 = functions.get_meta_tag_content( web_page_data2, "", tag) # Comparison time match1 = functions.compare(api_value, branch_io_tag_from_html_url1) match2 = functions.compare(api_value, branch_io_tag_from_html_url2) if match1 == True and match2 == True: message = " TEST for %s: %s\n" % (tag, PASSED) functions.show_message(message) return True else: message = " TEST for %s: %s\n" % (tag, FAILED) functions.show_message(message) return False
def checkEpisodePodcastList(list): """ If the Episode Podcast List has more than 1 dictionary, this function will iterate through and create a list for podcast_title,podcast_id, and categories_list. If it has only 1 dictionary, this function will get the single value for podcast_title, podcast_id, and categories_list. This function returns podcast_title, podcast_id, and categories_list. """ # If there is more than 1 dictionary in this list... if len(list) > 1: message = "Podcast has more than one dictionary in the list!" functions.show_message(message) list_of_podcast_titles = [] list_of_podcast_ids = [] list_of_categories = [] for item_dict in episode_podcast_list: # <-- iterate through title = item_dict["title"] id = item_dict["id"] category = item_dict["categories"] list_of_podcast_titles.append(title) list_of_podcast_ids.append(id) list_of_categories.append(category) podcast_title = list_of_podcast_titles podcast_id = list_of_podcast_ids categories_list = list_of_categories else: podcast_title = episode_podcast_list[0]["title"] podcast_id = episode_podcast_list[0]["id"] categories_list = episode_podcast_list[0]["categories"] return podcast_title, podcast_id, categories_list
def runTagListCheck(api_value, web_page_data, tag): """ Using the provided web page data source, find the listed content for the tag. Then compare that listed value with the expected api listed value. Return True or False. This function expects a list of content values and a single web page data source. Use runTagCheck if expecting only one content value and one web page data source. Use runTagCheckDouble if there are two web page data sources but only one content value. Use runTagListCheckDouble if there are two web page data sources AND content values are in a list. """ # Function returns string in upper case branch_io_tag_from_html_url = functions.get_meta_tag_content_list( web_page_data, "", tag) # Comparison time match = functions.compare(api_value, branch_io_tag_from_html_url) if match == True: message = " TEST for %s: %s\n" % (tag, PASSED) else: message = " TEST for %s: %s\n" % (tag, FAILED) functions.show_message(message) return match
def compareAndPrint(expected, actual): """ This function simply makes a compare, prints PASSED/FAILED, and returns True/False. """ match = functions.compare(expected, actual) if match: message = " TEST: %s\n" % PASSED else: message = " TEST: %s\n" % FAILED functions.show_message(message) return match
def isNone(param, descriptor): """ Checks if the param value is None. If yes, prints a message using the descriptor and returns True. If not, prints a message and returns False. """ if param is None: message = "%s -> %s is of type None" % (FAILED, descriptor) functions.show_message(message) return True else: message = "%s -> %s is: %s" % (PASSED, descriptor, param) functions.show_message(message) return False
def runTagCheck(api_value, web_page_data, tag): """ Using the provided web page data source, find the content for the tag. Then compare that content value with the expected api value. Return True or False. This function expects a single content value and a single web page data source. """ # Function returns string in upper case branch_io_tag_from_html_url = functions.get_meta_tag_content(web_page_data, "", tag) # Comparison time match = functions.compare(api_value, branch_io_tag_from_html_url) if match == True: message = " TEST for %s: %s\n" % (tag, PASSED) else: message = " TEST for %s: %s\n" % (tag, FAILED) functions.show_message(message) return match
def checkCategoryList(list): """ If there is more than 1 dictionary in the categories_list, this function will iterate through and put all the category names into a new list. if there is only 1 dictionary, this function will get the category name out of it. This function returns category_name. """ # ********************************************************************** # This will be used in test 6 - if code is ever adjusted to handle more # than one category # ********************************************************************** if len(list) > 1: message = "Categories has more than one dictionary in the list!" functions.show_message(message) list_of_category_names = [] for item_dict in categories_list: # <-- iterate through name = item_dict["name"].upper() list_of_category_names.append(name) category_name = list_of_category_names # <-- FYI variable not in use yet else: category_name = categories_list[0]["name"].upper( ) # <-- FYI not in use yet return category_name
def do_they_match(prod_value, preprod_value): """ Takes two boolean values. Assumes the first value is from PROD and the second value is from PREPROD. Prints and logs a message depending on which is true and which is not. Returns True if both values are True. Returns False if either or both are not True. """ # If both values are True, return True. If not, return False. Print and Log a message either way. if prod_value and preprod_value == True: functions.show_message("Yes! Prod and Preprod are True.") return True else: if prod_value == True: functions.show_message("Passed on Prod Only.") elif preprod_value == True: functions.show_message("Passed on PreProd Only.") else: functions.show_message("Both Prod and PreProd Failed.") return False
def check_url(url, descriptor): """ Call a url and return True if the status code is 200 OK. Else return False. If there are any problems, then return an error. """ functions.show_message("Calling: %s" % url) response_object = requests.get( url, timeout=30) #time out if 30 seconds passes with no response code = response_object.status_code if code != 200: functions.show_message(" %s. %s returned %d \n" % (FAILED, descriptor, code)) return False else: functions.show_message(" %s, %s returned 200 \n" % (PASSED, descriptor)) return True
# Add to the counters if there wasn't a match if match == False: meta_tag_og_description_failed += 1 tests_failed += 1 # ----------------------------- # End of Loop tally # ----------------------------- # If a test failed, the station is marked as failed if tests_failed != 0: stations_failed += 1 # Makes the console printout easier to read message = "\n" functions.show_message(message) # ------------------------------------------ # Outside the for loop, print the results # ------------------------------------------ print("==========\nRESULTS\n============") print("metaTitle failed = %d of %d" % (clay_meta_title_failed, total_calls)) print("og.title failed = %d of %d" % (meta_tag_og_title_failed, total_calls)) print("og.description failed = %d of %d" % (meta_tag_og_description_failed, total_calls)) print("---------------------------------") print("Stations failed = %d of %d" % (stations_failed, total_calls))
def singleTest(page_urls): """ This is the test for all the Stage only AdOps NMC Tag verification scripts. This function accepts 1 argument: page_urls. This function returns the number of pages failed so that the main test script can report to Google Sheets. """ failed_pid_test = 0 failed_tag_test = 0 pages_passed = 0 pages_failed = 0 total_calls = 0 for url in page_urls: tests_passed_for_this_page = 0 total_calls += 1 stg_url = url["url"] expected_pid = url["expected_pid"].upper() expected_tag = url["expected_tag"].upper() # Call for the web page data once and use the data for the tests web_page_data = functions.call_url(stg_url)[:PAGE_BUFFER] ################################################### # TEST 1 of 2: Compare HTML nmc:pid with expected ################################################### # Send web page data to AdOps_HTML_NMCTagTest # Check to see if it matches the expected value # Get back true/false status = runTest.test_nmc_pid_single(web_page_data, expected_pid) if status == True: message = " TEST 1 of 2: %s\n" % PASSED tests_passed_for_this_page += 1 else: message = " TEST 1 of 2: %s\n" % FAILED failed_pid_test += 1 functions.show_message(message) ################################################### # TEST 2 of 2: Compare HTML nmc:tag with expected ################################################### # Send web page data to AdOps_HTML_NMCTagTest # Check to see if it matches the expected value # Get back true/false status = runTest.test_nmc_tag_single(web_page_data, expected_tag) if status == True: message = " TEST 2 of 2: %s\n" % PASSED tests_passed_for_this_page += 1 else: message = " TEST 2 of 2: %s\n" % FAILED failed_tag_test += 1 functions.show_message(message) # Print and tally results before going to the next url for testing print("\n****\nThis page Passed = %d of 2\n****\n" % tests_passed_for_this_page) if tests_passed_for_this_page == 2: pages_passed += 1 else: pages_failed += 1 # Printing test results print("\n\n-------RESULTS----------") print("%d of %d pages PASSED" % (pages_passed, total_calls)) print("----------------------------") print("%d of %d NMC:PID Tests FAILED" % (failed_pid_test, total_calls)) print("%d of %d NMC:TAG Tests FAILED" % (failed_tag_test, total_calls)) return (pages_failed)
def baseTest(page_urls, html, html2=""): """ All the Branch IO Test scripts have the same base test set for Station URLs. This function tests the Station Market, Logo, Name, ID, Category, and Genre Name for a given list of page urls. This function accepts 3 arguments: 1) The list of page_urls to test 2) The HTML page under test (example: the article_url or the contest_url) 3) A second HTML page under test which is optional (example: the script for section fronts tests both the primary section front url and the secondary section front url at the same time). """ # Counters for tallying test results total_calls = 0 pages_passed = 0 pages_failed = 0 marketFail = 0 logoFail = 0 nameFail = 0 idFail = 0 categoryFail = 0 genreFail = 0 # Start a loop to go through the given page_urls list for url in page_urls: total_calls += 1 ##################################### # API Variable SECTION ##################################### api_url = url[ "api_url"] # <-- URL is given to us in the page_urls list # Get all of the station data from the core API as a list of dictionaries station_data_text = functions.call_url(api_url) station_data = json.loads( station_data_text) # <-- List of dictionaries station_dictionary = station_data[ "data"] # <-- We ALWAYS want variables from the data section # Set variables for API comparison testing later station_id = station_dictionary["id"] station_market = station_dictionary["attributes"]["market"][ "display_name"] station_name = station_dictionary["attributes"]["name"] station_logo = station_dictionary["attributes"]["square_logo_small"] # Category is a LIST (of ONLY ONE dictionary) that is nested inside of "attributes" station_category = station_dictionary["attributes"]["category"] # Genre is a LIST (of AT LEAST ONE dictionary) that is nested inside of "attributes" station_genre = station_dictionary["attributes"]["genre"] # Check if there is more than one dictionary in this list # Separate out the genre names from the other dictionary values if len(station_genre) > 1: message = "Station has more than one genre dictionary in the list!" functions.show_message(message) list_of_genre_names = [] for item_dict in station_genre: name = item_dict["name"] list_of_genre_names.append(name) station_genre_name = list_of_genre_names print("There is more than one genre name!! API returns: %s" % station_genre_name) else: station_genre_name = station_genre[0]["name"] # If any variables are empty, log them station_market = functions.isNoneOrEmpty(station_market, "Station Market") station_logo = functions.isNoneOrEmpty(station_logo, "Station Logo") station_name = functions.isNoneOrEmpty(station_name, "Station Name") station_category = functions.isCategoryOrGenreEmpty( station_category, "Station Category") station_genre = functions.isCategoryOrGenreEmpty( station_genre, "Station Genre") station_id = functions.isIDEmpty(station_id) # Check if Genre Name is a list (the list we made earlier or just a single value) # Make uppercase for comparison testing if type(station_genre_name) is list: message = "The API Genre is a list." station_genre_name = [item.upper() for item in station_genre_name] else: message = "The API Genre is a single value." station_genre_name = station_genre_name.upper() functions.show_message(message) # Check if category (the value not the section) is a list # Make uppercase for comparison testing if type(station_category) is list: message = "The API Category is a list." station_category = [item.upper() for item in station_category] else: message = "The API Category is a single value." station_category = station_category.upper() functions.show_message(message) ##################################### # HTML Variable SECTION ##################################### html_url1 = url[html] # <-- URL is given to us in the page_urls list url1_web_page_data = functions.call_url(html_url1)[:PAGE_BUFFER] # Some scripts have 2 HTML URLs to verify against. Some have only one. if html2 != "": html_url2 = url[html2] url2_web_page_data = functions.call_url(html_url2)[:PAGE_BUFFER] else: url2_web_page_data = "" ##################################### # TEST SECTION ##################################### # Tests with 1 HTML URL if url2_web_page_data == "": market_test = test.runTagCheck(station_market, url1_web_page_data, "branch:deeplink:market") logo_test = test.runTagCheck(station_logo, url1_web_page_data, "branch:deeplink:station_logo") name_test = test.runTagCheck(station_name, url1_web_page_data, "branch:deeplink:station_name") id_test = test.runTagCheck(str(station_id), url1_web_page_data, "branch:deeplink:station_id") # If there is a list of categories to compare against if type(station_category) is list: category_test = test.runTagListCheck( station_category, url1_web_page_data, "branch:deeplink:category") elif type(station_category) is str: category_test = test.runTagCheck(station_category, url1_web_page_data, "branch:deeplink:category") # If there is a list of genre names to compare against if type(station_genre_name) is list: genre_test = test.runTagListCheck(station_genre_name, url1_web_page_data, "branch:deeplink:genre") elif type(station_genre_name) is str: genre_test = test.runTagCheck(station_genre_name, url1_web_page_data, "branch:deeplink:genre") # Tests with 2 HTML URLs else: market_test = test.runTagCheckDouble(station_market, url1_web_page_data, url2_web_page_data, "branch:deeplink:market") logo_test = test.runTagCheckDouble(station_logo, url1_web_page_data, url2_web_page_data, "branch:deeplink:station_logo") name_test = test.runTagCheckDouble(station_name, url1_web_page_data, url2_web_page_data, "branch:deeplink:station_name") id_test = test.runTagCheckDouble(str(station_id), url1_web_page_data, url2_web_page_data, "branch:deeplink:station_id") # If there is a list of genre names to compare against if type(station_category) is list: category_test = test.runTagListCheckDouble( station_category, url1_web_page_data, url2_web_page_data, "branch:deeplink:category") elif type(station_category) is str: category_test = test.runTagCheckDouble( station_category, url1_web_page_data, url2_web_page_data, "branch:deeplink:category") # If there is a list of genre names to compare against if type(station_genre_name) is list: genre_test = test.runTagListCheckDouble( station_genre_name, url1_web_page_data, url2_web_page_data, "branch:deeplink:genre") elif type(station_genre_name) is str: genre_test = test.runTagCheckDouble(station_genre_name, url1_web_page_data, url2_web_page_data, "branch:deeplink:genre") ##################################### # TEST RESULTS SECTION ##################################### # Counter for how many of the 6 tests that this set of urls failed urlFailCount = 0 # Check if all are truthy if market_test and logo_test and name_test and id_test and category_test and genre_test: message = "\nAll tests passed for this URL set.\n\n" pages_passed += 1 # If at least one is not True, gotta count what failed and how many of them else: pages_failed += 1 # <-- Increment the overall/total URL Failure count if market_test == False: urlFailCount += 1 marketFail += 1 if logo_test == False: urlFailCount += 1 logoFail += 1 if name_test == False: urlFailCount += 1 nameFail += 1 if id_test == False: urlFailCount += 1 idFail += 1 if category_test == False: urlFailCount += 1 categoryFail += 1 if genre_test == False: urlFailCount += 1 genreFail += 1 # Print and tally results before going to the next url for testing message = "\n%d of 6 Tests Failed!!\n\n" % urlFailCount functions.show_message(message) # Finally outside the for loop # Printing test results print("\n-------STATION BASE TEST RESULTS----------") print("%d of %d Total Calls PASSED" % (pages_passed, total_calls)) print("----------------------------") print("%d of %d Market Tests FAILED" % (marketFail, total_calls)) print("%d of %d Logo Tests FAILED" % (logoFail, total_calls)) print("%d of %d Name Tests FAILED" % (nameFail, total_calls)) print("%d of %d ID Tests FAILED" % (idFail, total_calls)) print("%d of %d Category Tests FAILED" % (categoryFail, total_calls)) print("%d of %d Genre Name Tests FAILED" % (genreFail, total_calls)) print("-------STATION BASE TEST RESULTS----------\n\n") # Return how many total URL failures there were return pages_failed