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 unit tests for partial input optional field behaviour in update mutations #638

Conversation

SupImDos
Copy link
Contributor

@SupImDos SupImDos commented Oct 7, 2024

Description

As noted in the module's docstring, this PR adds some unit tests for Strawberry-Django's handling of partial input optional fields in update mutations, specifically when their values are omitted or explicitly set to null, for different variations of model fields:

  • Required fields
  • Optional and nullable fields
  • Optional and non-nullable fields
  • Required foreign key fields
  • Optional foreign key fields
  • Many-to-many fields

These tests stem from the fact that the GraphQL type-system doesn't distinguish between optional and nullable. That is, type T! is both required and non-nullable (i.e., must be supplied and cannot be null), but type T is both optional and nullable (i.e., can be omitted and can be null).

These tests uncovered what I believe is a small bug in update_m2m, where a value of None wasn't being explicitly checked.

For reference, we were going to add similar unit tests to our project, but thought they might be useful upstream.

Types of Changes

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

Issues Fixed or Closed by This PR

  • N/A

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).

Summary by Sourcery

Add unit tests for handling partial input optional fields in update mutations, covering various model field types and ensuring correct behavior when values are omitted or set to null. Fix a bug in the update_m2m function to handle None values correctly.

Bug Fixes:

  • Fix a bug in the update_m2m function where a value of None was not being explicitly checked, ensuring correct handling of None values.

Tests:

  • Add comprehensive unit tests to verify the behavior of partial input optional fields in update mutations for various model field types, including required fields, optional and nullable fields, optional and non-nullable fields, required foreign key fields, optional foreign key fields, and many-to-many fields.
  • Include tests for both standard and Relay GraphQL mutations to ensure consistent handling of omitted and null values across different field types.

Copy link
Contributor

sourcery-ai bot commented Oct 7, 2024

🧙 Sourcery has finished reviewing your pull request!


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 @SupImDos - 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: all looks good
  • 🟢 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.

@SupImDos SupImDos marked this pull request as draft October 7, 2024 06:24
@SupImDos
Copy link
Contributor Author

SupImDos commented Oct 7, 2024

@sourcery-ai review

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 @SupImDos - 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: all looks good
  • 🟢 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.

@SupImDos SupImDos marked this pull request as ready for review October 7, 2024 08:21
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.

We have skipped reviewing this pull request. It looks like we've already reviewed the commit cf21c16 in this pull request.

Copy link
Member

@bellini666 bellini666 left a comment

Choose a reason for hiding this comment

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

Thank you very much for this! ❤️

@bellini666 bellini666 merged commit 92c1221 into strawberry-graphql:main Oct 19, 2024
20 checks passed
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.

2 participants