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

Fix flaky test test_bulk_upsert #229

Merged
merged 1 commit into from
Jun 17, 2024
Merged

Fix flaky test test_bulk_upsert #229

merged 1 commit into from
Jun 17, 2024

Conversation

KevinJBoyer
Copy link
Contributor

Ticket

n/a

Changes

  • Make the IDs deterministic and guaranteed to be unique

Context for reviewers

  • Since the IDs were random, there was a small chance (but higher than you might intuitively expect!) of generating a collision. This would trigger integrity errors or cause the behavior of the test to be not what you'd expect (a new "insert" would actually be a "modify", for example)

Testing

First, (temporarily) add pytest-repeat:
poetry add pytest-repeat
Then run the test several thousand times:
make test args="tests/src/db/test_bulk_ops.py --count 2500"
You should observe some failures because a duplicate ID is generated:
Screenshot 2024-06-17 at 8 37 58 AM
Then, re-run after applying the patch:
Screenshot 2024-06-17 at 8 53 47 AM

@KevinJBoyer KevinJBoyer requested a review from lorenyu June 17, 2024 12:59
Copy link
Contributor

@chouinar chouinar left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +18 to +25
_next_id = 0


def get_random_number_object() -> Number:
global _next_id
_next_id += 1
return Number(
id=str(random.randint(1000000, 9999999)),
id=str(_next_id),
Copy link
Contributor

Choose a reason for hiding this comment

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

(not required - just throwing an idea out) You could use Factory boy for building these. While we normally use it for DB models, it also supports creating a dict. Mostly would help with not needing to manage a global counter.
https://github.com/navapbc/simpler-grants-gov/blob/main/api/tests/src/db/models/factories.py#L1269

Not really necessary for just this, but if this was expanded significantly in use I'd recommend it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh nice! I'll defer that change for now, thanks for sharing (also, very convenient that the grants.gov project is open source)

@KevinJBoyer KevinJBoyer merged commit f2d19d5 into main Jun 17, 2024
4 checks passed
@KevinJBoyer KevinJBoyer deleted the kb/fix-flaky branch June 17, 2024 14:57
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