def check_farm_action(student_module): """`farm_action` returns correct values for all test cases""" StandardChecks.n_params(student_module.farm_action, n_params=7) cases = [ ( ("sunny", "day", True, "pasture", "spring", False, True), "take cows to cowshed\nmilk cows\ntake cows back to pasture", ), (("rainy", "night", False, "cowshed", "winter", False, True), "wait"), ( ("rainy", "night", False, "cowshed", "winter", True, True), "fertilize pasture", ), (("windy", "night", True, "cowshed", "winter", True, True), "milk cows"), (("bowling", "night", False, "cowshed", "winter", False, True), "wait"), (("sunny", "night", True, "cowshed", "summer", False, True), "milk cows"), ] for args, return_val in cases: assert student_module.farm_action(*args) == return_val, ( "Your implementation did not work when called with these parameters: " + "`" + str(args) + "`")
def check_random_koala_fact(student_module): StandardChecks.n_params(student_module.random_koala_fact, n_params=0) facts = __get_all_facts(student_module) for _ in range(10): assert ( student_module.random_koala_fact() in facts ), "`random_koala_fact` returned something other than a koala fact"
def check_commentator_init(student_module): """The `Commentator.__init__` method has been implemented correctly""" StandardChecks.n_params(student_module.Commentator, n_params=1) commentator = student_module.Commentator("Super Double Plus Commentator") assert ( commentator.name == "Super Double Plus Commentator" ), "`.name` should be set to the value of the `name` parameter passed to `__init__`"
def check_platform(student_module): StandardChecks.n_params(student_module.platform, n_params=0) expected_platform = sys.platform platform = student_module.platform() assert ( student_module.platform() == sys.platform ), f"Expected platform to be {expected_platform}, but it was {platform}"
def check_won_golden_globe(student_module): StandardChecks.n_params(student_module.won_golden_globe, n_params=1) assert student_module.won_golden_globe("Jeff") is False assert student_module.won_golden_globe("jaws") is True assert student_module.won_golden_globe("JAWS") is True assert student_module.won_golden_globe("memoirs of a geisha") is True assert student_module.won_golden_globe("test") is False
def check_alphabetical_order(student_module): StandardChecks.n_params(student_module.alphabetical_order, n_params=1) assert student_module.alphabetical_order(["b", "a", "c"]) == ["a", "b", "c"] assert student_module.alphabetical_order(["b", "c", "d"]) == ["b", "c", "d"] assert student_module.alphabetical_order([5, 1, 5]) == [1, 5, 5]
def check_remove_toto_albums(student_module): StandardChecks.n_params(student_module.remove_toto_albums, n_params=1) assert student_module.remove_toto_albums(["Old Is New"]) == [] assert student_module.remove_toto_albums([]) == [] assert student_module.remove_toto_albums(["test", "Old Is New"]) == ["test"] assert student_module.remove_toto_albums( ["test", "Fahrenheit", "Old Is New"]) == ["test"]
def check_most_vowels(student_module): StandardChecks.n_params(student_module.most_vowels, n_params=1) countries = student_module.get_countries() assert set(student_module.most_vowels(countries)) == { "South Georgia and the South Sandwich Islands", "Micronesia, Federated States of", "United States Minor Outlying Islands", }, "Your top 3 is not correct."
def check_player_strength(student_module): """The `Player.strength` method is implemented correctly""" StandardChecks.n_params(student_module.Player, n_params=4) tip = "`.strength()` should return a tuple of the player's best attribute and its value" Player = student_module.Player assert Player("Super Bob", 0.3, 0.5, 0.5).strength() == ("endurance", 0.5), tip assert Player("_ _", 0.3, 0.5, 0.6).strength() == ("accuracy", 0.6), tip assert Player("_ _", 0.3, 0.5, 0.6).strength() == ("accuracy", 0.6), tip assert Player("_ _", 0.7, 0.5, 0.6).strength() == ("speed", 0.7), tip
def check_commentator_sum_player(student_module): """The `Commentator.sum_player` method has been implemented correctly""" StandardChecks.n_params(student_module.Commentator, n_params=1) StandardChecks.n_params(student_module.Commentator.sum_player, n_params=2) commentator = student_module.Commentator("Super Double Plus Commentator") player = student_module.Player("Super Bob", 0.3, 0.5, 0.5) assert ( commentator.sum_player(player) == 1.3 ), "`.sum_player()` should return the sum of the player's speed, endurance and accuracy"
def check_alphabet_set(student_module): StandardChecks.n_params(student_module.alphabet_set, n_params=1) countries = student_module.get_countries() alpha = {chr(i) for i in range(97, 123)} provided = set("".join( [x.lower() for x in student_module.alphabet_set(countries)])) assert provided.issuperset( alpha ), f"Your set of countries does not provide these letters: {alpha - provided}"
def check_add_rating(student_module): """`add_rating_to_restaurant` is implemented correctly""" StandardChecks.n_params(student_module.add_rating_to_restaurant, n_params=0) current_rating_count = student_module.models.Rating.select().count() student_module.add_rating_to_restaurant() new_rating_count = student_module.models.Rating.select().count() assert ( current_rating_count < new_rating_count ), f"Expected number of ratings to go from {current_rating_count} to {new_rating_count}"
def check_wait(student_module): StandardChecks.n_params(student_module.wait, n_params=1) wait_duration = random.random() start = time.time() student_module.wait(wait_duration) actual_duration = time.time() - start assert round(actual_duration, 1) == round( wait_duration, 1 ), f"Expected to wait for {wait_duration} but actually waited for {actual_duration}"
def check_greet(student_module): StandardChecks.n_params(student_module.greet, n_params=2, count_optionals=True) name = f"Bob #{RANDOM_STR}" assert (student_module.greet(name) == f"Hello, {name}!" ), "The greeting for the default case is not correct." template = f"Testing, <name>{RANDOM_STR}!" assert student_module.greet(name, template) == template.replace( "<name>", name), "The greeting with a template is not not correct."
def check_cheapest_dish(student_module): """`cheapest_dish` is implemented correctly""" StandardChecks.n_params(student_module.cheapest_dish, n_params=0) StandardChecks.n_params(student_module.models.Dish.select, n_params=1) dish = student_module.cheapest_dish() assert dish == ( student_module.models.Dish.select() .order_by(student_module.models.Dish.price_in_cents) .first() ), f"Expected the cheapest dish to be {dish}"
def check_get_item_from_list(student_module): StandardChecks.n_params(student_module.get_item_from_list, n_params=2) foo = list(range(10)) assert student_module.get_item_from_list(foo, 9) == 9 assert student_module.get_item_from_list(foo, -1) == 9 assert student_module.get_item_from_list(foo, 10) is None src = strip_comments(inspect.getsource(student_module.get_item_from_list)) assert ( "try" in src and "if" not in src ), "`get_item_from_list` should make use of `try..except` and not `if`"
def check_add(student_module): StandardChecks.n_params(student_module.add, n_params=2) assert student_module.add(1, 1) == 2, "`1+1` should be 2" assert student_module.add(1.2, 3.8) == 5, "`1.2 + 3.8` should be 5" assert student_module.add("Hi", 2) == 0, "'Hi' + 2 should return `0`" assert student_module.add(3.2, "Hello") == 0, "`3.2 + 'Hello' should return `0`" src = strip_comments(inspect.getsource(student_module.add)) assert ("try" in src and "if" not in src), "`add` should make use of `try..except` and not `if`"
def check_cache_zip(student_module): StandardChecks.n_params(student_module.cache_zip, n_params=2) __clean(student_module) cache_path, zip_path = __get_paths(student_module) student_module.cache_zip(zip_path, cache_path) n_cached_files = len(os.listdir(cache_path)) assert ( n_cached_files == 1000 ), f"Expected to find 1000 files in the cache folder after running `cache_zip` but found {n_cached_files}" __clean(student_module)
def check_vegetarian_dishes(student_module): """`vegetarian_dishes` is implemented correctly""" StandardChecks.n_params(student_module.vegetarian_dishes, n_params=0) StandardChecks.n_params(student_module.models.Dish.select, n_params=1) dishes = student_module.vegetarian_dishes() assert set(dishes) == set( [ dish for dish in student_module.models.Dish.select() if all([i.is_vegetarian for i in dish.ingredients]) ] ), f"Expected the set of vegetarian dishes to be {set(dishes)}"
def check_add_dish_to_menu(student_module): """`add_dish_to_menu` is implemented correctly""" StandardChecks.n_params(student_module.add_dish_to_menu, n_params=0) new_dish = student_module.add_dish_to_menu() assert new_dish, "Expected the new dish to be returned" assert "cheese" in [ x.name for x in new_dish.ingredients ], "Expected 'cheese' to be in the ingredients for the new dish" assert ( student_module.models.Ingredient.select() .where(student_module.models.Ingredient.name == "cheese") .count() == 1 ), "The ingredient 'cheese' was created twice"
def check_best_restaurant(student_module): """`best_average_rating` is implemented correctly""" StandardChecks.n_params(student_module.best_average_rating, n_params=0) restaurant = student_module.best_average_rating() assert restaurant == ( student_module.models.Restaurant.select( student_module.models.Restaurant, peewee.fn.AVG(student_module.models.Rating.rating).alias("average"), ) .join(student_module.models.Rating) .group_by(student_module.models.Restaurant) .order_by(peewee.fn.AVG(student_module.models.Rating.rating).desc()) .first() ), f"Expected the restaurant with the best average rating to be {restaurant}"
def check_read_file(student_module): StandardChecks.n_params(student_module.read_file, n_params=1) main_abspath = utils.get_main_abspath(student_module) with open(main_abspath, "r") as f: main_src = f.read() assert (student_module.read_file(main_abspath) == main_src ), "`read_file` doesn't read files correctly" assert (student_module.read_file("doesnotexist9912.xtx") == "" ), "`read_file` doesn't handle files that don't exist correctly" src = strip_comments(inspect.getsource(student_module.read_file)) assert ("try" in src and "if" not in src ), "`read_file` should make use of `try..except` and not `if`"
def check_unique_koala_fact(student_module): StandardChecks.n_params(student_module.unique_koala_facts, n_params=1) facts = __get_all_facts(student_module) for n in range(15): assert (len(set(student_module.unique_koala_facts(n))) == n ), "`unique_koala_facts` returned duplicate koala facts" assert set(student_module.unique_koala_facts(n)).issubset( facts), "Some of your return values are not koala facts" n_unique_facts = 29 assert ( len(student_module.unique_koala_facts(50)) == n_unique_facts ), "`unique_koala_facts` returned more supposedly unique facts than possible"
def check_add_stamp(student_module): StandardChecks.n_params(student_module.create_passport, n_params=5) StandardChecks.n_params(student_module.add_stamp, n_params=2) passport = student_module.create_passport(**TEST_DATA) nationality = TEST_DATA["nationality"] assert nationality not in passport.get( "stamps", {}), f"Did not expect to find {nationality} in stamps" for country in ["Afghanistan", "Bulgaria"]: assert country not in passport.get( "stamps", {}), f"Did not expect to find {country} in the passport yet" passport = student_module.add_stamp(passport, country) assert (country in passport["stamps"] ), f"Expected to find {country} in the passport"
def check_clean_cache(student_module): StandardChecks.n_params(student_module.clean_cache, n_params=0) cache_path, _ = __get_paths(student_module) student_module.clean_cache() assert os.path.isdir(cache_path), f"`cache_path` did not create {cache_path}" # Put some bogus file in it that should be gone after calling clean_cache() with open(os.path.join(cache_path, "throwaway"), "w") as fp: fp.write("You can remove this file.") student_module.clean_cache() assert not os.listdir( cache_path ), f"`clean_cache` does not ensure the cache is empty" __clean(student_module)
def check_shortest_names(student_module): StandardChecks.n_params(student_module.get_countries, n_params=0) countries = student_module.get_countries() assert student_module.shortest_names(countries) == [ "Chad", "Cuba", "Guam", "Iran", "Iraq", "Laos", "Mali", "Niue", "Oman", "Peru", "Togo", ]
def check_player_class_init(student_module): """The `Player.__init__` method is implemented correctly""" StandardChecks.n_params(student_module.Player, n_params=4) player = student_module.Player("Super Bob", 0.3, 0.5, 0.5) assert player.name == "Super Bob", "player.name is not initialized correctly" assert player.speed == 0.3 assert player.endurance == 0.5 assert player.accuracy == 0.5 try: student_module.Player("Bob", -2, 5, 9) raise Exception( "The Player class init function doesn't handle erroneous values correctly." ) except ValueError: # Expected behavior pass
def check_find_password(student_module): try: check_cached_files(student_module) except: raise AssertionError( "You need to implement `cached_files` before we can check `find_password`" ) StandardChecks.n_params(student_module.cache_zip, n_params=2) StandardChecks.n_params(student_module.cached_files, n_params=0) cache_path, zip_path = __get_paths(student_module) student_module.cache_zip(zip_path, cache_path) assert ( student_module.find_password(student_module.cached_files()) == "correct_horse_battery_staple" ), "The returned password is not correct."
def check_force(student_module): StandardChecks.n_params(student_module.force, n_params=2, count_optionals=True) assert student_module.force( 10) == 98, "Did you use earth's gravity by default?" assert (round(student_module.force(50)) == 490 ), "Did you use earth's gravity by default?" assert student_module.force( 10, "sun") == 2740, "'sun' is not handled correctly" assert student_module.force( 10, "pluto") == 6, "'pluto' is not handled correctly" assert (student_module.force( 10, "saturn") == 104), "'saturn' is not handled correctly" assert student_module.force( 10, "earth") == 98, "'earth' is not handled correctly"
def check_dinner_date_possible(student_module): """`dinner_date_possible` is implemented correctly""" StandardChecks.n_params(student_module.dinner_date_possible, n_params=0) date_restaurants = student_module.dinner_date_possible() assert set(date_restaurants) == set( [ restaurant for restaurant in student_module.models.Restaurant.select() .where(student_module.models.Restaurant.opening_time <= "19:00") .where(student_module.models.Restaurant.closing_time >= "19:00") if any( [ all([i.is_vegan for i in dish.ingredients]) for dish in restaurant.dish_set.select() ] ) ] ), f"Expected dinner date restaurants to be {date_restaurants}"