fix: attach_account_to_invoice assigns queryset instead of list to ArrayField#3702
Conversation
📝 WalkthroughWalkthroughModifies ChangesInvoice charge item attachment fix
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Greptile SummaryThis PR fixes a one-line bug in
Confidence Score: 5/5Safe to merge — the change is a minimal, targeted correction to a data-type mismatch that was already silently broken in production. The one-line fix exactly mirrors the two already-correct call sites in the same function, and the new test file directly asserts the post-save type and contents of No files require special attention. Important Files Changed
Reviews (1): Last reviewed commit: "fix: attach_account_to_invoice assigns q..." | Re-trigger Greptile |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
care/emr/tests/test_invoice_api.py (1)
71-111: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winConsider asserting invoice totals too.
Charge items carry non-zero
total_price, but the test never checksinvoice.total_net/total_grossaftersync_invoice_itemsruns — the monetary side-effect of this endpoint goes unverified.✅ Suggested addition
self.assertEqual(self.charge_item_1.paid_invoice, invoice) self.assertEqual(self.charge_item_2.paid_invoice, invoice) + self.assertEqual(invoice.total_gross, Decimal("300.00"))🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@care/emr/tests/test_invoice_api.py` around lines 71 - 111, The attach-account-to-invoice test verifies charge-item linking and statuses, but it does not cover the monetary side effect from sync_invoice_items. In test_attach_account_to_invoice_attaches_all_billable_charge_items, after the POST and refresh_from_db on invoice, add assertions for invoice.total_net and invoice.total_gross to match the expected summed totals from the billable charge items (using the existing charge_item_1 and charge_item_2 fixtures). Keep the checks alongside the existing invoice.charge_items assertions so the endpoint’s invoice total update is covered too.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@care/emr/tests/test_invoice_api.py`:
- Around line 62-69: The URL reversal in the test helper `_get_url` is using the
wrong action name format; update the `reverse()` target to match the DRF
`@action` method name with underscores, not hyphens. Use the router action name
associated with `invoice_attach_account_to_invoice` so it resolves the generated
route correctly when building the invoice URL.
---
Nitpick comments:
In `@care/emr/tests/test_invoice_api.py`:
- Around line 71-111: The attach-account-to-invoice test verifies charge-item
linking and statuses, but it does not cover the monetary side effect from
sync_invoice_items. In
test_attach_account_to_invoice_attaches_all_billable_charge_items, after the
POST and refresh_from_db on invoice, add assertions for invoice.total_net and
invoice.total_gross to match the expected summed totals from the billable charge
items (using the existing charge_item_1 and charge_item_2 fixtures). Keep the
checks alongside the existing invoice.charge_items assertions so the endpoint’s
invoice total update is covered too.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 29fcb143-1177-48fc-93c5-d3029f305e34
📒 Files selected for processing (2)
care/emr/api/viewsets/invoice.pycare/emr/tests/test_invoice_api.py
|
@coderabbitai Confirmed! The Django URL reverse name is actually |
|
Hey @vigneshhari, could you please take a look at this when you get a chance? Reviewed by both CodeRabbit and Greptile and came back clean. Could you please review the changes i made. Thank you! |
|
Tip For best results, initiate chat on the files or code changes.
✏️ Learnings added
|
Proposed Changes
attach_account_to_invoicewhereinvoice.charge_itemswas assigned a lazy
ValuesListQuerySetinstead of alist, causingincorrect behaviour when saving to the
ArrayField(IntegerField)backingthis field.
charge_items.values_list("id", flat=True)returns aValuesListQuerySet, not a list. Django'sArrayField.get_db_prep_valueonly handles
listortuple— anything else is passed through raw topsycopg, which either crashes or silently misbehaves, meaning charge
items are not correctly attached to the invoice.
file (lines ~143 and ~248) using
list(charge_items.values_list(...))—this is a copy-paste omission where the
list()call was dropped.list(), consistent with the two correctcall sites already in the same file.
charge items, rejection on non-draft invoice, and permission enforcement.
Associated Issue
No existing GitHub issue. Found via manual code audit — the missing
list()call was identified by comparing the threevalues_listcallsites in the same file, two of which correctly wrap with
list().Merge Checklist
/docs@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins
Summary by CodeRabbit