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

feat: turn off autoflush on integration tests #30394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

betodealmeida
Copy link
Member

@betodealmeida betodealmeida commented Sep 25, 2024

SUMMARY

#24969 introduced the @transaction decorator, to be used in commands to ensure that commits are explicit and happen only once in the lifetime of a request, preventing eagers commits. The PR failed to catch a few cases where legacy API endpoints were not using commands, resulting in successful requests that wouldn't be persisted to the database. These were fixed in #30215.

Why did our integration tests fail to catch these endpoints that were not committing? Tests failed because by default SQLAlchemy will autoflush before running a SELECT. In real world, favoriting a chart and verifying that it was favorited are two separate requests. If the first one fails to commit, the second one won't see a change. But our integration tests share the same global session (db.session), so that an insert followed by a select will result in flush between them, and the non-committed data will be seen even though we use READ COMMITTED as the isolation level.

To prevent this from happening again, this PR turns off the autoflush in the integration tests.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@dosubot dosubot bot added change:backend Requires changing the backend need:tests This PR requires tests labels Sep 25, 2024
@betodealmeida betodealmeida changed the title WIP feat: turn off autoflush on integration tests Sep 25, 2024
# many integration tests write and read in the same session, and because by default
# SQLAlchemy uses autoflush, the test would see data that in real world wouldn't be
# there.
db.session.configure(autoflush=False)
Copy link
Member

Choose a reason for hiding this comment

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

QQ: would this be needed/preferred on the tests/unit_tests/ side of the house too? If so it could be great to factor out into tests/common/ (?)

Copy link
Member

Choose a reason for hiding this comment

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

just that one question, otherwise PR LGTM

@geido geido self-requested a review September 30, 2024 16:42
@michael-s-molina
Copy link
Member

@betodealmeida Another idea would be to keep autoflush in our tests and check the session for pending commits/rollbacks after our tests run. This can be done globally and would work for current and new code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change:backend Requires changing the backend need:tests This PR requires tests preset-io size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants