CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl-labs/django-testing

Write correct Django tests — TestCase vs TransactionTestCase, setUpTestData, factory-boy, assertNumQueries, mock.patch placement, and DRF APITestCase patterns

99

1.33x
Quality

99%

Does it follow best practices?

Impact

99%

1.33x

Average score across 2 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mdevals/scenario-2/

E-commerce Checkout Tests

Write tests for a Django e-commerce checkout flow. The app has the following models:

  • Product: name (CharField), price_cents (IntegerField), stock (IntegerField)
  • Cart: user (ForeignKey to User), created_at (DateTimeField auto_now_add)
  • CartItem: cart (ForeignKey to Cart), product (ForeignKey to Product), quantity (PositiveIntegerField)
  • Order: user (ForeignKey to User), status (CharField choices=['pending', 'confirmed', 'shipped', 'cancelled']), total_cents (IntegerField), created_at (DateTimeField auto_now_add)
  • Payment: order (ForeignKey to Order), stripe_charge_id (CharField), amount_cents (IntegerField), status (CharField choices=['succeeded', 'failed'])

Checkout Flow

The checkout endpoint is POST /api/checkout/ and accepts {"cart_id": <int>}. The view is in orders/views.py and does the following:

  1. Validates the cart exists and belongs to the authenticated user
  2. Checks that the cart does not exceed the MAX_CART_ITEMS setting
  3. Calls charge_card(amount_cents, user) imported in orders/views.py as from payments.gateway import charge_card
  4. Creates an Order with status='confirmed' and a Payment record
  5. Uses transaction.on_commit() to send a confirmation email via send_order_confirmation(order), imported in orders/views.py as from notifications.email import send_order_confirmation
  6. Returns 201 with the order data

If the payment fails (charge_card raises PaymentError), the order is created with status='pending' and no email is sent.

Output

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.

evals

tile.json