Skip to content

Commit

Permalink
run black, write test, fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
evemartin committed Aug 27, 2024
1 parent de0a435 commit 98225ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
28 changes: 25 additions & 3 deletions game/end_to_end_tests/test_level_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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
10 changes: 6 additions & 4 deletions game/migrations/0101_update_python_den_level_41.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
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")

level41 = Level.objects.get(default=True, name="1041")
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
)
]
]
42 changes: 11 additions & 31 deletions game/views/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 ""

Expand All @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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,
},
)
Expand Down Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98225ca

Please sign in to comment.