From 0934881015db48f499e673678655a5db7ae746e2 Mon Sep 17 00:00:00 2001 From: Lucas Pickering Date: Sat, 19 Aug 2023 19:38:32 -0400 Subject: [PATCH] Fix updating hold-attached moves --- api/src/core/schema/mutation.py | 15 ++--- api/src/core/tests/factories.py | 6 ++ .../schema/mutation/test_mutate_beta_move.py | 65 ++++++++++--------- api/src/schema.graphql | 2 +- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/api/src/core/schema/mutation.py b/api/src/core/schema/mutation.py index e18c426e..70ba0a24 100644 --- a/api/src/core/schema/mutation.py +++ b/api/src/core/schema/mutation.py @@ -346,7 +346,8 @@ def create_beta_move( previous_beta_move: Annotated[ Optional[relay.GlobalID], strawberry.argument( - description="Move prior to this one in the beta" + description="Move prior to this one in the beta " + "(null for first move)" ), ], ) -> BetaMoveNode: @@ -357,13 +358,9 @@ def create_beta_move( # Convert GQL IDs to PKs beta_dj: Beta = beta.resolve_node_sync(info, ensure_type=Beta) - hold_dj: Hold | None = ( - hold.resolve_node_sync(info, ensure_type=Hold) if hold else None - ) - previous_beta_move_dj: BetaMove | None = ( + hold_dj = hold and (hold.resolve_node_sync(info, ensure_type=Hold)) + previous_beta_move_dj = previous_beta_move and ( previous_beta_move.resolve_node_sync(info, ensure_type=BetaMove) - if previous_beta_move - else None ) # Convert position from SVG coords to normalized [0,1] @@ -423,9 +420,7 @@ def update_beta_move( beta_move_dj: BetaMove = id.resolve_node_sync( info, ensure_type=BetaMove ) - hold_dj: Hold | None = ( - hold.resolve_node_sync(info, ensure_type=Hold) if hold else None - ) + hold_dj = hold and (hold.resolve_node_sync(info, ensure_type=Hold)) normal_position = position and position.to_normalized( beta_move_dj.beta.problem.boulder.image ) diff --git a/api/src/core/tests/factories.py b/api/src/core/tests/factories.py index 565d631b..059a845d 100644 --- a/api/src/core/tests/factories.py +++ b/api/src/core/tests/factories.py @@ -1,4 +1,5 @@ import factory +import factory.fuzzy import factory.random from django.contrib.auth.models import User from factory import Factory @@ -11,6 +12,7 @@ from core.models import ( Beta, BetaMove, + BodyPart, Boulder, Hold, HoldAnnotationSource, @@ -103,6 +105,10 @@ class Params: beta = factory.SubFactory(BetaFactory, moves=[]) order = factory.Sequence(lambda n: n) + body_part = Faker( + "random_element", + elements=[key for key, _ in BodyPart.choices], + ) hold = factory.Maybe( "is_free", yes_declaration=None, diff --git a/api/src/core/tests/schema/mutation/test_mutate_beta_move.py b/api/src/core/tests/schema/mutation/test_mutate_beta_move.py index 12a4ea95..5fa2f534 100644 --- a/api/src/core/tests/schema/mutation/test_mutate_beta_move.py +++ b/api/src/core/tests/schema/mutation/test_mutate_beta_move.py @@ -18,50 +18,29 @@ bodyPart isStart annotation - target + target { + __typename + ... on HoldNode { + id + } + ... on SVGPosition { + x + y + } + } permissions { canEdit canDelete } - beta { - moves { - edges { - node { - id - order - } - } - } - } } } """ -update_beta_move_mutation = """ +update_annotation_mutation = """ mutation($input: UpdateBetaMoveInput!) { updateBetaMove(input: $input) { id - order - bodyPart - isStart annotation - target { - __typename - } - permissions { - canEdit - canDelete - } - beta { - moves { - edges { - node { - id - order - } - } - } - } } } """ @@ -85,6 +64,28 @@ """ +@pytest.mark.parametrize( + "beta_move__is_free", [False, True], ids=["hold", "free"] +) +def test_update_annotation( + context: StrawberryDjangoContext, beta_move: BetaMove +) -> None: + beta_move_id = relay.to_base64(BetaMoveNode, beta_move.id) + assert_graphql_result( + schema.execute_sync( + update_annotation_mutation, + context_value=context, + variable_values={ + "input": {"id": beta_move_id, "annotation": "new value"} + }, + ), + {"updateBetaMove": {"id": beta_move_id, "annotation": "new value"}}, + ) + + +# TODO test missing permissions + + def test_delete_beta_move( context: StrawberryDjangoContext, beta_move: BetaMove ) -> None: diff --git a/api/src/schema.graphql b/api/src/schema.graphql index 699f3fc4..cb0d3b9d 100644 --- a/api/src/schema.graphql +++ b/api/src/schema.graphql @@ -134,7 +134,7 @@ input CreateBetaMoveInput { hold: ID position: SVGPositionInput - """Move prior to this one in the beta""" + """Move prior to this one in the beta (null for first move)""" previousBetaMove: ID }