Add regression test for bulk_create with M2M field using explicit related_name#582
Conversation
|
The All 13 errors flagged by
None of these lines were modified by this PR. The same errors appear on PR #583 and would affect any PR raised today. Cross-referencing against the last successfully merged PR (#576, Feb 19), the Happy to address these in a separate PR if that would be helpful, but they are out of scope for this change. |
6cc8624 to
d7cfd19
Compare
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded a new Django DB test Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@tests/test_baker.py`:
- Around line 1273-1274: The test only samples two stores (s1, s2) but should
verify every bulk-created Store row; replace the sliced selection that uses
models.Store.objects.all()[:2] with iterating over all stores
(models.Store.objects.all()) and assert for each Store instance (e.g., variable
name like store) that list(store.customers.all()) == [person] so every created
row is checked rather than just the first two.
d7cfd19 to
5b67759
Compare
…ated_name Prior to the fix in the bulk_create pipeline, calling baker.make() with _bulk_create=True on a model whose ManyToManyField defines an explicit related_name would raise a TypeError. The through-table constructor was being invoked with the related_name as a keyword argument instead of the correct column names sourced from m2m_field_name() and m2m_reverse_field_name(). Although the underlying bug has since been resolved, no regression test existed to guard against a recurrence. This commit adds that test using the Store.customers field (related_name="favorite_stores"), which is the canonical example from the original bug report. Changes: - tests/test_baker.py: add test_create_with_field_with_related_name to TestCreateM2MWhenBulkCreate, covering baker.make(Store, customers=[...], _quantity=10, _bulk_create=True) and asserting M2M rows are created correctly for all bulk-created instances Fixes model-bakers#489 Author: Benjamin Aduo (@benaduo)
5b67759 to
ccd776c
Compare
amureki
left a comment
There was a problem hiding this comment.
Fantastic, thank you very much for noting that it was missing a regression test and adding it! 🙌
Summary
TestCreateM2MWhenBulkCreatecovering the scenarioreported in
baker.make()with_bulk_create=Truefails for M2M-fields that have a reverse_name #489: callingbaker.make()with_bulk_create=Trueon a modelwhose
ManyToManyFielddefines an explicitrelated_nameStore.customers(related_name="favorite_stores") with_quantity=10,asserting that all bulk-created instances have the correct M2M rows populated
Motivation
Issue #489 reported a
TypeErrorwhere the through-table constructor was beingcalled with the
related_nameas a keyword argument(
Store_customers(favorite_stores=...)). That bug was fixed in a prior release,but no regression test was ever added for this specific case, leaving the fix
unguarded against future regressions.
Changes
tests/test_baker.py: addtest_create_with_field_with_related_nametoTestCreateM2MWhenBulkCreateTesting
pytest tests/test_baker.py::TestCreateM2MWhenBulkCreate -vAuthor
Benjamin Aduo (@benaduo)
Summary by CodeRabbit