Write correct Django tests — TestCase vs TransactionTestCase, setUpTestData, factory-boy, assertNumQueries, mock.patch placement, and DRF APITestCase patterns
99
99%
Does it follow best practices?
Impact
99%
1.33xAverage score across 2 eval scenarios
Passed
No known issues
Write tests for a Django e-commerce checkout flow. The app has the following models:
name (CharField), price_cents (IntegerField), stock (IntegerField)user (ForeignKey to User), created_at (DateTimeField auto_now_add)cart (ForeignKey to Cart), product (ForeignKey to Product), quantity (PositiveIntegerField)user (ForeignKey to User), status (CharField choices=['pending', 'confirmed', 'shipped', 'cancelled']), total_cents (IntegerField), created_at (DateTimeField auto_now_add)order (ForeignKey to Order), stripe_charge_id (CharField), amount_cents (IntegerField), status (CharField choices=['succeeded', 'failed'])The checkout endpoint is POST /api/checkout/ and accepts {"cart_id": <int>}. The view is in orders/views.py and does the following:
MAX_CART_ITEMS settingcharge_card(amount_cents, user) imported in orders/views.py as from payments.gateway import charge_cardtransaction.on_commit() to send a confirmation email via send_order_confirmation(order), imported in orders/views.py as from notifications.email import send_order_confirmationIf the payment fails (charge_card raises PaymentError), the order is created with status='pending' and no email is sent.
Produce a test file at orders/tests/test_checkout.py with comprehensive test coverage. Include any supporting factory files you need. Assume models, views, and services already exist; you are only writing the tests.