From 98225ca9ea571c7fbd626e13edbe9df730c96398 Mon Sep 17 00:00:00 2001 From: Eve Martin Date: Tue, 27 Aug 2024 15:48:26 +0000 Subject: [PATCH] run black, write test, fix test --- game/end_to_end_tests/test_level_selection.py | 28 +++++++++++-- .../0101_update_python_den_level_41.py | 10 +++-- game/views/level.py | 42 +++++-------------- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/game/end_to_end_tests/test_level_selection.py b/game/end_to_end_tests/test_level_selection.py index 59534b42b..891f4c04d 100644 --- a/game/end_to_end_tests/test_level_selection.py +++ b/game/end_to_end_tests/test_level_selection.py @@ -34,7 +34,9 @@ def test_coins(self): page = self.go_to_reverse("levels") # The coin images for the levels - level_coin_images = page.browser.find_elements(By.CSS_SELECTOR, ("#collapse-4 div img")) + level_coin_images = page.browser.find_elements( + By.CSS_SELECTOR, ("#collapse-4 div img") + ) # There are 4 levels in this episode, each with gold coin assert_that(len(level_coin_images), equal_to(4)) @@ -45,13 +47,33 @@ def test_coins(self): ) # So the episode has a gold coin too - episode_coin_image = page.browser.find_element(By.CSS_SELECTOR, "#episode-4 > p > img") + episode_coin_image = page.browser.find_element( + By.CSS_SELECTOR, "#episode-4 > p > img" + ) assert_that( episode_coin_image.get_attribute("src"), ends_with("/static/game/image/coins/coin_gold.svg"), ) try: - image_for_uncomplete_episode = page.browser.find_element(By.CSS_SELECTOR, "#episode-3 > p > img") + image_for_uncomplete_episode = page.browser.find_element( + By.CSS_SELECTOR, "#episode-3 > p > img" + ) except NoSuchElementException as this_should_happen: pass + + def test_redirect_to_levelless_episode(self): + levels_page = self.go_to_reverse("python_levels") + expected_url = self.get_current_url() + + page = self.go_to_level("41", True) + + self.wait_for_element_to_be_clickable((By.ID, "next_button")) + self.find_element(By.ID, "next_button").click() + + current_url = self.get_current_url() + assert current_url == expected_url + + episode_20_header = self.find_element(By.ID, "episode-20") + episode_20_expanded = episode_20_header.get_attribute("aria-expanded") + assert episode_20_expanded == True diff --git a/game/migrations/0101_update_python_den_level_41.py b/game/migrations/0101_update_python_den_level_41.py index 3fbb0d6f0..9fa161ec1 100644 --- a/game/migrations/0101_update_python_den_level_41.py +++ b/game/migrations/0101_update_python_den_level_41.py @@ -1,6 +1,7 @@ from django.apps.registry import Apps from django.db import migrations + def update_python_den_level_41(apps: Apps, *args): Level = apps.get_model("game", "Level") @@ -8,19 +9,20 @@ def update_python_den_level_41(apps: Apps, *args): level41.next_level = None level41.save() + def revert_python_den_level_41(apps: Apps, *args): Level = apps.get_model("game", "Level") - + level41 = Level.objects.get(default=True, name="1041") level41.next_level = Level.objects.get(default=True, name="1042") level41.save() + class Migration(migrations.Migration): dependencies = [("game", "0100_reorder_python_levels")] operations = [ migrations.RunPython( - code=update_python_den_level_41, - reverse_code=revert_python_den_level_41 + code=update_python_den_level_41, reverse_code=revert_python_den_level_41 ) - ] \ No newline at end of file + ] diff --git a/game/views/level.py b/game/views/level.py index 946b0237d..f027517ee 100644 --- a/game/views/level.py +++ b/game/views/level.py @@ -79,9 +79,7 @@ def _prev_level_url(level, user, night_mode, from_python_den): if is_prev_level_locked: while is_prev_level_locked and int(prev_level.name) > 1: prev_level = prev_level.prev_level.all()[0] - is_prev_level_locked = ( - klass in prev_level.locked_for_class.all() - ) + is_prev_level_locked = klass in prev_level.locked_for_class.all() return _level_url(prev_level, night_mode, from_python_den) @@ -95,7 +93,7 @@ def _next_level_url(level, user, night_mode, from_python_den): """ if not level.next_level: - if level.episode.pk == 13: + if level.episode and level.episode.pk == 13: return reverse("python_levels") return "" @@ -112,9 +110,7 @@ def _next_level_url(level, user, night_mode, from_python_den): int(next_level.name) < 60 if from_python_den else 80 ): next_level = next_level.next_level - is_next_level_locked = ( - klass in next_level.locked_for_class.all() - ) + is_next_level_locked = klass in next_level.locked_for_class.all() return _level_url(next_level, night_mode, from_python_den) @@ -135,9 +131,7 @@ def _level_url(level, night_mode, from_python_den): def _default_level_url(level, from_python_den): - viewname = ( - "play_python_default_level" if from_python_den else "play_default_level" - ) + viewname = "play_python_default_level" if from_python_den else "play_default_level" level_name = int(level.name) - 1000 if from_python_den else level.name @@ -169,9 +163,7 @@ def play_level(request, level, from_editor=False, from_python_den=False): """ night_mode = ( - False - if not app_settings.NIGHT_MODE_FEATURE_ENABLED - else "night" in request.GET + False if not app_settings.NIGHT_MODE_FEATURE_ENABLED else "night" in request.GET ) if not permissions.can_play_level( @@ -194,9 +186,7 @@ def play_level(request, level, from_editor=False, from_python_den=False): ) commands_attr = "commands_level" + str(level.name) commands = ( - getattr(messages, commands_attr, None) - if level.default - else level.commands + getattr(messages, commands_attr, None) if level.default else level.commands ) character = level.character character_url = character.top_down @@ -220,9 +210,7 @@ def play_level(request, level, from_editor=False, from_python_den=False): workspace = None python_workspace = None - if not request.user.is_anonymous and hasattr( - request.user.userprofile, "student" - ): + if not request.user.is_anonymous and hasattr(request.user.userprofile, "student"): student = request.user.userprofile.student attempt = ( Attempt.objects.filter( @@ -261,9 +249,7 @@ def play_level(request, level, from_editor=False, from_python_den=False): return_view = ( "level_editor" if from_editor - else "python_levels" - if from_python_den - else "levels" + else "python_levels" if from_python_den else "levels" ) temp_block_data = [] @@ -308,9 +294,7 @@ def play_level(request, level, from_editor=False, from_python_den=False): "next_level_url": _next_level_url( level, request.user, night_mode, from_python_den ), - "flip_night_mode_url": _level_url( - level, not night_mode, from_python_den - ), + "flip_night_mode_url": _level_url(level, not night_mode, from_python_den), "available_language_dict": language_code_dict, }, ) @@ -399,9 +383,7 @@ def close_and_reset(attempt): def load_list_of_workspaces(request): workspaces_owned = [] if permissions.can_create_workspace(request.user): - workspaces_owned = Workspace.objects.filter( - owner=request.user.userprofile - ) + workspaces_owned = Workspace.objects.filter(owner=request.user.userprofile) workspaces = [ { @@ -441,9 +423,7 @@ def save_workspace(request, workspaceID=None): "python_enabled", "pythonViewEnabled", ] - missing_params = [ - param for param in request_params if param not in request.POST - ] + missing_params = [param for param in request_params if param not in request.POST] if missing_params != []: raise Exception( "Request missing the following required parameters", missing_params