Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to validation behaviour of graphql-transport-ws subscriptions #3671

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

kristjanvalur
Copy link
Contributor

@kristjanvalur kristjanvalur commented Oct 13, 2024

Description

Additional tests for queries and subscriptions over graphql_transport_ws protocol.

  • tests to ensure that validation runs on a worker Task
  • tests to check error handling
  • tests to check validation

Note:

The tests uncover a discrepancy regarding the validation handling for queries vs. subscriptions. This is a behavior change since the recent changes in the subscriptions code.

When a subscription fails during validation, an error message is sent. But if the same method is a single result (query), a
next message is sent.

Ideally, validation should be handled the same for both. The "errors" field of the ExecutionResult should indicate an error during execution, not pre-validation.

The failing test is test_validation_query while test_validation_subscription succeeds.

The PR is not complete, it serves to illustrate a problem, I am open to suggestions. Accept the behaviour? Make validation return a PreExecutionError? Add some other special handling?

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Copy link

codspeed-hq bot commented Oct 13, 2024

CodSpeed Performance Report

Merging #3671 will not alter performance

Comparing mainframeindustries:subscription_tests (c4d0b05) with main (56172dc)

Summary

✅ 15 untouched benchmarks

@kristjanvalur kristjanvalur marked this pull request as ready for review October 13, 2024 19:57
Copy link
Contributor

sourcery-ai bot commented Oct 13, 2024

Reviewer's Guide by Sourcery

This pull request adds new tests to validate the behavior of GraphQL Transport WebSocket (WS) subscriptions and queries. It focuses on ensuring validation runs on a worker task, error handling, and validation checks for both queries and subscriptions. The changes reveal a discrepancy in how validation errors are handled between queries and subscriptions.

Sequence diagram for validation error handling in GraphQL WS

sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Send query { conditionalFail(fail:true) }
    Server-->>Client: ErrorMessage { "type": "error", "id": "sub1", "message": "failed after sleep None" }
    Client->>Server: Send subscription { conditionalFail(fail:true) }
    Server-->>Client: ErrorMessage { "type": "error", "id": "sub1", "message": "failed after sleep None" }
Loading

Class diagram for ConditionalFailPermission and Schema changes

classDiagram
    class ConditionalFailPermission {
        +string message
        +bool has_permission(source, info, **kwargs)
        +float sleep
        +bool fail
    }
    class Query {
        +string conditional_fail(sleep, fail)
    }
    class Subscription {
        +string conditional_fail(sleep, fail)
    }
    Query --> ConditionalFailPermission
    Subscription --> ConditionalFailPermission
    note for ConditionalFailPermission "Handles permission with optional sleep and fail parameters"
Loading

File-Level Changes

Change Details Files
Added new test cases for GraphQL Transport WS protocol
  • Implemented test for query field errors
  • Added tests for validation of queries and subscriptions
  • Created tests for long-running validation to ensure non-blocking behavior
  • Implemented a test for task error handling
tests/websockets/test_graphql_transport_ws.py
Introduced ConditionalFailPermission class for testing
  • Created ConditionalFailPermission class with configurable failure and sleep behavior
  • Added conditional_fail field to Query and Subscription classes using the new permission
tests/views/schema.py
Added release notes
  • Created RELEASE.md file with patch release type
  • Noted fix for error handling in query operations over graphql-transport-ws
RELEASE.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kristjanvalur - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +993 to +1002
async def test_validation_query(ws: WebSocketClient):
"""
Test validation for query
"""
await ws.send_json(
SubscribeMessage(
id="sub1",
payload=SubscribeMessagePayload(
query="query { conditionalFail(fail:true) }"
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (testing): Inconsistency in error handling between queries and subscriptions

As noted in the PR description, there's a discrepancy in how validation errors are handled for queries vs. subscriptions. This test is failing because it expects an ErrorMessage, but receives a NextMessage instead. Consider updating the implementation to handle validation errors consistently across queries and subscriptions.

Copy link

codecov bot commented Oct 14, 2024

Codecov Report

Attention: Patch coverage is 12.69841% with 55 lines in your changes missing coverage. Please review.

Project coverage is 10.99%. Comparing base (56172dc) to head (c4d0b05).

❗ There is a different number of reports uploaded between BASE (56172dc) and HEAD (c4d0b05). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (56172dc) HEAD (c4d0b05)
2 1
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #3671       +/-   ##
===========================================
- Coverage   97.01%   10.99%   -86.02%     
===========================================
  Files         503      454       -49     
  Lines       33458    29078     -4380     
  Branches     5618     1659     -3959     
===========================================
- Hits        32460     3198    -29262     
- Misses        792    25719    +24927     
+ Partials      206      161       -45     

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant